X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=internal%2Fsdlgoredis%2Fsdlgoredis_test.go;h=c95b8afbdc9790bd28cee286e3dd7b1dbf37770b;hb=refs%2Ftags%2Fv0.9.6;hp=7ed7d70ce2d4c3e982c8a30f679882d80158238e;hpb=2b62c749ada363a2f20fd8ae34f1a66ad12d307d;p=ric-plt%2Fsdlgo.git diff --git a/internal/sdlgoredis/sdlgoredis_test.go b/internal/sdlgoredis/sdlgoredis_test.go index 7ed7d70..c95b8af 100644 --- a/internal/sdlgoredis/sdlgoredis_test.go +++ b/internal/sdlgoredis/sdlgoredis_test.go @@ -23,9 +23,10 @@ package sdlgoredis_test import ( + "context" "errors" "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis" - "github.com/go-redis/redis/v7" + "github.com/go-redis/redis/v8" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "strconv" @@ -45,15 +46,15 @@ type MockOS struct { mock.Mock } -func (m *pubSubMock) Channel() <-chan *redis.Message { +func (m *pubSubMock) Channel(opts ...redis.ChannelOption) <-chan *redis.Message { return m.Called().Get(0).(chan *redis.Message) } -func (m *pubSubMock) Subscribe(channels ...string) error { +func (m *pubSubMock) Subscribe(ctx context.Context, channels ...string) error { return m.Called().Error(0) } -func (m *pubSubMock) Unsubscribe(channels ...string) error { +func (m *pubSubMock) Unsubscribe(ctx context.Context, channels ...string) error { return m.Called().Error(0) } @@ -61,7 +62,7 @@ func (m *pubSubMock) Close() error { return m.Called().Error(0) } -func (m *clientMock) Command() *redis.CommandsInfoCmd { +func (m *clientMock) Command(ctx context.Context) *redis.CommandsInfoCmd { return m.Called().Get(0).(*redis.CommandsInfoCmd) } @@ -69,75 +70,75 @@ func (m *clientMock) Close() error { return m.Called().Error(0) } -func (m *clientMock) Subscribe(channels ...string) *redis.PubSub { +func (m *clientMock) Subscribe(ctx context.Context, channels ...string) *redis.PubSub { return m.Called(channels).Get(0).(*redis.PubSub) } -func (m *clientMock) MSet(pairs ...interface{}) *redis.StatusCmd { +func (m *clientMock) MSet(ctx context.Context, pairs ...interface{}) *redis.StatusCmd { return m.Called(pairs).Get(0).(*redis.StatusCmd) } -func (m *clientMock) Do(args ...interface{}) *redis.Cmd { +func (m *clientMock) Do(ctx context.Context, args ...interface{}) *redis.Cmd { return m.Called(args).Get(0).(*redis.Cmd) } -func (m *clientMock) MGet(keys ...string) *redis.SliceCmd { +func (m *clientMock) MGet(ctx context.Context, keys ...string) *redis.SliceCmd { return m.Called(keys).Get(0).(*redis.SliceCmd) } -func (m *clientMock) Del(keys ...string) *redis.IntCmd { +func (m *clientMock) Del(ctx context.Context, keys ...string) *redis.IntCmd { return m.Called(keys).Get(0).(*redis.IntCmd) } -func (m *clientMock) Keys(pattern string) *redis.StringSliceCmd { +func (m *clientMock) Keys(ctx context.Context, pattern string) *redis.StringSliceCmd { return m.Called(pattern).Get(0).(*redis.StringSliceCmd) } -func (m *clientMock) SetNX(key string, value interface{}, expiration time.Duration) *redis.BoolCmd { +func (m *clientMock) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd { return m.Called(key, value, expiration).Get(0).(*redis.BoolCmd) } -func (m *clientMock) SAdd(key string, members ...interface{}) *redis.IntCmd { +func (m *clientMock) SAdd(ctx context.Context, key string, members ...interface{}) *redis.IntCmd { return m.Called(key, members).Get(0).(*redis.IntCmd) } -func (m *clientMock) SRem(key string, members ...interface{}) *redis.IntCmd { +func (m *clientMock) SRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd { return m.Called(key, members).Get(0).(*redis.IntCmd) } -func (m *clientMock) SMembers(key string) *redis.StringSliceCmd { +func (m *clientMock) SMembers(ctx context.Context, key string) *redis.StringSliceCmd { return m.Called(key).Get(0).(*redis.StringSliceCmd) } -func (m *clientMock) SIsMember(key string, member interface{}) *redis.BoolCmd { +func (m *clientMock) SIsMember(ctx context.Context, key string, member interface{}) *redis.BoolCmd { return m.Called(key, member).Get(0).(*redis.BoolCmd) } -func (m *clientMock) SCard(key string) *redis.IntCmd { +func (m *clientMock) SCard(ctx context.Context, key string) *redis.IntCmd { return m.Called(key).Get(0).(*redis.IntCmd) } -func (m *clientMock) PTTL(key string) *redis.DurationCmd { +func (m *clientMock) PTTL(ctx context.Context, key string) *redis.DurationCmd { return m.Called(key).Get(0).(*redis.DurationCmd) } -func (m *clientMock) Eval(script string, keys []string, args ...interface{}) *redis.Cmd { +func (m *clientMock) Eval(ctx context.Context, 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 { +func (m *clientMock) EvalSha(ctx context.Context, 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 { +func (m *clientMock) ScriptExists(ctx context.Context, scripts ...string) *redis.BoolSliceCmd { return m.Called(scripts).Get(0).(*redis.BoolSliceCmd) } -func (m *clientMock) ScriptLoad(script string) *redis.StringCmd { +func (m *clientMock) ScriptLoad(ctx context.Context, script string) *redis.StringCmd { return m.Called(script).Get(0).(*redis.StringCmd) } -func (m *clientMock) Info(section ...string) *redis.StringCmd { +func (m *clientMock) Info(ctx context.Context, section ...string) *redis.StringCmd { return m.Called(section).Get(0).(*redis.StringCmd) } @@ -145,24 +146,24 @@ type MockRedisSentinel struct { mock.Mock } -func (m *MockRedisSentinel) Master(name string) *redis.StringStringMapCmd { +func (m *MockRedisSentinel) Master(ctx context.Context, name string) *redis.StringStringMapCmd { a := m.Called(name) return a.Get(0).(*redis.StringStringMapCmd) } -func (m *MockRedisSentinel) Slaves(name string) *redis.SliceCmd { +func (m *MockRedisSentinel) Slaves(ctx context.Context, name string) *redis.SliceCmd { a := m.Called(name) return a.Get(0).(*redis.SliceCmd) } -func (m *MockRedisSentinel) Sentinels(name string) *redis.SliceCmd { +func (m *MockRedisSentinel) Sentinels(ctx context.Context, name string) *redis.SliceCmd { a := m.Called(name) return a.Get(0).(*redis.SliceCmd) } func setSubscribeNotifications() (*pubSubMock, sdlgoredis.SubscribeFn) { mock := new(pubSubMock) - return mock, func(client sdlgoredis.RedisClient, channels ...string) sdlgoredis.Subscriber { + return mock, func(ctx context.Context, client sdlgoredis.RedisClient, channels ...string) sdlgoredis.Subscriber { return mock } } @@ -899,7 +900,7 @@ func TestSubscribeChannelDBSubscribeRXUnsubscribe(t *testing.T) { ps, r, db := setupHaEnv(true) ch := make(chan *redis.Message) msg := redis.Message{ - Channel: "{prefix}channel", + Channel: "{prefix},channel", Pattern: "pattern", Payload: "event", } @@ -911,9 +912,9 @@ func TestSubscribeChannelDBSubscribeRXUnsubscribe(t *testing.T) { db.SubscribeChannelDB(func(channel string, payload ...string) { count++ receivedChannel = channel - }, "{prefix}", "---", "{prefix}channel") + }, "{prefix},channel") ch <- &msg - db.UnsubscribeChannelDB("{prefix}channel") + db.UnsubscribeChannelDB("{prefix},channel") time.Sleep(1 * time.Second) assert.Equal(t, 1, count) assert.Equal(t, "channel", receivedChannel) @@ -925,12 +926,12 @@ func TestSubscribeChannelDBSubscribeTwoUnsubscribeOne(t *testing.T) { ps, r, db := setupHaEnv(true) ch := make(chan *redis.Message) msg1 := redis.Message{ - Channel: "{prefix}channel1", + Channel: "{prefix},channel1", Pattern: "pattern", Payload: "event", } msg2 := redis.Message{ - Channel: "{prefix}channel2", + Channel: "{prefix},channel2", Pattern: "pattern", Payload: "event", } @@ -944,18 +945,18 @@ func TestSubscribeChannelDBSubscribeTwoUnsubscribeOne(t *testing.T) { db.SubscribeChannelDB(func(channel string, payload ...string) { count++ receivedChannel1 = channel - }, "{prefix}", "---", "{prefix}channel1") + }, "{prefix},channel1") ch <- &msg1 receivedChannel2 := "" db.SubscribeChannelDB(func(channel string, payload ...string) { count++ receivedChannel2 = channel - }, "{prefix}", "---", "{prefix}channel2") + }, "{prefix},channel2") time.Sleep(1 * time.Second) - db.UnsubscribeChannelDB("{prefix}channel1") + db.UnsubscribeChannelDB("{prefix},channel1") ch <- &msg2 - db.UnsubscribeChannelDB("{prefix}channel2") + db.UnsubscribeChannelDB("{prefix},channel2") time.Sleep(1 * time.Second) assert.Equal(t, 2, count) assert.Equal(t, "channel1", receivedChannel1) @@ -964,11 +965,54 @@ func TestSubscribeChannelDBSubscribeTwoUnsubscribeOne(t *testing.T) { ps.AssertExpectations(t) } +func TestSubscribeChannelDBTwoSubscribesWithUnequalPrefixAndUnsubscribes(t *testing.T) { + ps, r, db := setupHaEnv(true) + ch := make(chan *redis.Message) + msg1 := redis.Message{ + Channel: "{prefix1},channel", + Pattern: "pattern", + Payload: "event", + } + msg2 := redis.Message{ + Channel: "{prefix2},channel", + Pattern: "pattern", + Payload: "event", + } + ps.On("Channel").Return(ch) + ps.On("Subscribe").Return(nil) + ps.On("Unsubscribe").Return(nil) + ps.On("Unsubscribe").Return(nil) + ps.On("Close").Return(nil) + count := 0 + receivedChannel1 := "" + db.SubscribeChannelDB(func(channel string, payload ...string) { + count++ + receivedChannel1 = channel + }, "{prefix1},channel") + ch <- &msg1 + receivedChannel2 := "" + db.SubscribeChannelDB(func(channel string, payload ...string) { + count++ + receivedChannel2 = channel + }, "{prefix2},channel") + + time.Sleep(1 * time.Second) + db.UnsubscribeChannelDB("{prefix1},channel") + ch <- &msg2 + db.UnsubscribeChannelDB("{prefix2},channel") + time.Sleep(1 * time.Second) + assert.Equal(t, 2, count) + assert.Equal(t, "channel", receivedChannel1) + assert.Equal(t, "channel", receivedChannel2) + r.AssertExpectations(t) + ps.AssertExpectations(t) +} + func TestSubscribeChannelReDBSubscribeAfterUnsubscribe(t *testing.T) { ps, r, db := setupHaEnv(true) ch := make(chan *redis.Message) msg := redis.Message{ - Channel: "{prefix}channel", + Channel: "{prefix},channel", Pattern: "pattern", Payload: "event", } @@ -981,17 +1025,17 @@ func TestSubscribeChannelReDBSubscribeAfterUnsubscribe(t *testing.T) { db.SubscribeChannelDB(func(channel string, payload ...string) { count++ receivedChannel = channel - }, "{prefix}", "---", "{prefix}channel") + }, "{prefix},channel") ch <- &msg - db.UnsubscribeChannelDB("{prefix}channel") + db.UnsubscribeChannelDB("{prefix},channel") time.Sleep(1 * time.Second) db.SubscribeChannelDB(func(channel string, payload ...string) { count++ receivedChannel = channel - }, "{prefix}", "---", "{prefix}channel") + }, "{prefix}", "---", "{prefix},channel") ch <- &msg - db.UnsubscribeChannelDB("{prefix}channel") + db.UnsubscribeChannelDB("{prefix},channel") time.Sleep(1 * time.Second) assert.Equal(t, 2, count) @@ -1000,6 +1044,32 @@ func TestSubscribeChannelReDBSubscribeAfterUnsubscribe(t *testing.T) { ps.AssertExpectations(t) } +func TestSubscribeChannelDBSubscribeReceivedEventIgnoredIfChannelNameIsUnknown(t *testing.T) { + ps, r, db := setupHaEnv(true) + ch := make(chan *redis.Message) + msg := redis.Message{ + Channel: "missingNsPrefixchannel", + 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},channel") + ch <- &msg + db.UnsubscribeChannelDB("{prefix},channel") + time.Sleep(1 * time.Second) + assert.Equal(t, 0, count) + assert.Equal(t, "", receivedChannel) + r.AssertExpectations(t) + ps.AssertExpectations(t) +} + func TestPTTLSuccessfully(t *testing.T) { _, r, db := setupHaEnv(true) expectedKey := "key" @@ -1454,6 +1524,34 @@ func TestStateWithPrimaryAndTwoReplicaRedisFailureWhenIntConversionFails(t *test r.AssertExpectations(t) } +// Test case to test ignoring of a sentinel entry with zero port. Implementation has been +// done because we miss the fix for the Redis Bug #9240. +func TestStateWithPrimaryAndTwoReplicaFirstSentinelStateIgnoredBecauseZeroPortBugRedisSuccessfully(t *testing.T) { + _, r, s, db := setupHaEnvWithSentinels(true, "3") + + redisPrimaryState := newMockRedisMasterCallResp("master", "10.20.30.30", "6379", "master") + redisReplicasState := newMockRedisSlavesCall() + redisReplicasState.add("slave", "10.20.30.40", "6379", "up", "slave") + redisReplicasState.add("slave", "10.20.30.50", "30000", "up", "slave") + redisSentinelsState := newMockRedisSentinelsCall() + redisSentinelsState.add("10.20.30.40", "0", "s_down,sentinel,disconnected") + redisSentinelsState.add("10.20.30.50", "26379", "sentinel") + + expState := newExpDbState(3, nil) + expState.addPrimary("master", "10.20.30.30", "6379", "master", nil) + expState.addReplica("slave", "10.20.30.40", "6379", "up", "slave", nil) + expState.addReplica("slave", "10.20.30.50", "30000", "up", "slave", nil) + expState.addSentinel("10.20.30.50", "26379", "sentinel", nil) + + s[0].On("Master", "dbaasmaster").Return(redis.NewStringStringMapResult(redisPrimaryState, nil)) + s[0].On("Slaves", "dbaasmaster").Return(redis.NewSliceResult(redisReplicasState.resp, nil)) + s[0].On("Sentinels", "dbaasmaster").Return(redis.NewSliceResult(redisSentinelsState.resp, nil)) + ret, err := db.State() + assert.Nil(t, err) + assert.Equal(t, expState.s, *ret) + r.AssertExpectations(t) +} + func TestStateWithSinglePrimaryRedisSuccessfully(t *testing.T) { _, r, db := setupSingleEnv(true, "1") redisInfo := "# Replication\r\n" +