You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/10/04 02:54:50 UTC

[incubator-kvrocks] branch unstable updated: Move TCL test unit/slowlog to Go case (#935)

This is an automated email from the ASF dual-hosted git repository.

tison pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 43cca6a  Move TCL test unit/slowlog to Go case (#935)
43cca6a is described below

commit 43cca6a1ec50bfc850b8d9d08c552a61de8c0034
Author: Ruixiang Tan <81...@qq.com>
AuthorDate: Tue Oct 4 10:54:44 2022 +0800

    Move TCL test unit/slowlog to Go case (#935)
---
 tests/gocase/unit/slowlog/slowlog_test.go | 164 ++++++++++++++++++++++++++++++
 tests/tcl/tests/test_helper.tcl           |   1 -
 tests/tcl/tests/unit/slowlog.tcl          | 137 -------------------------
 3 files changed, 164 insertions(+), 138 deletions(-)

diff --git a/tests/gocase/unit/slowlog/slowlog_test.go b/tests/gocase/unit/slowlog/slowlog_test.go
new file mode 100644
index 0000000..200aedd
--- /dev/null
+++ b/tests/gocase/unit/slowlog/slowlog_test.go
@@ -0,0 +1,164 @@
+/*
+ * 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 slowlog
+
+import (
+	"context"
+	"strings"
+	"testing"
+
+	"github.com/apache/incubator-kvrocks/tests/gocase/util"
+	"github.com/go-redis/redis/v9"
+	"github.com/stretchr/testify/require"
+)
+
+func TestSlowlog(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{
+		"slowlog-log-slower-than": "1000000",
+	})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	t.Run("SLOWLOG - check that it starts with an empty log", func(t *testing.T) {
+		require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
+	})
+
+	t.Run("SLOWLOG - only logs commands taking more time than specified", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "100000").Err())
+		require.NoError(t, rdb.Ping(ctx).Err())
+		require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
+	})
+
+	t.Run("SLOWLOG - max entries is correctly handled", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "0").Err())
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-max-len", "10").Err())
+		for i := 0; i < 100; i++ {
+			require.NoError(t, rdb.Ping(ctx).Err())
+		}
+		require.EqualValues(t, 10, len(rdb.SlowLogGet(ctx, -1).Val()))
+	})
+
+	t.Run("SLOWLOG - GET optional argument to limit output len works", func(t *testing.T) {
+		require.EqualValues(t, 5, len(rdb.SlowLogGet(ctx, 5).Val()))
+	})
+
+	t.Run("SLOWLOG - RESET subcommand works", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "100000").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+		require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
+	})
+
+	t.Run("SLOWLOG - logged entry sanity check", func(t *testing.T) {
+		require.NoError(t, rdb.Do(ctx, "client", "setname", "foobar").Err())
+		require.NoError(t, rdb.Do(ctx, "debug", "sleep", 0.2).Err())
+		val, err := rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, 105, val[0].ID)
+		require.EqualValues(t, true, val[0].Duration > 100000)
+		require.EqualValues(t, []string{"debug", "sleep", "0.2"}, val[0].Args)
+	})
+
+	t.Run("SLOWLOG - Rewritten commands are logged as their original command", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "0").Err())
+		// Test rewriting client arguments
+		require.NoError(t, rdb.SAdd(ctx, "set", "a", "b", "c", "d", "e").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+
+		// SPOP is rewritten as DEL when all keys are removed
+		require.NoError(t, rdb.SPopN(ctx, "set", 10).Err())
+		val, err := rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"spop", "set", "10"}, val[0].Args)
+
+		// Test replacing client arguments
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+
+		// GEOADD is replicated as ZADD
+		require.NoError(t, rdb.GeoAdd(ctx, "cool-cities", &redis.GeoLocation{Longitude: -122.33207, Latitude: 47.60621, Name: "Seattle"}).Err())
+
+		val, err = rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"geoadd", "cool-cities", "-122.33207", "47.60621", "Seattle"}, val[0].Args)
+
+		// Test replacing a single command argument
+		require.NoError(t, rdb.Set(ctx, "A", 5, 0).Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+
+		// GETSET is replicated as SET
+		require.EqualValues(t, redis.Nil, rdb.GetSet(ctx, "a", "5").Err())
+		val, err = rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"getset", "a", "5"}, val[0].Args)
+
+		// INCRBYFLOAT calls rewrite multiple times, so it's a special case
+		require.NoError(t, rdb.Set(ctx, "A", 0, 0).Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+
+		// INCRBYFLOAT is replicated as SET
+		require.NoError(t, rdb.IncrByFloat(ctx, "A", 1.0).Err())
+		val, err = rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"incrbyfloat", "A", "1"}, val[0].Args)
+	})
+
+	t.Run("SLOWLOG - commands with too many arguments are trimmed", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "0").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+		require.NoError(t, rdb.SAdd(ctx, "set", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33).Err())
+		val, err := rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"sadd", "set", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "... (2 more arguments)"}, val[0].Args)
+	})
+
+	t.Run("SLOWLOG - too long arguments are trimmed", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "0").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+		require.NoError(t, rdb.SAdd(ctx, "set", "foo", strings.Repeat("A", 129)).Err())
+		val, err := rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, []string{"sadd", "set", "foo", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... (1 more bytes)"}, val[0].Args)
+	})
+
+	t.Run("SLOWLOG - can clean older entries", func(t *testing.T) {
+		require.NoError(t, rdb.Do(ctx, "client", "setname", "lastentry_client").Err())
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-max-len", "1").Err())
+		require.NoError(t, rdb.Do(ctx, "debug", "sleep", 0.2).Err())
+
+		val, err := rdb.SlowLogGet(ctx, -1).Result()
+		require.NoError(t, err)
+		require.EqualValues(t, 1, len(val))
+	})
+
+	t.Run("SLOWLOG - can be disabled", func(t *testing.T) {
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-max-len", "1").Err())
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "1").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+		require.NoError(t, rdb.Do(ctx, "debug", "sleep", 0.2).Err())
+		require.EqualValues(t, 1, len(rdb.SlowLogGet(ctx, -1).Val()))
+
+		require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "-1").Err())
+		require.NoError(t, rdb.Do(ctx, "slowlog", "reset").Err())
+		require.NoError(t, rdb.Do(ctx, "debug", "sleep", 0.2).Err())
+		require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
+	})
+
+}
diff --git a/tests/tcl/tests/test_helper.tcl b/tests/tcl/tests/test_helper.tcl
index 8808a74..e66ac5b 100644
--- a/tests/tcl/tests/test_helper.tcl
+++ b/tests/tcl/tests/test_helper.tcl
@@ -39,7 +39,6 @@ set ::all_tests {
     unit/type/stream
     unit/multi
     unit/expire
-    unit/slowlog
     unit/pubsub
     unit/introspection
     unit/geo
diff --git a/tests/tcl/tests/unit/slowlog.tcl b/tests/tcl/tests/unit/slowlog.tcl
deleted file mode 100644
index 333791b..0000000
--- a/tests/tcl/tests/unit/slowlog.tcl
+++ /dev/null
@@ -1,137 +0,0 @@
-# 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.
-
-# Copyright (c) 2006-2020, Salvatore Sanfilippo
-# See bundled license file licenses/LICENSE.redis for details.
-
-# This file is copied and modified from the Redis project,
-# which started out as: https://github.com/redis/redis/blob/dbcc0a8/tests/unit/slowlog.tcl
-
-start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
-    test {SLOWLOG - check that it starts with an empty log} {
-        r slowlog len
-    } {0}
-
-    test {SLOWLOG - only logs commands taking more time than specified} {
-        r config set slowlog-log-slower-than 100000
-        r ping
-        assert_equal [r slowlog len] 0
-    }
-
-    test {SLOWLOG - max entries is correctly handled} {
-        r config set slowlog-log-slower-than 0
-        r config set slowlog-max-len 10
-        for {set i 0} {$i < 100} {incr i} {
-            r ping
-        }
-        r slowlog len
-    } {10}
-
-    test {SLOWLOG - GET optional argument to limit output len works} {
-        llength [r slowlog get 5]
-    } {5}
-
-    test {SLOWLOG - RESET subcommand works} {
-        r config set slowlog-log-slower-than 100000
-        r slowlog reset
-        r slowlog len
-    } {0}
-
-    test {SLOWLOG - logged entry sanity check} {
-        r client setname foobar
-        r debug sleep 0.2
-        set e [lindex [r slowlog get] 0]
-        assert_equal [llength $e] 4
-        assert_equal [lindex $e 0] 105
-        assert_equal [expr {[lindex $e 2] > 100000}] 1
-        assert_equal [lindex $e 3] {debug sleep 0.2}
-        #assert_equal {foobar} [lindex $e 5]
-    }
-
-    test {SLOWLOG - Rewritten commands are logged as their original command} {
-        r config set slowlog-log-slower-than 0
-
-        # Test rewriting client arguments
-        r sadd set a b c d e
-        r slowlog reset
-
-        # SPOP is rewritten as DEL when all keys are removed
-        r spop set 10
-        assert_equal {spop set 10} [lindex [lindex [r slowlog get] 0] 3]
-
-        # Test replacing client arguments
-        r slowlog reset
-
-        # GEOADD is replicated as ZADD
-        r geoadd cool-cities -122.33207 47.60621 Seattle
-        assert_equal {geoadd cool-cities -122.33207 47.60621 Seattle} [lindex [lindex [r slowlog get] 0] 3]
-
-        # Test replacing a single command argument
-        r set A 5
-        r slowlog reset
-        
-        # GETSET is replicated as SET
-        r getset a 5
-        assert_equal {getset a 5} [lindex [lindex [r slowlog get] 0] 3]
-
-        # INCRBYFLOAT calls rewrite multiple times, so it's a special case
-        r set A 0
-        r slowlog reset
-        
-        # INCRBYFLOAT is replicated as SET
-        r INCRBYFLOAT A 1.0
-        assert_equal {INCRBYFLOAT A 1.0} [lindex [lindex [r slowlog get] 0] 3]
-    }
-
-    test {SLOWLOG - commands with too many arguments are trimmed} {
-        r config set slowlog-log-slower-than 0
-        r slowlog reset
-        r sadd set 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
-        set e [lindex [r slowlog get] 0]
-        lindex $e 3
-    } {sadd set 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 {... (2 more arguments)}}
-
-    test {SLOWLOG - too long arguments are trimmed} {
-        r config set slowlog-log-slower-than 0
-        r slowlog reset
-        set arg [string repeat A 129]
-        r sadd set foo $arg
-        set e [lindex [r slowlog get] 0]
-        lindex $e 3
-    } {sadd set foo {AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... (1 more bytes)}}
-
-    test {SLOWLOG - can clean older entries} {
-        r client setname lastentry_client
-        r config set slowlog-max-len 1
-        r debug sleep 0.2
-        assert {[llength [r slowlog get]] == 1}
-        set e [lindex [r slowlog get] 0]
-        #assert_equal {lastentry_client} [lindex $e 5]
-    }
-
-    test {SLOWLOG - can be disabled} {
-        r config set slowlog-max-len 1
-        r config set slowlog-log-slower-than 1
-        r slowlog reset
-        r debug sleep 0.2
-        assert_equal [r slowlog len] 1
-        r config set slowlog-log-slower-than -1
-        r slowlog reset
-        r debug sleep 0.2
-        assert_equal [r slowlog len] 0
-    }
-}