Add Ovaska and Maki-aijala, remove Badorek and Tallskog
[ric-plt/sdlgo.git] / internal / sdlgoredis / sdlgoredis_test.go
index 81c211a..469e8c4 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
    Copyright (c) 2019 AT&T Intellectual Property.
    Copyright (c) 2018-2019 Nokia.
    limitations under the License.
 */
 
+/*
+ * This source code is part of the near-RT RIC (RAN Intelligent Controller)
+ * platform project (RICP).
+ */
+
 package sdlgoredis_test
 
 import (
-       "testing"
        "errors"
+       "strconv"
+       "testing"
        "time"
 
-       "github.com/go-redis/redis"
        "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
+       "github.com/go-redis/redis"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/mock"
 )
@@ -37,6 +42,10 @@ type pubSubMock struct {
        mock.Mock
 }
 
+type MockOS struct {
+       mock.Mock
+}
+
 func (m *pubSubMock) Channel() <-chan *redis.Message {
        return m.Called().Get(0).(chan *redis.Message)
 }
@@ -109,6 +118,26 @@ func (m *clientMock) SCard(key string) *redis.IntCmd {
        return m.Called(key).Get(0).(*redis.IntCmd)
 }
 
+func (m *clientMock) PTTL(key string) *redis.DurationCmd {
+       return m.Called(key).Get(0).(*redis.DurationCmd)
+}
+
+func (m *clientMock) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
+       return m.Called(script, keys).Get(0).(*redis.Cmd)
+}
+
+func (m *clientMock) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
+       return m.Called(sha1, keys, args).Get(0).(*redis.Cmd)
+}
+
+func (m *clientMock) ScriptExists(scripts ...string) *redis.BoolSliceCmd {
+       return m.Called(scripts).Get(0).(*redis.BoolSliceCmd)
+}
+
+func (m *clientMock) ScriptLoad(script string) *redis.StringCmd {
+       return m.Called(script).Get(0).(*redis.StringCmd)
+}
+
 func setSubscribeNotifications() (*pubSubMock, sdlgoredis.SubscribeFn) {
        mock := new(pubSubMock)
        return mock, func(client sdlgoredis.RedisClient, channels ...string) sdlgoredis.Subscriber {
@@ -116,6 +145,11 @@ func setSubscribeNotifications() (*pubSubMock, sdlgoredis.SubscribeFn) {
        }
 }
 
+func (m *MockOS) Getenv(key string, defValue string) string {
+       a := m.Called(key, defValue)
+       return a.String(0)
+}
+
 func setup(commandsExists bool) (*pubSubMock, *clientMock, *sdlgoredis.DB) {
        mock := new(clientMock)
        pubSubMock, subscribeNotifications := setSubscribeNotifications()
@@ -128,12 +162,12 @@ func setup(commandsExists bool) (*pubSubMock, *clientMock, *sdlgoredis.DB) {
 
        if commandsExists {
                cmdResult = map[string]*redis.CommandInfo{
-                       "setie": &dummyCommandInfo,
-                       "delie": &dummyCommandInfo,
+                       "setie":    &dummyCommandInfo,
+                       "delie":    &dummyCommandInfo,
                        "setiepub": &dummyCommandInfo,
                        "setnxpub": &dummyCommandInfo,
                        "msetmpub": &dummyCommandInfo,
-                       "delmpub": &dummyCommandInfo,
+                       "delmpub":  &dummyCommandInfo,
                }
        } else {
                cmdResult = map[string]*redis.CommandInfo{
@@ -146,10 +180,58 @@ func setup(commandsExists bool) (*pubSubMock, *clientMock, *sdlgoredis.DB) {
        return pubSubMock, mock, db
 }
 
+func setupEnv(host, port, msname, sntport, clsaddrlist string) ([]*clientMock, []*sdlgoredis.DB) {
+       var clmocks []*clientMock
+
+       dummyCommandInfo := redis.CommandInfo{
+               Name: "dummy",
+       }
+       cmdResult := make(map[string]*redis.CommandInfo, 0)
+
+       cmdResult = map[string]*redis.CommandInfo{
+               "dummy": &dummyCommandInfo,
+       }
+
+       osmock := new(MockOS)
+       osmock.On("Getenv", "DBAAS_SERVICE_HOST", "localhost").Return(host)
+       osmock.On("Getenv", "DBAAS_SERVICE_PORT", "6379").Return(port)
+       osmock.On("Getenv", "DBAAS_MASTER_NAME", "").Return(msname)
+       osmock.On("Getenv", "DBAAS_SERVICE_SENTINEL_PORT", "").Return(sntport)
+       osmock.On("Getenv", "DBAAS_CLUSTER_ADDR_LIST", "").Return(clsaddrlist)
+
+       clients := sdlgoredis.ReadConfigAndCreateDbClients(
+               osmock,
+               func(addr, port, clusterName string, isHa bool) sdlgoredis.RedisClient {
+                       clm := new(clientMock)
+                       clm.On("Command").Return(redis.NewCommandsInfoCmdResult(cmdResult, nil))
+                       clmocks = append(clmocks, clm)
+                       return clm
+               },
+       )
+
+       return clmocks, clients
+}
+
+func TestCloseDbSuccessfully(t *testing.T) {
+       _, r, db := setup(true)
+       r.On("Close").Return(nil)
+       err := db.CloseDB()
+       assert.Nil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestCloseDbFailure(t *testing.T) {
+       _, r, db := setup(true)
+       r.On("Close").Return(errors.New("Some error"))
+       err := db.CloseDB()
+       assert.NotNil(t, err)
+       r.AssertExpectations(t)
+}
+
 func TestMSetSuccessfully(t *testing.T) {
        _, r, db := setup(true)
        expectedKeysAndValues := []interface{}{"key1", "value1", "key2", 2}
-       r.On("MSet",expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       r.On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
        err := db.MSet("key1", "value1", "key2", 2)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -158,7 +240,7 @@ func TestMSetSuccessfully(t *testing.T) {
 func TestMSetFailure(t *testing.T) {
        _, r, db := setup(true)
        expectedKeysAndValues := []interface{}{"key1", "value1", "key2", 2}
-       r.On("MSet",expectedKeysAndValues).Return(redis.NewStatusResult("OK", errors.New("Some error")))
+       r.On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", errors.New("Some error")))
        err := db.MSet("key1", "value1", "key2", 2)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -167,30 +249,30 @@ func TestMSetFailure(t *testing.T) {
 func TestMSetMPubSuccessfully(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"MSETMPUB", 2, 2, "key1", "val1", "key2", "val2",
-                                                                        "chan1", "event1", "chan2", "event2"}
+               "chan1", "event1", "chan2", "event2"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("", nil))
        assert.Nil(t, db.MSetMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                         "key1", "val1", "key2", "val2"))
+               "key1", "val1", "key2", "val2"))
        r.AssertExpectations(t)
 }
 
 func TestMsetMPubFailure(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"MSETMPUB", 2, 2, "key1", "val1", "key2", "val2",
-                                                                        "chan1", "event1", "chan2", "event2"}
+               "chan1", "event1", "chan2", "event2"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("", errors.New("Some error")))
        assert.NotNil(t, db.MSetMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                            "key1", "val1", "key2", "val2"))
+               "key1", "val1", "key2", "val2"))
        r.AssertExpectations(t)
 }
 
 func TestMSetMPubCommandMissing(t *testing.T) {
        _, r, db := setup(false)
        expectedMessage := []interface{}{"MSETMPUB", 2, 2, "key1", "val1", "key2", "val2",
-                                                                        "chan1", "event1", "chan2", "event2"}
+               "chan1", "event1", "chan2", "event2"}
        r.AssertNotCalled(t, "Do", expectedMessage)
        assert.NotNil(t, db.MSetMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                                "key1", "val1", "key2", "val2"))
+               "key1", "val1", "key2", "val2"))
        r.AssertExpectations(t)
 
 }
@@ -211,7 +293,7 @@ func TestMGetFailure(t *testing.T) {
        expectedKeys := []string{"key1", "key2", "key3"}
        expectedResult := []interface{}{nil}
        r.On("MGet", expectedKeys).Return(redis.NewSliceResult(expectedResult,
-                                                                                                                  errors.New("Some error")))
+               errors.New("Some error")))
        result, err := db.MGet([]string{"key1", "key2", "key3"})
        assert.Equal(t, result, expectedResult)
        assert.NotNil(t, err)
@@ -221,30 +303,30 @@ func TestMGetFailure(t *testing.T) {
 func TestDelMPubSuccessfully(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"DELMPUB", 2, 2, "key1", "key2", "chan1", "event1",
-                                                                        "chan2", "event2"}
+               "chan2", "event2"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("", nil))
        assert.Nil(t, db.DelMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                        []string{"key1", "key2"}))
+               []string{"key1", "key2"}))
        r.AssertExpectations(t)
 }
 
 func TestDelMPubFailure(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"DELMPUB", 2, 2, "key1", "key2", "chan1", "event1",
-                                                                        "chan2", "event2"}
+               "chan2", "event2"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("", errors.New("Some error")))
        assert.NotNil(t, db.DelMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                               []string{"key1", "key2"}))
+               []string{"key1", "key2"}))
        r.AssertExpectations(t)
 }
 
 func TestDelMPubCommandMissing(t *testing.T) {
        _, r, db := setup(false)
        expectedMessage := []interface{}{"DELMPUB", 2, 2, "key1", "key2", "chan1", "event1",
-                                                                        "chan2", "event2"}
+               "chan2", "event2"}
        r.AssertNotCalled(t, "Do", expectedMessage)
        assert.NotNil(t, db.DelMPub([]string{"chan1", "event1", "chan2", "event2"},
-                                                               []string{"key1", "key2"}))
+               []string{"key1", "key2"}))
        r.AssertExpectations(t)
 }
 
@@ -280,7 +362,7 @@ func TestKeysFailure(t *testing.T) {
        expectedPattern := "pattern*"
        expectedResult := []string{}
        r.On("Keys", expectedPattern).Return(redis.NewStringSliceResult(expectedResult,
-                                                                                                                                   errors.New("Some error")))
+               errors.New("Some error")))
        _, err := db.Keys("pattern*")
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -328,9 +410,9 @@ func TestSetIECommandMissing(t *testing.T) {
 
 func TestSetIEPubKeyExists(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETIEPUB", "key", "newdata", "olddata", "channel", "message"}
+       expectedMessage := []interface{}{"SETIEMPUB", "key", "newdata", "olddata", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("OK", nil))
-       result, err := db.SetIEPub("channel", "message", "key", "olddata", "newdata")
+       result, err := db.SetIEPub([]string{"channel", "message"}, "key", "olddata", "newdata")
        assert.True(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -338,9 +420,9 @@ func TestSetIEPubKeyExists(t *testing.T) {
 
 func TestSetIEPubKeyDoesntExists(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETIEPUB", "key", "newdata", "olddata", "channel", "message"}
+       expectedMessage := []interface{}{"SETIEMPUB", "key", "newdata", "olddata", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(nil, nil))
-       result, err := db.SetIEPub("channel", "message", "key", "olddata", "newdata")
+       result, err := db.SetIEPub([]string{"channel", "message"}, "key", "olddata", "newdata")
        assert.False(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -348,9 +430,9 @@ func TestSetIEPubKeyDoesntExists(t *testing.T) {
 
 func TestSetIEPubFailure(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETIEPUB", "key", "newdata", "olddata", "channel", "message"}
+       expectedMessage := []interface{}{"SETIEMPUB", "key", "newdata", "olddata", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(nil, errors.New("Some error")))
-       result, err := db.SetIEPub("channel", "message", "key", "olddata", "newdata")
+       result, err := db.SetIEPub([]string{"channel", "message"}, "key", "olddata", "newdata")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -358,9 +440,9 @@ func TestSetIEPubFailure(t *testing.T) {
 
 func TestSetIEPubCommandMissing(t *testing.T) {
        _, r, db := setup(false)
-       expectedMessage := []interface{}{"SETIEPUB", "key", "newdata", "olddata", "channel", "message"}
+       expectedMessage := []interface{}{"SETIEMPUB", "key", "newdata", "olddata", "channel", "message"}
        r.AssertNotCalled(t, "Do", expectedMessage)
-       result, err := db.SetIEPub("channel", "message", "key", "olddata", "newdata")
+       result, err := db.SetIEPub([]string{"channel", "message"}, "key", "olddata", "newdata")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -368,9 +450,9 @@ func TestSetIEPubCommandMissing(t *testing.T) {
 
 func TestSetNXPubKeyDoesntExist(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETNXPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"SETNXMPUB", "key", "data", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult("OK", nil))
-       result, err := db.SetNXPub("channel", "message", "key", "data")
+       result, err := db.SetNXPub([]string{"channel", "message"}, "key", "data")
        assert.True(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -378,9 +460,9 @@ func TestSetNXPubKeyDoesntExist(t *testing.T) {
 
 func TestSetNXPubKeyExists(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETNXPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"SETNXMPUB", "key", "data", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(nil, nil))
-       result, err := db.SetNXPub("channel", "message", "key", "data")
+       result, err := db.SetNXPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -388,9 +470,9 @@ func TestSetNXPubKeyExists(t *testing.T) {
 
 func TestSetNXPubFailure(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"SETNXPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"SETNXMPUB", "key", "data", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(nil, errors.New("Some error")))
-       result, err := db.SetNXPub("channel", "message", "key", "data")
+       result, err := db.SetNXPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -398,9 +480,9 @@ func TestSetNXPubFailure(t *testing.T) {
 
 func TestSetNXPubCommandMissing(t *testing.T) {
        _, r, db := setup(false)
-       expectedMessage := []interface{}{"SETNXPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"SETNXMPUB", "key", "data", "channel", "message"}
        r.AssertNotCalled(t, "Do", expectedMessage)
-       result, err := db.SetNXPub("channel", "message", "key", "data")
+       result, err := db.SetNXPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -411,7 +493,7 @@ func TestSetNXSuccessfully(t *testing.T) {
        expectedKey := "key"
        expectedData := "data"
        r.On("SetNX", expectedKey, expectedData, time.Duration(0)).Return(redis.NewBoolResult(true, nil))
-       result, err := db.SetNX("key", "data")
+       result, err := db.SetNX("key", "data", 0)
        assert.True(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -422,7 +504,7 @@ func TestSetNXUnsuccessfully(t *testing.T) {
        expectedKey := "key"
        expectedData := "data"
        r.On("SetNX", expectedKey, expectedData, time.Duration(0)).Return(redis.NewBoolResult(false, nil))
-       result, err := db.SetNX("key", "data")
+       result, err := db.SetNX("key", "data", 0)
        assert.False(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -433,8 +515,8 @@ func TestSetNXFailure(t *testing.T) {
        expectedKey := "key"
        expectedData := "data"
        r.On("SetNX", expectedKey, expectedData, time.Duration(0)).
-               Return(redis.NewBoolResult(false,errors.New("Some error")))
-       result, err := db.SetNX("key", "data")
+               Return(redis.NewBoolResult(false, errors.New("Some error")))
+       result, err := db.SetNX("key", "data", 0)
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -442,9 +524,9 @@ func TestSetNXFailure(t *testing.T) {
 
 func TestDelIEPubKeyDoesntExist(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"DELIEPUB", "key", "data", "channel", "message"}
-       r.On("Do", expectedMessage).Return(redis.NewCmdResult(0, nil))
-       result, err := db.DelIEPub("channel", "message", "key", "data")
+       expectedMessage := []interface{}{"DELIEMPUB", "key", "data", "channel", "message"}
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(0), nil))
+       result, err := db.DelIEPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -452,9 +534,19 @@ func TestDelIEPubKeyDoesntExist(t *testing.T) {
 
 func TestDelIEPubKeyExists(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"DELIEPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"DELIEMPUB", "key", "data", "channel", "message"}
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(1), nil))
+       result, err := db.DelIEPub([]string{"channel", "message"}, "key", "data")
+       assert.True(t, result)
+       assert.Nil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestDelIEPubKeyExistsIntTypeRedisValue(t *testing.T) {
+       _, r, db := setup(true)
+       expectedMessage := []interface{}{"DELIEMPUB", "key", "data", "channel", "message"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(1, nil))
-       result, err := db.DelIEPub("channel", "message", "key", "data")
+       result, err := db.DelIEPub([]string{"channel", "message"}, "key", "data")
        assert.True(t, result)
        assert.Nil(t, err)
        r.AssertExpectations(t)
@@ -462,9 +554,9 @@ func TestDelIEPubKeyExists(t *testing.T) {
 
 func TestDelIEPubFailure(t *testing.T) {
        _, r, db := setup(true)
-       expectedMessage := []interface{}{"DELIEPUB", "key", "data", "channel", "message"}
-       r.On("Do", expectedMessage).Return(redis.NewCmdResult(0, errors.New("Some error")))
-       result, err := db.DelIEPub("channel", "message", "key", "data")
+       expectedMessage := []interface{}{"DELIEMPUB", "key", "data", "channel", "message"}
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(0), errors.New("Some error")))
+       result, err := db.DelIEPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -472,9 +564,9 @@ func TestDelIEPubFailure(t *testing.T) {
 
 func TestDelIEPubCommandMissing(t *testing.T) {
        _, r, db := setup(false)
-       expectedMessage := []interface{}{"DELIEPUB", "key", "data", "channel", "message"}
+       expectedMessage := []interface{}{"DELIEMPUB", "key", "data", "channel", "message"}
        r.AssertNotCalled(t, "Do", expectedMessage)
-       result, err := db.DelIEPub("channel", "message", "key", "data")
+       result, err := db.DelIEPub([]string{"channel", "message"}, "key", "data")
        assert.False(t, result)
        assert.NotNil(t, err)
        r.AssertExpectations(t)
@@ -483,7 +575,7 @@ func TestDelIEPubCommandMissing(t *testing.T) {
 func TestDelIEKeyDoesntExist(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"DELIE", "key", "data"}
-       r.On("Do", expectedMessage).Return(redis.NewCmdResult(0, nil))
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(0), nil))
        result, err := db.DelIE("key", "data")
        assert.False(t, result)
        assert.Nil(t, err)
@@ -491,6 +583,16 @@ func TestDelIEKeyDoesntExist(t *testing.T) {
 }
 
 func TestDelIEKeyExists(t *testing.T) {
+       _, r, db := setup(true)
+       expectedMessage := []interface{}{"DELIE", "key", "data"}
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(1), nil))
+       result, err := db.DelIE("key", "data")
+       assert.True(t, result)
+       assert.Nil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestDelIEKeyExistsIntTypeRedisValue(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"DELIE", "key", "data"}
        r.On("Do", expectedMessage).Return(redis.NewCmdResult(1, nil))
@@ -503,7 +605,7 @@ func TestDelIEKeyExists(t *testing.T) {
 func TestDelIEFailure(t *testing.T) {
        _, r, db := setup(true)
        expectedMessage := []interface{}{"DELIE", "key", "data"}
-       r.On("Do", expectedMessage).Return(redis.NewCmdResult(0, errors.New("Some error")))
+       r.On("Do", expectedMessage).Return(redis.NewCmdResult(int64(0), errors.New("Some error")))
        result, err := db.DelIE("key", "data")
        assert.False(t, result)
        assert.NotNil(t, err)
@@ -572,7 +674,7 @@ func TestSMembersFailure(t *testing.T) {
        expectedKey := "key"
        expectedResult := []string{"member1", "member2"}
        r.On("SMembers", expectedKey).Return(redis.NewStringSliceResult(expectedResult,
-                                                                                                                                       errors.New("Some error")))
+               errors.New("Some error")))
        result, err := db.SMembers("key")
        assert.Equal(t, result, expectedResult)
        assert.NotNil(t, err)
@@ -646,10 +748,10 @@ func TestSubscribeChannelDBSubscribeRXUnsubscribe(t *testing.T) {
        ps.On("Close").Return(nil)
        count := 0
        receivedChannel := ""
-       db.SubscribeChannelDB(func(channel string, payload ...string){
+       db.SubscribeChannelDB(func(channel string, payload ...string) {
                count++
                receivedChannel = channel
-               },"{prefix}", "---", "{prefix}channel")
+       }, "{prefix}", "---", "{prefix}channel")
        ch <- &msg
        db.UnsubscribeChannelDB("{prefix}channel")
        time.Sleep(1 * time.Second)
@@ -679,17 +781,18 @@ func TestSubscribeChannelDBSubscribeTwoUnsubscribeOne(t *testing.T) {
        ps.On("Close").Return(nil)
        count := 0
        receivedChannel1 := ""
-       db.SubscribeChannelDB(func(channel string, payload ...string){
+       db.SubscribeChannelDB(func(channel string, payload ...string) {
                count++
                receivedChannel1 = channel
-               },"{prefix}", "---", "{prefix}channel1")
+       }, "{prefix}", "---", "{prefix}channel1")
        ch <- &msg1
        receivedChannel2 := ""
-       db.SubscribeChannelDB(func(channel string, payload ...string){
+       db.SubscribeChannelDB(func(channel string, payload ...string) {
                count++
                receivedChannel2 = channel
-               },"{prefix}", "---", "{prefix}channel2")
+       }, "{prefix}", "---", "{prefix}channel2")
 
+       time.Sleep(1 * time.Second)
        db.UnsubscribeChannelDB("{prefix}channel1")
        ch <- &msg2
        db.UnsubscribeChannelDB("{prefix}channel2")
@@ -699,4 +802,178 @@ func TestSubscribeChannelDBSubscribeTwoUnsubscribeOne(t *testing.T) {
        assert.Equal(t, "channel2", receivedChannel2)
        r.AssertExpectations(t)
        ps.AssertExpectations(t)
-}
\ No newline at end of file
+}
+
+func TestSubscribeChannelReDBSubscribeAfterUnsubscribe(t *testing.T) {
+       ps, r, db := setup(true)
+       ch := make(chan *redis.Message)
+       msg := redis.Message{
+               Channel: "{prefix}channel",
+               Pattern: "pattern",
+               Payload: "event",
+       }
+       ps.On("Channel").Return(ch)
+       ps.On("Unsubscribe").Return(nil)
+       ps.On("Close").Return(nil)
+       count := 0
+       receivedChannel := ""
+
+       db.SubscribeChannelDB(func(channel string, payload ...string) {
+               count++
+               receivedChannel = channel
+       }, "{prefix}", "---", "{prefix}channel")
+       ch <- &msg
+       db.UnsubscribeChannelDB("{prefix}channel")
+       time.Sleep(1 * time.Second)
+
+       db.SubscribeChannelDB(func(channel string, payload ...string) {
+               count++
+               receivedChannel = channel
+       }, "{prefix}", "---", "{prefix}channel")
+       ch <- &msg
+       db.UnsubscribeChannelDB("{prefix}channel")
+
+       time.Sleep(1 * time.Second)
+       assert.Equal(t, 2, count)
+       assert.Equal(t, "channel", receivedChannel)
+       r.AssertExpectations(t)
+       ps.AssertExpectations(t)
+}
+
+func TestPTTLSuccessfully(t *testing.T) {
+       _, r, db := setup(true)
+       expectedKey := "key"
+       expectedResult := time.Duration(1)
+       r.On("PTTL", expectedKey).Return(redis.NewDurationResult(expectedResult,
+               nil))
+       result, err := db.PTTL("key")
+       assert.Equal(t, result, expectedResult)
+       assert.Nil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestPTTLFailure(t *testing.T) {
+       _, r, db := setup(true)
+       expectedKey := "key"
+       expectedResult := time.Duration(1)
+       r.On("PTTL", expectedKey).Return(redis.NewDurationResult(expectedResult,
+               errors.New("Some error")))
+       result, err := db.PTTL("key")
+       assert.Equal(t, result, expectedResult)
+       assert.NotNil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestPExpireIESuccessfully(t *testing.T) {
+       _, r, db := setup(true)
+       expectedKey := "key"
+       expectedData := "data"
+       expectedDuration := strconv.FormatInt(int64(10000), 10)
+
+       r.On("EvalSha", mock.Anything, []string{expectedKey}, []interface{}{expectedData, expectedDuration}).
+               Return(redis.NewCmdResult(int64(1), nil))
+
+       err := db.PExpireIE("key", "data", 10*time.Second)
+       assert.Nil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestPExpireIEFailure(t *testing.T) {
+       _, r, db := setup(true)
+       expectedKey := "key"
+       expectedData := "data"
+       expectedDuration := strconv.FormatInt(int64(10000), 10)
+
+       r.On("EvalSha", mock.Anything, []string{expectedKey}, []interface{}{expectedData, expectedDuration}).
+               Return(redis.NewCmdResult(int64(1), errors.New("Some error")))
+
+       err := db.PExpireIE("key", "data", 10*time.Second)
+       assert.NotNil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestPExpireIELockNotHeld(t *testing.T) {
+       _, r, db := setup(true)
+       expectedKey := "key"
+       expectedData := "data"
+       expectedDuration := strconv.FormatInt(int64(10000), 10)
+
+       r.On("EvalSha", mock.Anything, []string{expectedKey}, []interface{}{expectedData, expectedDuration}).
+               Return(redis.NewCmdResult(int64(0), nil))
+
+       err := db.PExpireIE("key", "data", 10*time.Second)
+       assert.NotNil(t, err)
+       r.AssertExpectations(t)
+}
+
+func TestClientStandaloneRedisLegacyEnv(t *testing.T) {
+       rcls, dbs := setupEnv(
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt", "6376", "", "", "",
+       )
+       assert.Equal(t, 1, len(rcls))
+       assert.Equal(t, 1, len(dbs))
+
+       expectedKeysAndValues := []interface{}{"key1", "value1"}
+       rcls[0].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err := dbs[0].MSet("key1", "value1")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+}
+
+func TestClientSentinelRedisLegacyEnv(t *testing.T) {
+       rcls, dbs := setupEnv(
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt", "6376", "dbaasmaster", "26376", "",
+       )
+       assert.Equal(t, 1, len(rcls))
+       assert.Equal(t, 1, len(dbs))
+
+       expectedKeysAndValues := []interface{}{"key1", "value1"}
+       rcls[0].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err := dbs[0].MSet("key1", "value1")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+}
+
+func TestClientTwoStandaloneRedisEnvs(t *testing.T) {
+       rcls, dbs := setupEnv(
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt", "6376", "", "",
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt",
+       )
+       assert.Equal(t, 2, len(rcls))
+       assert.Equal(t, 2, len(dbs))
+
+       expectedKeysAndValues := []interface{}{"key1", "value1"}
+       rcls[0].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err := dbs[0].MSet("key1", "value1")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+
+       expectedKeysAndValues = []interface{}{"key2", "value2"}
+       rcls[1].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err = dbs[1].MSet("key2", "value2")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+       rcls[1].AssertExpectations(t)
+}
+
+func TestClientTwoSentinelRedisEnvs(t *testing.T) {
+       rcls, dbs := setupEnv(
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt", "6376", "dbaasmaster", "26376",
+               "service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt",
+       )
+       assert.Equal(t, 2, len(rcls))
+       assert.Equal(t, 2, len(dbs))
+
+       expectedKeysAndValues := []interface{}{"key1", "value1"}
+       rcls[0].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err := dbs[0].MSet("key1", "value1")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+
+       expectedKeysAndValues = []interface{}{"key2", "value2"}
+       rcls[1].On("MSet", expectedKeysAndValues).Return(redis.NewStatusResult("OK", nil))
+       err = dbs[1].MSet("key2", "value2")
+       assert.Nil(t, err)
+       rcls[0].AssertExpectations(t)
+       rcls[1].AssertExpectations(t)
+}