11set testmodule [file normalize YOUR_PATH/redisxslot.so]
22
3+ # reference:
4+ # 0. https://github.com/tcltk/tcl
5+ # 0. https://wiki.tcl-lang.org/page/Tcllib+Installation
6+ # 1. tests/unit/cluster/cli.tcl
7+ # 2. tests/unit/dump.tcl
8+ # 3. tests/unit/moduleapi/propagate.tcl
9+ # 4. tests/unit/support/cluster.tcl
10+
11+ # need install tcllib
12+ package require crc32
13+
14+ # --------------------- prepare -----------------#
15+ proc init_config_1 {r} {
16+ $r config set hz 100
17+ # try to config set, it will fail if redis was compiled without it
18+ catch {$r config set activerehashing yes} e
19+ # (error) ERR CONFIG SET failed (possibly related to argument 'enable-module-command') - can't set immutable config
20+ catch {$r config set enable-module-command yes} e
21+ }
22+
23+ # --------------------- test -------------------#
24+ proc add_test_data {r n tag} {
25+ set key_list {}
26+ for {set i 0} {$i < $n } {incr i} {
27+ set key " $i "
28+ append key [randstring 0 512 alpha]
29+ append key " {$tag }"
30+ lappend key_list $key
31+ if {$i >=10 && $i < 20} {
32+ $r sadd $key m1 m2 m3
33+ continue
34+ }
35+ if {$i >=20 && $i < 30} {
36+ $r hmset $key f1 v1 f2 v2
37+ continue
38+ }
39+ if {$i >=30 && $i < 40} {
40+ $r lpush $key l1 l2
41+ continue
42+ }
43+ if {$i >=40 && $i < 50} {
44+ $r zadd $key 100 z1 10 z2
45+ continue
46+ }
47+ $r setex $key 86400 [randstring 0 512 alpha]
48+ }
49+ return $key_list
50+ }
51+
52+ proc put_slot_list {r slotsize n tag_list} {
53+ set slot_list {}
54+ foreach tag $tag_list {
55+ add_test_data $r $n $tag
56+ set c32 [crc::crc32 $tag ]
57+ lappend slot_list [expr {$c32 %$slotsize }]
58+ }
59+ return [lsort -integer $slot_list ]
60+ }
61+
62+ proc print_module_args {r} {
63+ set mlist [$r module list]
64+ puts " $mlist "
65+ }
66+
67+ proc test_local_cmd {r slotsize} {
68+ test " test slotshashkey - slotsize: $slotsize " {
69+ set c32 [crc::crc32 " tag" ]
70+ set key_slot [expr {$c32 %$slotsize }]
71+ set res_list [$r slotshashkey " {tag}" " 123{tag}" " 1{tag}" " 1{tag}{tag123}" ]
72+ foreach item $res_list {
73+ assert_equal $item $key_slot
74+ }
75+
76+ foreach type {alpha compr} {
77+ for {set i 0} {$i < 10} {incr i} {
78+ set key [randstring 0 512 $type ]
79+ set c32 [crc::crc32 $key ]
80+ set key_slot [expr {$c32 %$slotsize }]
81+ assert_equal $key_slot [$r slotshashkey $key ]
82+ }
83+ }
84+ }
85+
86+ test " test slotsinfo - slotsize: $slotsize " {
87+ $r flushdb
88+ set res [$r slotsinfo 0 $slotsize ]
89+ assert_equal 0 [llength $res ]
90+
91+ set n 100
92+ set tag_list {" tag0" " tag1" " tag2" " tag3" " tag4" " tag5" }
93+ set slot_list [put_slot_list $r $slotsize $n $tag_list ]
94+ assert_equal [llength $slot_list ] [llength $tag_list ]
95+ # puts "slot_list $slot_list"
96+ set res [$r slotsinfo 0 $slotsize ]
97+ # puts "slotsinfo_res $res"
98+ assert_equal [llength $slot_list ] [llength $res ]
99+ for {set i 0} {$i < [llength $slot_list ]} {incr i} {
100+ assert_equal [lindex $slot_list $i ] [lindex [lindex $res $i ] 0]
101+ assert_equal $n [lindex [lindex $res $i ] 1]
102+ }
103+ }
104+
105+ test " test slotsscan - slotsize: $slotsize " {
106+ $r flushdb
107+ set res [$r slotsinfo 0 $slotsize ]
108+ assert_equal 0 [llength $res ]
109+
110+ set n 100
111+ set tag " tag5"
112+ set slot [expr {[crc::crc32 $tag ]%$slotsize }]
113+ set key_list [add_test_data $r $n $tag ]
114+ assert_equal $n [llength $key_list ]
115+ set res [$r slotsscan $slot 0 count $n ]
116+ assert_equal 2 [llength $res ]
117+ assert_equal [llength $key_list ] [llength [lindex $res 1]]
118+ set res [$r slotsscan $slot 0 count [expr {$n +1}]]
119+ assert_equal 2 [llength $res ]
120+ assert_equal [llength $key_list ] [llength [lindex $res 1]]
121+ }
122+
123+ test " test slotsdel - slotsize: $slotsize " {
124+ $r flushdb
125+ set res [$r slotsinfo 0 $slotsize ]
126+ assert_equal 0 [llength $res ]
127+
128+ set n 100
129+ set tag_list {" tag0" " tag1" " tag2" " tag3" " tag4" " tag5" }
130+ set slot_list [put_slot_list $r $slotsize $n $tag_list ]
131+ assert_equal [llength $slot_list ] [llength $tag_list ]
132+ foreach slot $slot_list {
133+ set res [$r slotsdel $slot ]
134+ assert_equal 1 [llength $res ]
135+ assert_equal 2 [llength [lindex $res 0]]
136+ assert_equal $slot [lindex [lindex $res 0] 0]
137+ assert_equal 0 [lindex [lindex $res 0] 1]
138+ }
139+ }
140+ }
141+
142+ proc test_mgrt_cmd {r} {
143+ }
144+
145+ # --------------------- unload -----------------#
146+ proc test_unload {r} {
147+ test " Unload the module - redisxslot" {
148+ set info [r config get enable-module-command]
149+ if { $info eq " enable-module-command yes" } {
150+ assert_equal {OK} [r module unload redisxslot]
151+ puts " unload ok"
152+ }
153+ }
154+ }
155+
156+ # --------------------- start server loadmodule test -------------------#
3157tags " modules" {
4- test {modules are default 1024 slots no async block no thread pool} {
5- start_server [list overrides [list loadmodule " $testmodule " ]] {
6- assert_equal 899 [r slotshashkey " {tag}" ]
158+ test {start redis server loadmodule: default 1024 slots no async block no thread pool} {
159+ start_server [list overrides [list loadmodule " $testmodule " enable-module-command " yes" ]] {
160+ print_module_args r
161+ test_local_cmd r 1024
162+ test_unload r
7163 }
8164 }
9- }
165+
166+ test {start redis server loadmodule: 65536 slots no async block no thread pool} {
167+ start_server [list overrides [list loadmodule " $testmodule 65536" enable-module-command " yes" ]] {
168+ print_module_args r
169+ test_local_cmd r 65536
170+ test_unload r
171+ }
172+ }
173+ }
174+
175+ # ------------ start server and request module load test --------------#
176+ start_server {tags {" modules" }} {
177+ test {cli module load: default 1024 slots no async block no thread pool} {
178+ init_config_1 r
179+ assert_equal {OK} [r module load $testmodule ]
180+ print_module_args r
181+ test_local_cmd r 1024
182+ test_unload r
183+ }
184+ }
0 commit comments