tisonkun commented on code in PR #846: URL: https://github.com/apache/incubator-kvrocks/pull/846#discussion_r967749497
########## tests/gocase/unit/keyspace_test.go: ########## @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package unit + +import ( + "context" + "sort" + "testing" + "time" + + "github.com/apache/incubator-kvrocks/tests/gocase/util" + "github.com/stretchr/testify/require" +) + +func TestKeyspace(t *testing.T) { + srv := util.StartServer(t, map[string]string{}) + defer srv.Close() + + ctx := context.Background() + rdb := srv.NewClient() + defer func() { require.NoError(t, rdb.Close()) }() + + t.Run("DEL against a single item", func(t *testing.T) { + key := "x" + value := "foo" + require.NoError(t, rdb.Set(ctx, key, value, 0).Err()) + require.Equal(t, value, rdb.Get(ctx, key).Val()) + require.Equal(t, int64(1), rdb.Del(ctx, key).Val()) + require.Equal(t, "", rdb.Get(ctx, key).Val()) + }) + + t.Run("Vararg DEL", func(t *testing.T) { + require.NoError(t, rdb.Set(ctx, "foo1", "a", 0).Err()) + require.NoError(t, rdb.Set(ctx, "foo2", "b", 0).Err()) + require.NoError(t, rdb.Set(ctx, "foo3", "c", 0).Err()) + require.Equal(t, int64(3), rdb.Del(ctx, "foo1", "foo2", "foo3").Val()) + require.Equal(t, []interface{}{nil, nil, nil}, rdb.MGet(ctx, "foo1", "foo2", "foo3").Val()) + }) + + t.Run("KEYS with pattern", func(t *testing.T) { + for _, key := range []string{"key_x", "key_y", "key_z", "foo_a", "foo_b", "foo_c"} { + require.NoError(t, rdb.Set(ctx, key, "hello", 0).Err()) + } + keys := rdb.Keys(ctx, "foo*").Val() + sort.Slice(keys, func(i, j int) bool { + return keys[i] < keys[j] + }) + require.Equal(t, []string{"foo_a", "foo_b", "foo_c"}, keys) + }) + + t.Run("KEYS to get all keys", func(t *testing.T) { + keys := rdb.Keys(ctx, "*").Val() + sort.Slice(keys, func(i, j int) bool { + return keys[i] < keys[j] + }) + require.Equal(t, []string{"foo_a", "foo_b", "foo_c", "key_x", "key_y", "key_z"}, keys) + }) + + t.Run("DEL all keys", func(t *testing.T) { + vals := rdb.Keys(ctx, "*").Val() + require.Equal(t, int64(len(vals)), rdb.Del(ctx, vals...).Val()) + require.NoError(t, rdb.Do(ctx, "dbsize", "scan").Err()) + time.Sleep(100 * time.Millisecond) + require.Equal(t, int64(0), rdb.Do(ctx, "dbsize").Val()) + }) + + t.Run("EXISTS", func(t *testing.T) { + newKey := "newkey" + require.NoError(t, rdb.Set(ctx, newKey, "test", 0).Err()) + require.Equal(t, int64(1), rdb.Exists(ctx, newKey).Val()) + require.Equal(t, int64(1), rdb.Del(ctx, newKey).Val()) + require.Equal(t, int64(0), rdb.Exists(ctx, newKey).Val()) + }) + + t.Run("Zero length value in key. SET/GET/EXISTS", func(t *testing.T) { + emptyKey := "emptykey" + require.NoError(t, rdb.Set(ctx, emptyKey, nil, 0).Err()) + require.Equal(t, int64(1), rdb.Exists(ctx, emptyKey).Val()) + require.Equal(t, int64(1), rdb.Del(ctx, emptyKey).Val()) + require.Equal(t, int64(0), rdb.Exists(ctx, emptyKey).Val()) + }) + + t.Run("Commands pipelining", func(t *testing.T) { + c := srv.NewTCPClient() + defer func() { require.NoError(t, c.Close()) }() + require.NoError(t, c.Write("SET k1 xyzk\r\nGET k1\r\nPING\r\n")) + r, err := c.ReadLine() + require.NoError(t, err) + require.Equal(t, "+OK", r) + r, err = c.ReadLine() + require.NoError(t, err) + require.Equal(t, "$4", r) + r, err = c.ReadLine() + require.NoError(t, err) + require.Equal(t, "xyzk", r) + r, err = c.ReadLine() + require.NoError(t, err) + require.Equal(t, "+PONG", r) + }) + + t.Run("Non existing command", func(t *testing.T) { + require.Contains(t, rdb.Do(ctx, "foobaredcommand").Err().Error(), "ERR") + }) + + t.Run("RANDOMKEY", func(t *testing.T) { + rdb.FlushDB(ctx) + require.NoError(t, rdb.Set(ctx, "foo", "x", 0).Err()) + require.NoError(t, rdb.Set(ctx, "bar", "y", 0).Err()) + + fooSeen := 0 + barSeen := 0 + for i := 0; i < 1000; i++ { + randomKey := rdb.RandomKey(ctx).Val() + if randomKey == "foo" { + fooSeen = 1 + } else if randomKey == "bar" { + barSeen = 1 + } + if fooSeen == 1 || barSeen == 1 { + break + } + time.Sleep(10 * time.Millisecond) + } + require.Greater(t, fooSeen+barSeen, 0) + }) + + t.Run("RANDOMKEY against empty DB", func(t *testing.T) { + rdb.FlushDB(ctx) + require.Equal(t, "", rdb.RandomKey(ctx).Val()) + }) + + t.Run("RANDOMKEY regression 1", func(t *testing.T) { + rdb.FlushDB(ctx) + require.NoError(t, rdb.Set(ctx, "x", 10, 0).Err()) + require.Equal(t, int64(1), rdb.Del(ctx, "x").Val()) + require.Equal(t, "", rdb.RandomKey(ctx).Val()) + }) + + t.Run("KEYS * two times with long key, Github issue #1208", func(t *testing.T) { + rdb.FlushDB(ctx) + require.NoError(t, rdb.Set(ctx, "dlskeriewrioeuwqoirueioqwrueoqwrueqw", "teset", 0).Err()) + require.Equal(t, []string{"dlskeriewrioeuwqoirueioqwrueoqwrueqw"}, rdb.Keys(ctx, "*").Val()) Review Comment: No. The case is that Redis once encounter an issue that if run `KEY *` twice, it will fail. I tend to keep these two lines or we just rename/remove the test case. See also https://github.com/redis/redis/issues/1208. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
