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/09/11 17:05:14 UTC

[incubator-kvrocks] branch unstable updated: Move TCL test unit/type/list-3 to Go case (#835)

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 2402dfe  Move TCL test unit/type/list-3 to Go case (#835)
2402dfe is described below

commit 2402dfec05c582235dfb195bb1d4210584e59848
Author: Sg <su...@users.noreply.github.com>
AuthorDate: Mon Sep 12 01:05:07 2022 +0800

    Move TCL test unit/type/list-3 to Go case (#835)
---
 tests/gocase/unit/type/list/list_test.go | 149 +++++++++++++++++++++++++++++++
 tests/gocase/util/random.go              | 108 ++++++++++++++++++++++
 tests/tcl/tests/test_helper.tcl          |   1 -
 tests/tcl/tests/unit/type/list-3.tcl     | 145 ------------------------------
 4 files changed, 257 insertions(+), 146 deletions(-)

diff --git a/tests/gocase/unit/type/list/list_test.go b/tests/gocase/unit/type/list/list_test.go
index eb75dd5..e8a288a 100644
--- a/tests/gocase/unit/type/list/list_test.go
+++ b/tests/gocase/unit/type/list/list_test.go
@@ -84,3 +84,152 @@ func BenchmarkLTRIM(b *testing.B) {
 		})
 	}
 }
+
+func TestZipList(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{
+		"list-max-ziplist-size": "16",
+	})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	rand.Seed(0)
+
+	t.Run("Explicit regression for a list bug", func(t *testing.T) {
+		key := "l"
+		myList := []string{
+			"49376042582", "BkG2o\\pIC]4YYJa9cJ4GWZalG[4tin;1D2whSkCOW`mX;SFXGyS8sedcff3fQI^tgPCC@^Nu1J6o]meM@Lko]t_jRyo<xSJ1oObDYd`ppZuW6P@fS278YaOx=s6lvdFlMbP0[SbkI^Kr\\HBXtuFaA^mDx:yzS4a[skiiPWhT<nNfAf=aQVfclcuwDrfe;iVuKdNvB9kbfq>tK?tH[\\EvWqS]b`o2OCtjg:?nUTwdjpcUm]y:pg5q24q7LlCOwQE^",
+		}
+		require.NoError(t, rdb.Del(ctx, key).Err())
+		require.NoError(t, rdb.RPush(ctx, key, myList[0]).Err())
+		require.NoError(t, rdb.RPush(ctx, key, myList[1]).Err())
+		require.Equal(t, myList[0], rdb.LIndex(ctx, key, 0).Val())
+		require.Equal(t, myList[1], rdb.LIndex(ctx, key, 1).Val())
+	})
+
+	t.Run("Regression for quicklist #3343 bug", func(t *testing.T) {
+		key := "myList"
+		require.NoError(t, rdb.Del(ctx, key).Err())
+		require.NoError(t, rdb.LPush(ctx, key, "401").Err())
+		require.NoError(t, rdb.LPush(ctx, key, "392").Err())
+
+		require.NoError(t, rdb.RPush(ctx, key, fmt.Sprintf("%s\"%s\"", strings.Repeat("x", 5105), "799")).Err())
+		require.NoError(t, rdb.LSet(ctx, key, -1, fmt.Sprintf("%s\"%s\"", strings.Repeat("x", 1014), "702")).Err())
+
+		require.NoError(t, rdb.LPop(ctx, key).Err())
+
+		require.NoError(t, rdb.LSet(ctx, key, -1, fmt.Sprintf("%s\"%s\"", strings.Repeat("x", 4149), "852")).Err())
+
+		require.NoError(t, rdb.LInsert(ctx, key, "before", "401", fmt.Sprintf("%s\"%s\"", strings.Repeat("x", 9927), "12")).Err())
+		require.NoError(t, rdb.LRange(ctx, key, 0, -1).Err())
+		require.Equal(t, rdb.Ping(ctx).Val(), "PONG")
+	})
+
+	t.Run("Stress tester for #3343-alike bugs", func(t *testing.T) {
+		key := "key"
+		require.NoError(t, rdb.Del(ctx, key).Err())
+		for i := 0; i < 10000; i++ {
+			op := rand.Int63n(6)
+			randCnt := 5 - rand.Int63n(10)
+			var ele string
+			if rand.Int31n(2) == 0 {
+				ele = fmt.Sprintf("%d", rand.Int63n(1000))
+			} else {
+				ele = fmt.Sprintf("%s%d", strings.Repeat("x", int(rand.Int63n(10000))), 1)
+			}
+			switch op {
+			case 0:
+				require.NoError(t, rdb.LPush(ctx, key, ele).Err())
+			case 1:
+				require.NoError(t, rdb.RPush(ctx, key, ele).Err())
+			case 2:
+				rdb.LPop(ctx, key)
+			case 3:
+				rdb.RPop(ctx, key)
+			case 4:
+				rdb.LSet(ctx, key, randCnt, ele)
+			case 5:
+				otherEle := fmt.Sprintf("%d", rand.Int63n(1000))
+				var where string
+				if rand.Int31n(2) == 0 {
+					where = "before"
+				} else {
+					where = "after"
+				}
+				require.NoError(t, rdb.LInsert(ctx, key, where, otherEle, ele).Err())
+			}
+		}
+	})
+
+	t.Run("ziplist implementation: value encoding and backlink", func(t *testing.T) {
+		iterations := 100
+		key := "l"
+		for j := 0; j < iterations; j++ {
+			require.NoError(t, rdb.Del(ctx, key).Err())
+			lis := []string{}
+			for i := 0; i < 200; i++ {
+				op := rand.Int63n(7)
+				data := ""
+				switch op {
+				case 0:
+					data = strings.Repeat("x", int(rand.Int63n(1000000)))
+				case 1:
+					data = fmt.Sprintf("%d", rand.Int63n(65536))
+				case 2:
+					data = fmt.Sprintf("%d", rand.Int63n(4294967296))
+				case 3:
+					data = fmt.Sprintf("%d", rand.Uint64())
+				case 4:
+					data = fmt.Sprintf("-%d", rand.Int63n(65536))
+					if data == "-0" {
+						data = "0"
+					}
+				case 5:
+					data = fmt.Sprintf("-%d", rand.Int63n(4294967296))
+					if data == "-0" {
+						data = "0"
+					}
+				case 6:
+					data = fmt.Sprintf("-%d", rand.Uint64())
+					if data == "-0" {
+						data = "0"
+					}
+				}
+				lis = append(lis, data)
+				require.NoError(t, rdb.RPush(ctx, key, data).Err())
+			}
+			require.Equal(t, int64(len(lis)), rdb.LLen(ctx, key).Val())
+
+			for i := 199; i >= 0; i-- {
+				require.Equal(t, lis[i], rdb.LIndex(ctx, key, int64(i)).Val())
+			}
+		}
+	})
+
+	t.Run("ziplist implementation: encoding stress testing", func(t *testing.T) {
+		key := "l"
+		for j := 0; j < 200; j++ {
+			require.NoError(t, rdb.Del(ctx, key).Err())
+			var lis []string
+			l := int(rand.Int63n(400))
+			for i := 0; i < l; i++ {
+				rv := util.RandomValue()
+				util.RandPathNoResult(
+					func() {
+						lis = append(lis, rv)
+						require.NoError(t, rdb.RPush(ctx, key, rv).Err())
+					},
+					func() {
+						lis = append([]string{rv}, lis...)
+						require.NoError(t, rdb.LPush(ctx, key, rv).Err())
+					},
+				)
+			}
+			require.Equal(t, int64(len(lis)), rdb.LLen(ctx, key).Val())
+			for i := 0; i < l; i++ {
+				require.Equal(t, lis[i], rdb.LIndex(ctx, key, int64(i)).Val())
+			}
+		}
+	})
+}
diff --git a/tests/gocase/util/random.go b/tests/gocase/util/random.go
new file mode 100644
index 0000000..c681a78
--- /dev/null
+++ b/tests/gocase/util/random.go
@@ -0,0 +1,108 @@
+/*
+ * 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 util
+
+import (
+	"fmt"
+	"math/rand"
+	"strings"
+)
+
+func RandPath[T any](funcs ...func() T) T {
+	index := rand.Int31n(int32(len(funcs)))
+	return funcs[index]()
+}
+
+func RandPathNoResult(funcs ...func()) {
+	index := rand.Int31n(int32(len(funcs)))
+	funcs[index]()
+}
+
+// Random signed integer in (-max, max)
+func randomSignedInt(max int32) int64 {
+	return rand.Int63n(int64(max)*2-1) - int64(max) + 1
+}
+
+type RandStringType int
+
+const (
+	Alpha RandStringType = iota
+	Binary
+	Compr
+)
+
+func RandString(min, max int, typ RandStringType) string {
+	length := min + rand.Intn(max-min+1)
+
+	var minVal, maxVal int
+	switch typ {
+	case Binary:
+		minVal, maxVal = 0, 255
+	case Alpha:
+		minVal, maxVal = 48, 122
+	case Compr:
+		minVal, maxVal = 48, 52
+	}
+
+	var sb strings.Builder
+	for ; length > 0; length-- {
+		s := fmt.Sprintf("%c", minVal+rand.Intn(maxVal-minVal+1))
+		sb.WriteString(s)
+	}
+	return sb.String()
+}
+
+func RandomValue() string {
+	return RandPath(
+		// Small enough to likely collide
+		func() string {
+			return fmt.Sprintf("%d", randomSignedInt(1000))
+		},
+		// 32 bit compressible signed/unsigned
+		func() string {
+			return RandPath(
+				func() string {
+					return fmt.Sprintf("%d", rand.Int63n(2000000000))
+				},
+				func() string {
+					return fmt.Sprintf("%d", rand.Int63n(4000000000))
+				},
+			)
+		},
+		// 64 bit
+		func() string {
+			return fmt.Sprintf("%d", rand.Int63n(1000000000000))
+		},
+		// Random string
+		func() string {
+			return RandPath(
+				func() string {
+					return RandString(0, 256, Alpha)
+				},
+				func() string {
+					return RandString(0, 256, Compr)
+				},
+				func() string {
+					return RandString(0, 256, Binary)
+				},
+			)
+		},
+	)
+}
diff --git a/tests/tcl/tests/test_helper.tcl b/tests/tcl/tests/test_helper.tcl
index 9bdb682..1d0781d 100644
--- a/tests/tcl/tests/test_helper.tcl
+++ b/tests/tcl/tests/test_helper.tcl
@@ -36,7 +36,6 @@ set ::all_tests {
     unit/scan
     unit/type/string
     unit/type/list
-    unit/type/list-3
     unit/type/set
     unit/type/zset
     unit/type/hash
diff --git a/tests/tcl/tests/unit/type/list-3.tcl b/tests/tcl/tests/unit/type/list-3.tcl
deleted file mode 100644
index 0cea10c..0000000
--- a/tests/tcl/tests/unit/type/list-3.tcl
+++ /dev/null
@@ -1,145 +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/type/list-3.tcl
-
-start_server {
-    tags {list ziplist}
-    overrides {
-        "list-max-ziplist-size" 16
-    }
-} {
-    test {Explicit regression for a list bug} {
-        set mylist {49376042582 {BkG2o\pIC]4YYJa9cJ4GWZalG[4tin;1D2whSkCOW`mX;SFXGyS8sedcff3fQI^tgPCC@^Nu1J6o]meM@Lko]t_jRyo<xSJ1oObDYd`ppZuW6P@fS278YaOx=s6lvdFlMbP0[SbkI^Kr\HBXtuFaA^mDx:yzS4a[skiiPWhT<nNfAf=aQVfclcuwDrfe;iVuKdNvB9kbfq>tK?tH[\EvWqS]b`o2OCtjg:?nUTwdjpcUm]y:pg5q24q7LlCOwQE^}}
-        r del l
-        r rpush l [lindex $mylist 0]
-        r rpush l [lindex $mylist 1]
-        assert_equal [r lindex l 0] [lindex $mylist 0]
-        assert_equal [r lindex l 1] [lindex $mylist 1]
-    }
-
-    test {Regression for quicklist #3343 bug} {
-        r del mylist
-        r lpush mylist 401
-        r lpush mylist 392
-        r rpush mylist [string repeat x 5105]"799"
-        r lset mylist -1 [string repeat x 1014]"702"
-        r lpop mylist
-        r lset mylist -1 [string repeat x 4149]"852"
-        r linsert mylist before 401 [string repeat x 9927]"12"
-        r lrange mylist 0 -1
-        r ping ; # It's enough if the server is still alive
-    } {PONG}
-
-    test {Stress tester for #3343-alike bugs} {
-        r del key
-        for {set j 0} {$j < 10000} {incr j} {
-            set op [randomInt 6]
-            set small_signed_count [expr 5-[randomInt 10]]
-            if {[randomInt 2] == 0} {
-                set ele [randomInt 1000]
-            } else {
-                set ele [string repeat x [randomInt 10000]][randomInt 1000]
-            }
-            switch $op {
-                0 {r lpush key $ele}
-                1 {r rpush key $ele}
-                2 {r lpop key}
-                3 {r rpop key}
-                4 {
-                    catch {r lset key $small_signed_count $ele}
-                }
-                5 {
-                    set otherele [randomInt 1000]
-                    if {[randomInt 2] == 0} {
-                        set where before
-                    } else {
-                        set where after
-                    }
-                    r linsert key $where $otherele $ele
-                }
-            }
-        }
-    }
-
-    tags {slow} {
-        test {ziplist implementation: value encoding and backlink} {
-            if {$::accurate} {set iterations 100} else {set iterations 10}
-            for {set j 0} {$j < $iterations} {incr j} {
-                r del l
-                set l {}
-                for {set i 0} {$i < 200} {incr i} {
-                    randpath {
-                        set data [string repeat x [randomInt 100000]]
-                    } {
-                        set data [randomInt 65536]
-                    } {
-                        set data [randomInt 4294967296]
-                    } {
-                        set data [randomInt 18446744073709551616]
-                    } {
-                        set data -[randomInt 65536]
-                        if {$data eq {-0}} {set data 0}
-                    } {
-                        set data -[randomInt 4294967296]
-                        if {$data eq {-0}} {set data 0}
-                    } {
-                        set data -[randomInt 18446744073709551616]
-                        if {$data eq {-0}} {set data 0}
-                    }
-                    lappend l $data
-                    r rpush l $data
-                }
-                assert_equal [llength $l] [r llen l]
-                # Traverse backward
-                for {set i 199} {$i >= 0} {incr i -1} {
-                    if {[lindex $l $i] ne [r lindex l $i]} {
-                        assert_equal [lindex $l $i] [r lindex l $i]
-                    }
-                }
-            }
-        }
-
-        test {ziplist implementation: encoding stress testing} {
-            for {set j 0} {$j < 200} {incr j} {
-                r del l
-                set l {}
-                set len [randomInt 400]
-                for {set i 0} {$i < $len} {incr i} {
-                    set rv [randomValue]
-                    randpath {
-                        lappend l $rv
-                        r rpush l $rv
-                    } {
-                        set l [concat [list $rv] $l]
-                        r lpush l $rv
-                    }
-                }
-                assert_equal [llength $l] [r llen l]
-                for {set i 0} {$i < $len} {incr i} {
-                    if {[lindex $l $i] ne [r lindex l $i]} {
-                        assert_equal [lindex $l $i] [r lindex l $i]
-                    }
-                }
-            }
-        }
-    }
-}