diff options
author | Valery Piashchynski <[email protected]> | 2021-04-22 22:53:12 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-04-22 22:53:12 +0300 |
commit | 06ee748c1ca23bf489ae707041f0e8f3e67fbaf7 (patch) | |
tree | 4249c893d194c779fd7510cef2de7e71b888735f /tests | |
parent | 91c1fa2e2693cb662425c1ba7cca2325a8458995 (diff) |
- Add redis driver
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/plugins/kv/configs/.rr-redis.yaml | 12 | ||||
-rw-r--r-- | tests/plugins/kv/storage_plugin_test.go | 202 |
2 files changed, 213 insertions, 1 deletions
diff --git a/tests/plugins/kv/configs/.rr-redis.yaml b/tests/plugins/kv/configs/.rr-redis.yaml new file mode 100644 index 00000000..0a7396ca --- /dev/null +++ b/tests/plugins/kv/configs/.rr-redis.yaml @@ -0,0 +1,12 @@ +rpc: + listen: tcp://127.0.0.1:6001 + +logs: + mode: development + level: error + +kv: + redis-rr: + driver: redis + addrs: + - 'localhost:6379' diff --git a/tests/plugins/kv/storage_plugin_test.go b/tests/plugins/kv/storage_plugin_test.go index 0b42e19d..0c322441 100644 --- a/tests/plugins/kv/storage_plugin_test.go +++ b/tests/plugins/kv/storage_plugin_test.go @@ -18,9 +18,9 @@ import ( "github.com/spiral/roadrunner/v2/plugins/kv/drivers/boltdb" "github.com/spiral/roadrunner/v2/plugins/kv/drivers/memcached" "github.com/spiral/roadrunner/v2/plugins/kv/drivers/memory" + "github.com/spiral/roadrunner/v2/plugins/kv/drivers/redis" "github.com/spiral/roadrunner/v2/plugins/kv/payload/generated" "github.com/spiral/roadrunner/v2/plugins/logger" - "github.com/spiral/roadrunner/v2/plugins/redis" rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" "github.com/stretchr/testify/assert" ) @@ -789,3 +789,203 @@ func testRPCMethodsInMemory(t *testing.T) { assert.NoError(t, err) assert.Len(t, ret, 0) } + +func TestRedis(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-redis.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &kv.Plugin{}, + &redis.Plugin{}, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second * 1) + t.Run("testRedisRPCMethods", testRPCMethodsRedis) + stopCh <- struct{}{} + wg.Wait() +} + +func testRPCMethodsRedis(t *testing.T) { + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) + + // add 5 second ttl + tt := time.Now().Add(time.Second * 5).Format(time.RFC3339) + keys := makePayload(flatbuffers.NewBuilder(100), "redis-rr", []kv.Item{ + { + Key: "a", + }, + { + Key: "b", + }, + { + Key: "c", + }, + }) + data := makePayload(flatbuffers.NewBuilder(100), "redis-rr", []kv.Item{ + { + Key: "a", + Value: "aa", + }, + { + Key: "b", + Value: "bb", + }, + { + Key: "c", + Value: "cc", + TTL: tt, + }, + { + Key: "d", + Value: "dd", + }, + { + Key: "e", + Value: "ee", + }, + }) + + var setRes bool + + // Register 3 keys with values + err = client.Call("kv.Set", data, &setRes) + assert.NoError(t, err) + assert.True(t, setRes) + + ret := make(map[string]bool) + err = client.Call("kv.Has", keys, &ret) + assert.NoError(t, err) + assert.Len(t, ret, 3) // should be 3 + + // key "c" should be deleted + time.Sleep(time.Second * 7) + + ret = make(map[string]bool) + err = client.Call("kv.Has", keys, &ret) + assert.NoError(t, err) + assert.Len(t, ret, 2) // should be 2 + + mGet := make(map[string]interface{}) + err = client.Call("kv.MGet", keys, &mGet) + assert.NoError(t, err) + assert.Len(t, mGet, 2) // c is expired + assert.Equal(t, "aa", mGet["a"].(string)) + assert.Equal(t, "bb", mGet["b"].(string)) + + tt2 := time.Now().Add(time.Second * 10).Format(time.RFC3339) + data2 := makePayload(flatbuffers.NewBuilder(100), "redis-rr", []kv.Item{ + { + Key: "a", + TTL: tt2, + }, + { + Key: "b", + TTL: tt2, + }, + { + Key: "d", + TTL: tt2, + }, + }) + + // MEXPIRE + var mExpRes bool + err = client.Call("kv.MExpire", data2, &mExpRes) + assert.NoError(t, err) + assert.True(t, mExpRes) + + // TTL + keys2 := makePayload(flatbuffers.NewBuilder(100), "redis-rr", []kv.Item{ + { + Key: "a", + }, + { + Key: "b", + }, + { + Key: "d", + }, + }) + ttlRes := make(map[string]interface{}) + err = client.Call("kv.TTL", keys2, &ttlRes) + assert.NoError(t, err) + assert.Len(t, ttlRes, 3) + + // HAS AFTER TTL + time.Sleep(time.Second * 15) + ret = make(map[string]bool) + err = client.Call("kv.Has", keys2, &ret) + assert.NoError(t, err) + assert.Len(t, ret, 0) + + // DELETE + keysDel := makePayload(flatbuffers.NewBuilder(100), "redis-rr", []kv.Item{ + { + Key: "e", + }, + }) + var delRet bool + err = client.Call("kv.Delete", keysDel, &delRet) + assert.NoError(t, err) + assert.True(t, delRet) + + // HAS AFTER DELETE + ret = make(map[string]bool) + err = client.Call("kv.Has", keysDel, &ret) + assert.NoError(t, err) + assert.Len(t, ret, 0) +} |