You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/10/07 12:59:00 UTC
[GitHub] [incubator-kvrocks] vmihailenco commented on a diff in pull request #956: Move TCL test unit/introspection to Go case
vmihailenco commented on code in PR #956:
URL: https://github.com/apache/incubator-kvrocks/pull/956#discussion_r990061030
##########
tests/gocase/unit/introspection/introspection_test.go:
##########
@@ -20,33 +20,230 @@
package introspection
import (
+ "context"
+ "fmt"
+ "strconv"
+ "strings"
"testing"
+ "time"
"github.com/apache/incubator-kvrocks/tests/gocase/util"
+ "github.com/go-redis/redis/v9"
"github.com/stretchr/testify/require"
)
func TestIntrospection(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
defer srv.Close()
- t.Run("PING", func(t *testing.T) {
+ ctx := context.Background()
+ rdb := srv.NewClient()
+ defer func() { require.NoError(t, rdb.Close()) }()
+
+ t.Run("CLIENT LIST", func(t *testing.T) {
+ v := rdb.ClientList(ctx).Val()
+ require.Regexp(t, "id=.* addr=.*:.* fd=.* name=.* age=.* idle=.* flags=N namespace=.* qbuf=.* .*obuf=.* cmd=client.*", v)
+ })
+
+ t.Run("MONITOR can log executed commands", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()
- require.NoError(t, c.WriteArgs("PING"))
- r, err := c.ReadLine()
+ require.NoError(t, c.WriteArgs("MONITOR"))
+ v, err := c.ReadLine()
+ require.NoError(t, err)
+ require.Equal(t, "+OK", v)
+ require.NoError(t, rdb.Set(ctx, "foo", "bar", 0).Err())
+ require.NoError(t, rdb.Get(ctx, "foo").Err())
+ v, err = c.ReadLine()
+ require.NoError(t, err)
+ require.Regexp(t, ".*set.*foo.*bar.*", v)
+ v, err = c.ReadLine()
+ require.NoError(t, err)
+ require.Regexp(t, ".*get.*foo.*", v)
+ })
+
+ t.Run("CLIENT GETNAME should return NIL if name is not assigned", func(t *testing.T) {
+ require.EqualError(t, rdb.ClientGetName(ctx).Err(), redis.Nil.Error())
+ })
+
+ t.Run("CLIENT LIST shows empty fields for unassigned names", func(t *testing.T) {
+ require.Regexp(t, ".*name= .*", rdb.ClientList(ctx).Val())
+ })
+
+ t.Run("CLIENT SETNAME does not accept spaces", func(t *testing.T) {
+ require.Error(t, rdb.Do(ctx, "CLIENT", "SETNAME", "foo bar").Err())
+ })
+
+ t.Run("CLIENT SETNAME can assign a name to this connection", func(t *testing.T) {
+ r := rdb.Do(ctx, "CLIENT", "SETNAME", "myname")
+ require.NoError(t, r.Err())
+ require.Equal(t, "OK", r.Val())
+ require.Regexp(t, ".*name=myname.*", rdb.ClientList(ctx).Val())
+ })
+
+ t.Run("CLIENT SETNAME can assign a name to this connection", func(t *testing.T) {
+ r := rdb.Do(ctx, "CLIENT", "SETNAME", "myname")
+ require.NoError(t, r.Err())
+ require.Equal(t, "OK", r.Val())
+ require.Regexp(t, ".*name=myname.*", rdb.ClientList(ctx).Val())
+ })
+
+ t.Run("CLIENT SETNAME can change the name of an existing connection", func(t *testing.T) {
+ r := rdb.Do(ctx, "CLIENT", "SETNAME", "someothername")
+ require.NoError(t, r.Err())
+ require.Equal(t, "OK", r.Val())
+ require.Regexp(t, ".*name=someothername.*", rdb.ClientList(ctx).Val())
+ })
+
+ t.Run("After CLIENT SETNAME, connection can still be closed", func(t *testing.T) {
+ func() {
+ c := srv.NewClient()
+ defer func() { require.NoError(t, c.Close()) }()
+ r := c.Do(ctx, "CLIENT", "SETNAME", "foobar")
+ require.NoError(t, r.Err())
+ require.Equal(t, "OK", r.Val())
+ require.Regexp(t, ".*name=foobar.*", rdb.ClientList(ctx).Val())
+ }()
+
+ // now the client should no longer be listed
+ require.Eventually(t, func() bool {
+ r := rdb.ClientList(ctx).Val()
+ return !strings.Contains(r, "foobar")
+ }, 5*time.Second, 100*time.Millisecond)
+ })
+
+ t.Run("Kill normal client", func(t *testing.T) {
+ defer func() { rdb = srv.NewClient() }()
+
+ c := srv.NewClient()
+ defer func() { require.NoError(t, c.Close()) }()
+ r := c.Do(ctx, "CLIENT", "SETNAME", "normal")
+ require.NoError(t, r.Err())
+ require.Equal(t, "OK", r.Val())
+ require.Regexp(t, ".*name=normal.*", rdb.ClientList(ctx).Val())
+
+ require.EqualValues(t, 1, rdb.ClientKillByFilter(ctx, "skipme", "yes", "type", "normal").Val())
+ require.EqualValues(t, 1, rdb.ClientKillByFilter(ctx, "skipme", "no", "type", "normal").Val())
+
+ // reconnect
+ rdb = srv.NewClient()
+ defer func() { require.NoError(t, rdb.Close()) }()
+
+ // now the client should no longer be listed
+ require.Eventually(t, func() bool {
+ r := rdb.ClientList(ctx).Val()
+ return !strings.Contains(r, "normal")
+ }, 5*time.Second, 100*time.Millisecond)
+ })
+
+ t.Run("Kill pubsub client", func(t *testing.T) {
+ // subscribe clients
+ c0 := srv.NewClient()
+ defer func() { require.NoError(t, c0.Close()) }()
+ require.NoError(t, c0.Do(ctx, "CLIENT", "SETNAME", "pubsub").Err())
+ v, err := c0.Subscribe(ctx, "foo").Receive(ctx)
+ require.NoError(t, err)
+ msg := v.(*redis.Subscription)
+ require.Equal(t, "subscribe", msg.Kind)
+ require.Equal(t, "foo", msg.Channel)
+ require.Equal(t, 1, msg.Count)
+
+ // psubscribe clients
+ c1 := srv.NewClient()
+ defer func() { require.NoError(t, c1.Close()) }()
+ require.NoError(t, c1.Do(ctx, "CLIENT", "SETNAME", "pubsub_patterns").Err())
+ v, err = c1.PSubscribe(ctx, "bar.*").Receive(ctx)
require.NoError(t, err)
- require.Equal(t, r, "+PONG")
- require.NoError(t, c.WriteArgs("PING", "PONG"))
- r, err = c.ReadLine()
+ msg = v.(*redis.Subscription)
+ require.Equal(t, "psubscribe", msg.Kind)
+ require.Equal(t, "bar.*", msg.Channel)
+ require.Equal(t, 1, msg.Count)
+
+ // normal clients
+ c2 := srv.NewClient()
+ require.NoError(t, c2.Do(ctx, "CLIENT", "SETNAME", "normal").Err())
+ defer func() { require.NoError(t, c2.Close()) }()
+
+ require.EqualValues(t, 2, rdb.ClientKillByFilter(ctx, "type", "pubsub").Val())
+
+ // now the pubsub client should no longer be listed
+ // but normal client should not be dropped
+ t.Skip("cannot run the following assertions")
Review Comment:
@tisonkun PubSub will try to reconnect when connection is lost. Perhaps that is what you are seeing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org