]>
Commit | Line | Data |
---|---|---|
7a932b74 | 1 | # test-redis.tcl |
2 | # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com | |
3 | # This softare is released under the BSD License. See the COPYING file for | |
4 | # more information. | |
ed9b544e | 5 | |
405b0a6a | 6 | set tcl_precision 17 |
f89c3a35 | 7 | source redis.tcl |
43c9dc7b | 8 | |
ed9b544e | 9 | set ::passed 0 |
10 | set ::failed 0 | |
fc77604c | 11 | set ::testnum 0 |
ed9b544e | 12 | |
13 | proc test {name code okpattern} { | |
fc77604c | 14 | incr ::testnum |
15 | if {$::testnum < $::first || $::testnum > $::last} return | |
16 | puts -nonewline [format "%-70s " "#$::testnum $name"] | |
ed9b544e | 17 | flush stdout |
18 | set retval [uplevel 1 $code] | |
19 | if {$okpattern eq $retval || [string match $okpattern $retval]} { | |
20 | puts "PASSED" | |
21 | incr ::passed | |
22 | } else { | |
23 | puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'" | |
24 | incr ::failed | |
25 | } | |
26 | } | |
27 | ||
75398fbc | 28 | proc randstring {min max {type binary}} { |
29 | set len [expr {$min+int(rand()*($max-$min+1))}] | |
30 | set output {} | |
31 | if {$type eq {binary}} { | |
32 | set minval 0 | |
33 | set maxval 255 | |
34 | } elseif {$type eq {alpha}} { | |
35 | set minval 48 | |
36 | set maxval 122 | |
37 | } elseif {$type eq {compr}} { | |
38 | set minval 48 | |
39 | set maxval 52 | |
40 | } | |
41 | while {$len} { | |
42 | append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]] | |
43 | incr len -1 | |
44 | } | |
45 | return $output | |
46 | } | |
47 | ||
d7f43c08 | 48 | # Useful for some test |
49 | proc zlistAlikeSort {a b} { | |
50 | if {[lindex $a 0] > [lindex $b 0]} {return 1} | |
51 | if {[lindex $a 0] < [lindex $b 0]} {return -1} | |
52 | string compare [lindex $a 1] [lindex $b 1] | |
53 | } | |
54 | ||
0d36ded0 | 55 | proc waitForBgsave r { |
56 | while 1 { | |
57 | set i [$r info] | |
58 | if {[string match {*bgsave_in_progress:1*} $i]} { | |
59 | puts -nonewline "\nWaiting for background save to finish... " | |
60 | flush stdout | |
61 | after 1000 | |
62 | } else { | |
63 | break | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
e96e4fbf | 68 | proc waitForBgrewriteaof r { |
69 | while 1 { | |
70 | set i [$r info] | |
71 | if {[string match {*bgrewriteaof_in_progress:1*} $i]} { | |
72 | puts -nonewline "\nWaiting for background AOF rewrite to finish... " | |
73 | flush stdout | |
74 | after 1000 | |
75 | } else { | |
76 | break | |
77 | } | |
78 | } | |
79 | } | |
80 | ||
e054afda | 81 | proc randomInt {max} { |
82 | expr {int(rand()*$max)} | |
83 | } | |
84 | ||
85 | proc randpath args { | |
86 | set path [expr {int(rand()*[llength $args])}] | |
87 | uplevel 1 [lindex $args $path] | |
88 | } | |
89 | ||
90 | proc randomValue {} { | |
91 | randpath { | |
92 | # Small enough to likely collide | |
93 | randomInt 1000 | |
94 | } { | |
95 | # 32 bit compressible signed/unsigned | |
96 | randpath {randomInt 2000000000} {randomInt 4000000000} | |
97 | } { | |
98 | # 64 bit | |
99 | randpath {randomInt 1000000000000} | |
100 | } { | |
101 | # Random string | |
102 | randpath {randstring 0 256 alpha} \ | |
103 | {randstring 0 256 compr} \ | |
104 | {randstring 0 256 binary} | |
105 | } | |
106 | } | |
107 | ||
108 | proc randomKey {} { | |
109 | randpath { | |
110 | # Small enough to likely collide | |
111 | randomInt 1000 | |
112 | } { | |
113 | # 32 bit compressible signed/unsigned | |
114 | randpath {randomInt 2000000000} {randomInt 4000000000} | |
115 | } { | |
116 | # 64 bit | |
117 | randpath {randomInt 1000000000000} | |
118 | } { | |
119 | # Random string | |
120 | randpath {randstring 1 256 alpha} \ | |
121 | {randstring 1 256 compr} | |
122 | } | |
123 | } | |
124 | ||
125 | proc createComplexDataset {r ops} { | |
126 | for {set j 0} {$j < $ops} {incr j} { | |
127 | set k [randomKey] | |
128 | set v [randomValue] | |
129 | set d [expr {rand()}] | |
130 | set t [$r type $k] | |
131 | ||
132 | if {$t eq {none}} { | |
133 | randpath { | |
134 | $r set $k $v | |
135 | } { | |
136 | $r lpush $k $v | |
137 | } { | |
138 | $r sadd $k $v | |
139 | } { | |
140 | $r zadd $k $d $v | |
141 | } | |
142 | set t [$r type $k] | |
143 | } | |
144 | ||
145 | switch $t { | |
146 | {string} { | |
147 | # Nothing to do | |
148 | } | |
149 | {list} { | |
150 | randpath {$r lpush $k $v} \ | |
151 | {$r rpush $k $v} \ | |
152 | {$r lrem $k 0 $v} \ | |
153 | {$r rpop $k} \ | |
154 | {$r lpop $k} | |
155 | } | |
156 | {set} { | |
157 | randpath {$r sadd $k $v} \ | |
158 | {$r srem $k $v} | |
159 | } | |
160 | {zset} { | |
161 | randpath {$r zadd $k $d $v} \ | |
162 | {$r zrem $k $v} | |
163 | } | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | proc datasetDigest r { | |
169 | set keys [lsort [split [$r keys *] " "]] | |
e96e4fbf | 170 | set digest {} |
e054afda | 171 | foreach k $keys { |
172 | set t [$r type $k] | |
e96e4fbf | 173 | switch $t { |
174 | {string} { | |
175 | set aux [::sha1::sha1 -hex [$r get $k]] | |
176 | } {list} { | |
177 | if {[$r llen $k] == 0} { | |
178 | set aux {} | |
179 | } else { | |
180 | set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]] | |
181 | } | |
182 | } {set} { | |
183 | if {[$r scard $k] == 0} { | |
184 | set aux {} | |
185 | } else { | |
186 | set aux [::sha1::sha1 -hex [lsort [$r smembers $k]]] | |
187 | } | |
188 | } {zset} { | |
189 | if {[$r zcard $k] == 0} { | |
190 | set aux {} | |
191 | } else { | |
192 | set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]] | |
193 | } | |
194 | } default { | |
195 | error "Type not supported" | |
196 | } | |
e054afda | 197 | } |
e96e4fbf | 198 | if {$aux eq {}} continue |
199 | set digest [::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]] | |
e054afda | 200 | } |
201 | return $digest | |
202 | } | |
203 | ||
ed9b544e | 204 | proc main {server port} { |
43c9dc7b | 205 | set r [redis $server $port] |
eea4baf7 | 206 | $r select 9 |
43c9dc7b | 207 | set err "" |
fc77604c | 208 | set res "" |
ed9b544e | 209 | |
abcb223e BH |
210 | # The following AUTH test should be enabled only when requirepass |
211 | # <PASSWORD> is set in redis.conf and redis-server was started with | |
212 | # redis.conf as the first argument. | |
213 | ||
214 | #test {AUTH with requirepass in redis.conf} { | |
215 | # $r auth foobared | |
216 | #} {OK} | |
217 | ||
ed9b544e | 218 | test {DEL all keys to start with a clean DB} { |
43c9dc7b | 219 | foreach key [$r keys *] {$r del $key} |
220 | $r dbsize | |
ed9b544e | 221 | } {0} |
222 | ||
223 | test {SET and GET an item} { | |
43c9dc7b | 224 | $r set x foobar |
225 | $r get x | |
ed9b544e | 226 | } {foobar} |
227 | ||
7c49733c | 228 | test {SET and GET an empty item} { |
229 | $r set x {} | |
230 | $r get x | |
231 | } {} | |
232 | ||
ed9b544e | 233 | test {DEL against a single item} { |
43c9dc7b | 234 | $r del x |
235 | $r get x | |
ed9b544e | 236 | } {} |
237 | ||
cc582a77 | 238 | test {Vararg DEL} { |
239 | $r set foo1 a | |
240 | $r set foo2 b | |
241 | $r set foo3 c | |
242 | list [$r del foo1 foo2 foo3 foo4] [$r mget foo1 foo2 foo3] | |
243 | } {3 {{} {} {}}} | |
244 | ||
ed9b544e | 245 | test {KEYS with pattern} { |
246 | foreach key {key_x key_y key_z foo_a foo_b foo_c} { | |
43c9dc7b | 247 | $r set $key hello |
ed9b544e | 248 | } |
43c9dc7b | 249 | lsort [$r keys foo*] |
ed9b544e | 250 | } {foo_a foo_b foo_c} |
251 | ||
252 | test {KEYS to get all keys} { | |
43c9dc7b | 253 | lsort [$r keys *] |
ed9b544e | 254 | } {foo_a foo_b foo_c key_x key_y key_z} |
255 | ||
256 | test {DBSIZE} { | |
43c9dc7b | 257 | $r dbsize |
ed9b544e | 258 | } {6} |
259 | ||
260 | test {DEL all keys} { | |
43c9dc7b | 261 | foreach key [$r keys *] {$r del $key} |
262 | $r dbsize | |
ed9b544e | 263 | } {0} |
264 | ||
265 | test {Very big payload in GET/SET} { | |
266 | set buf [string repeat "abcd" 1000000] | |
43c9dc7b | 267 | $r set foo $buf |
268 | $r get foo | |
ed9b544e | 269 | } [string repeat "abcd" 1000000] |
270 | ||
271 | test {SET 10000 numeric keys and access all them in reverse order} { | |
272 | for {set x 0} {$x < 10000} {incr x} { | |
43c9dc7b | 273 | $r set $x $x |
ed9b544e | 274 | } |
275 | set sum 0 | |
276 | for {set x 9999} {$x >= 0} {incr x -1} { | |
43c9dc7b | 277 | incr sum [$r get $x] |
ed9b544e | 278 | } |
279 | format $sum | |
280 | } {49995000} | |
281 | ||
282 | test {DBSIZE should be 10001 now} { | |
43c9dc7b | 283 | $r dbsize |
ed9b544e | 284 | } {10001} |
285 | ||
286 | test {INCR against non existing key} { | |
287 | set res {} | |
43c9dc7b | 288 | append res [$r incr novar] |
289 | append res [$r get novar] | |
ed9b544e | 290 | } {11} |
291 | ||
292 | test {INCR against key created by incr itself} { | |
43c9dc7b | 293 | $r incr novar |
ed9b544e | 294 | } {2} |
295 | ||
296 | test {INCR against key originally set with SET} { | |
43c9dc7b | 297 | $r set novar 100 |
298 | $r incr novar | |
ed9b544e | 299 | } {101} |
300 | ||
d68ed120 | 301 | test {INCR over 32bit value} { |
302 | $r set novar 17179869184 | |
303 | $r incr novar | |
304 | } {17179869185} | |
305 | ||
306 | test {INCRBY over 32bit value with over 32bit increment} { | |
307 | $r set novar 17179869184 | |
308 | $r incrby novar 17179869184 | |
309 | } {34359738368} | |
310 | ||
311 | test {DECRBY over 32bit value with over 32bit increment, negative res} { | |
312 | $r set novar 17179869184 | |
313 | $r decrby novar 17179869185 | |
314 | } {-1} | |
315 | ||
ed9b544e | 316 | test {SETNX target key missing} { |
43c9dc7b | 317 | $r setnx novar2 foobared |
318 | $r get novar2 | |
ed9b544e | 319 | } {foobared} |
320 | ||
321 | test {SETNX target key exists} { | |
43c9dc7b | 322 | $r setnx novar2 blabla |
323 | $r get novar2 | |
ed9b544e | 324 | } {foobared} |
325 | ||
5acdc75d | 326 | test {SETNX will overwrite EXPIREing key} { |
327 | $r set x 10 | |
328 | $r expire x 10000 | |
329 | $r setnx x 20 | |
330 | $r get x | |
331 | } {20} | |
332 | ||
ed9b544e | 333 | test {EXISTS} { |
334 | set res {} | |
43c9dc7b | 335 | $r set newkey test |
336 | append res [$r exists newkey] | |
337 | $r del newkey | |
338 | append res [$r exists newkey] | |
ed9b544e | 339 | } {10} |
340 | ||
341 | test {Zero length value in key. SET/GET/EXISTS} { | |
43c9dc7b | 342 | $r set emptykey {} |
343 | set res [$r get emptykey] | |
344 | append res [$r exists emptykey] | |
345 | $r del emptykey | |
346 | append res [$r exists emptykey] | |
ed9b544e | 347 | } {10} |
348 | ||
349 | test {Commands pipelining} { | |
43c9dc7b | 350 | set fd [$r channel] |
ed9b544e | 351 | puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n" |
352 | flush $fd | |
353 | set res {} | |
43c9dc7b | 354 | append res [string match OK* [::redis::redis_read_reply $fd]] |
355 | append res [::redis::redis_read_reply $fd] | |
356 | append res [string match PONG* [::redis::redis_read_reply $fd]] | |
ed9b544e | 357 | format $res |
358 | } {1xyzk1} | |
359 | ||
360 | test {Non existing command} { | |
43c9dc7b | 361 | catch {$r foobaredcommand} err |
362 | string match ERR* $err | |
ed9b544e | 363 | } {1} |
364 | ||
365 | test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} { | |
43c9dc7b | 366 | $r lpush mylist a |
367 | $r lpush mylist b | |
368 | $r rpush mylist c | |
369 | set res [$r llen mylist] | |
370 | append res [$r lindex mylist 0] | |
371 | append res [$r lindex mylist 1] | |
372 | append res [$r lindex mylist 2] | |
ed9b544e | 373 | } {3bac} |
374 | ||
375 | test {DEL a list} { | |
43c9dc7b | 376 | $r del mylist |
377 | $r exists mylist | |
ed9b544e | 378 | } {0} |
379 | ||
380 | test {Create a long list and check every single element with LINDEX} { | |
381 | set ok 0 | |
382 | for {set i 0} {$i < 1000} {incr i} { | |
43c9dc7b | 383 | $r rpush mylist $i |
ed9b544e | 384 | } |
385 | for {set i 0} {$i < 1000} {incr i} { | |
43c9dc7b | 386 | if {[$r lindex mylist $i] eq $i} {incr ok} |
387 | if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} { | |
ed9b544e | 388 | incr ok |
389 | } | |
390 | } | |
391 | format $ok | |
392 | } {2000} | |
393 | ||
394 | test {Test elements with LINDEX in random access} { | |
395 | set ok 0 | |
396 | for {set i 0} {$i < 1000} {incr i} { | |
43c9dc7b | 397 | set rint [expr int(rand()*1000)] |
398 | if {[$r lindex mylist $rint] eq $rint} {incr ok} | |
399 | if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} { | |
ed9b544e | 400 | incr ok |
401 | } | |
402 | } | |
403 | format $ok | |
404 | } {2000} | |
405 | ||
210e29f7 | 406 | test {Check if the list is still ok after a DEBUG RELOAD} { |
407 | $r debug reload | |
408 | set ok 0 | |
409 | for {set i 0} {$i < 1000} {incr i} { | |
410 | set rint [expr int(rand()*1000)] | |
411 | if {[$r lindex mylist $rint] eq $rint} {incr ok} | |
412 | if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} { | |
413 | incr ok | |
414 | } | |
415 | } | |
416 | format $ok | |
417 | } {2000} | |
418 | ||
ed9b544e | 419 | test {LLEN against non-list value error} { |
43c9dc7b | 420 | $r del mylist |
421 | $r set mylist foobar | |
422 | catch {$r llen mylist} err | |
423 | format $err | |
c937aa89 | 424 | } {ERR*} |
ed9b544e | 425 | |
426 | test {LINDEX against non-list value error} { | |
43c9dc7b | 427 | catch {$r lindex mylist 0} err |
428 | format $err | |
c937aa89 | 429 | } {ERR*} |
ed9b544e | 430 | |
431 | test {LPUSH against non-list value error} { | |
43c9dc7b | 432 | catch {$r lpush mylist 0} err |
433 | format $err | |
c937aa89 | 434 | } {ERR*} |
ed9b544e | 435 | |
436 | test {RPUSH against non-list value error} { | |
43c9dc7b | 437 | catch {$r rpush mylist 0} err |
438 | format $err | |
c937aa89 | 439 | } {ERR*} |
ed9b544e | 440 | |
c08f1734 | 441 | test {RPOPLPUSH base case} { |
442 | $r del mylist | |
443 | $r rpush mylist a | |
444 | $r rpush mylist b | |
445 | $r rpush mylist c | |
446 | $r rpush mylist d | |
447 | set v1 [$r rpoplpush mylist newlist] | |
448 | set v2 [$r rpoplpush mylist newlist] | |
449 | set l1 [$r lrange mylist 0 -1] | |
450 | set l2 [$r lrange newlist 0 -1] | |
451 | list $v1 $v2 $l1 $l2 | |
452 | } {d c {a b} {c d}} | |
453 | ||
454 | test {RPOPLPUSH with the same list as src and dst} { | |
455 | $r del mylist | |
456 | $r rpush mylist a | |
457 | $r rpush mylist b | |
458 | $r rpush mylist c | |
459 | set l1 [$r lrange mylist 0 -1] | |
460 | set v [$r rpoplpush mylist mylist] | |
461 | set l2 [$r lrange mylist 0 -1] | |
462 | list $l1 $v $l2 | |
463 | } {{a b c} c {c a b}} | |
464 | ||
465 | test {RPOPLPUSH target list already exists} { | |
466 | $r del mylist | |
467 | $r del newlist | |
468 | $r rpush mylist a | |
469 | $r rpush mylist b | |
470 | $r rpush mylist c | |
471 | $r rpush mylist d | |
472 | $r rpush newlist x | |
473 | set v1 [$r rpoplpush mylist newlist] | |
474 | set v2 [$r rpoplpush mylist newlist] | |
475 | set l1 [$r lrange mylist 0 -1] | |
476 | set l2 [$r lrange newlist 0 -1] | |
477 | list $v1 $v2 $l1 $l2 | |
478 | } {d c {a b} {c d x}} | |
479 | ||
b9bb7ba2 | 480 | test {RPOPLPUSH against non existing key} { |
481 | $r del mylist | |
482 | $r del newlist | |
483 | set v1 [$r rpoplpush mylist newlist] | |
484 | list $v1 [$r exists mylist] [$r exists newlist] | |
485 | } {{} 0 0} | |
486 | ||
487 | test {RPOPLPUSH against non list src key} { | |
488 | $r del mylist | |
489 | $r del newlist | |
490 | $r set mylist x | |
491 | catch {$r rpoplpush mylist newlist} err | |
492 | list [$r type mylist] [$r exists newlist] [string range $err 0 2] | |
493 | } {string 0 ERR} | |
494 | ||
495 | test {RPOPLPUSH against non list dst key} { | |
496 | $r del mylist | |
497 | $r del newlist | |
498 | $r rpush mylist a | |
499 | $r rpush mylist b | |
500 | $r rpush mylist c | |
501 | $r rpush mylist d | |
502 | $r set newlist x | |
503 | catch {$r rpoplpush mylist newlist} err | |
504 | list [$r lrange mylist 0 -1] [$r type newlist] [string range $err 0 2] | |
505 | } {{a b c d} string ERR} | |
506 | ||
ed9b544e | 507 | test {RENAME basic usage} { |
43c9dc7b | 508 | $r set mykey hello |
509 | $r rename mykey mykey1 | |
510 | $r rename mykey1 mykey2 | |
511 | $r get mykey2 | |
ed9b544e | 512 | } {hello} |
513 | ||
514 | test {RENAME source key should no longer exist} { | |
43c9dc7b | 515 | $r exists mykey |
ed9b544e | 516 | } {0} |
517 | ||
518 | test {RENAME against already existing key} { | |
43c9dc7b | 519 | $r set mykey a |
520 | $r set mykey2 b | |
521 | $r rename mykey2 mykey | |
522 | set res [$r get mykey] | |
523 | append res [$r exists mykey2] | |
ed9b544e | 524 | } {b0} |
525 | ||
526 | test {RENAMENX basic usage} { | |
43c9dc7b | 527 | $r del mykey |
528 | $r del mykey2 | |
529 | $r set mykey foobar | |
530 | $r renamenx mykey mykey2 | |
531 | set res [$r get mykey2] | |
532 | append res [$r exists mykey] | |
ed9b544e | 533 | } {foobar0} |
534 | ||
535 | test {RENAMENX against already existing key} { | |
43c9dc7b | 536 | $r set mykey foo |
537 | $r set mykey2 bar | |
538 | $r renamenx mykey mykey2 | |
ed9b544e | 539 | } {0} |
540 | ||
541 | test {RENAMENX against already existing key (2)} { | |
43c9dc7b | 542 | set res [$r get mykey] |
543 | append res [$r get mykey2] | |
ed9b544e | 544 | } {foobar} |
545 | ||
546 | test {RENAME against non existing source key} { | |
43c9dc7b | 547 | catch {$r rename nokey foobar} err |
548 | format $err | |
c937aa89 | 549 | } {ERR*} |
ed9b544e | 550 | |
551 | test {RENAME where source and dest key is the same} { | |
43c9dc7b | 552 | catch {$r rename mykey mykey} err |
553 | format $err | |
c937aa89 | 554 | } {ERR*} |
ed9b544e | 555 | |
556 | test {DEL all keys again (DB 0)} { | |
43c9dc7b | 557 | foreach key [$r keys *] { |
558 | $r del $key | |
ed9b544e | 559 | } |
43c9dc7b | 560 | $r dbsize |
ed9b544e | 561 | } {0} |
562 | ||
563 | test {DEL all keys again (DB 1)} { | |
eea4baf7 | 564 | $r select 10 |
43c9dc7b | 565 | foreach key [$r keys *] { |
566 | $r del $key | |
ed9b544e | 567 | } |
43c9dc7b | 568 | set res [$r dbsize] |
eea4baf7 | 569 | $r select 9 |
ed9b544e | 570 | format $res |
571 | } {0} | |
572 | ||
573 | test {MOVE basic usage} { | |
43c9dc7b | 574 | $r set mykey foobar |
eea4baf7 | 575 | $r move mykey 10 |
ed9b544e | 576 | set res {} |
43c9dc7b | 577 | lappend res [$r exists mykey] |
578 | lappend res [$r dbsize] | |
eea4baf7 | 579 | $r select 10 |
43c9dc7b | 580 | lappend res [$r get mykey] |
581 | lappend res [$r dbsize] | |
eea4baf7 | 582 | $r select 9 |
ed9b544e | 583 | format $res |
584 | } [list 0 0 foobar 1] | |
585 | ||
586 | test {MOVE against key existing in the target DB} { | |
43c9dc7b | 587 | $r set mykey hello |
eea4baf7 | 588 | $r move mykey 10 |
ed9b544e | 589 | } {0} |
590 | ||
591 | test {SET/GET keys in different DBs} { | |
43c9dc7b | 592 | $r set a hello |
593 | $r set b world | |
eea4baf7 | 594 | $r select 10 |
43c9dc7b | 595 | $r set a foo |
596 | $r set b bared | |
eea4baf7 | 597 | $r select 9 |
ed9b544e | 598 | set res {} |
43c9dc7b | 599 | lappend res [$r get a] |
600 | lappend res [$r get b] | |
eea4baf7 | 601 | $r select 10 |
43c9dc7b | 602 | lappend res [$r get a] |
603 | lappend res [$r get b] | |
eea4baf7 | 604 | $r select 9 |
ed9b544e | 605 | format $res |
606 | } {hello world foo bared} | |
607 | ||
608 | test {Basic LPOP/RPOP} { | |
43c9dc7b | 609 | $r del mylist |
610 | $r rpush mylist 1 | |
611 | $r rpush mylist 2 | |
612 | $r lpush mylist 0 | |
613 | list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist] | |
ed9b544e | 614 | } [list 0 2 1 0] |
615 | ||
616 | test {LPOP/RPOP against empty list} { | |
43c9dc7b | 617 | $r lpop mylist |
ed9b544e | 618 | } {} |
619 | ||
620 | test {LPOP against non list value} { | |
43c9dc7b | 621 | $r set notalist foo |
622 | catch {$r lpop notalist} err | |
623 | format $err | |
c937aa89 | 624 | } {ERR*kind*} |
ed9b544e | 625 | |
626 | test {Mass LPUSH/LPOP} { | |
627 | set sum 0 | |
628 | for {set i 0} {$i < 1000} {incr i} { | |
43c9dc7b | 629 | $r lpush mylist $i |
ed9b544e | 630 | incr sum $i |
631 | } | |
632 | set sum2 0 | |
633 | for {set i 0} {$i < 500} {incr i} { | |
43c9dc7b | 634 | incr sum2 [$r lpop mylist] |
635 | incr sum2 [$r rpop mylist] | |
ed9b544e | 636 | } |
637 | expr $sum == $sum2 | |
638 | } {1} | |
639 | ||
640 | test {LRANGE basics} { | |
641 | for {set i 0} {$i < 10} {incr i} { | |
43c9dc7b | 642 | $r rpush mylist $i |
ed9b544e | 643 | } |
43c9dc7b | 644 | list [$r lrange mylist 1 -2] \ |
645 | [$r lrange mylist -3 -1] \ | |
646 | [$r lrange mylist 4 4] | |
ed9b544e | 647 | } {{1 2 3 4 5 6 7 8} {7 8 9} 4} |
648 | ||
649 | test {LRANGE inverted indexes} { | |
43c9dc7b | 650 | $r lrange mylist 6 2 |
ed9b544e | 651 | } {} |
652 | ||
653 | test {LRANGE out of range indexes including the full list} { | |
43c9dc7b | 654 | $r lrange mylist -1000 1000 |
ed9b544e | 655 | } {0 1 2 3 4 5 6 7 8 9} |
656 | ||
657 | test {LRANGE against non existing key} { | |
43c9dc7b | 658 | $r lrange nosuchkey 0 1 |
ed9b544e | 659 | } {} |
660 | ||
661 | test {LTRIM basics} { | |
43c9dc7b | 662 | $r del mylist |
ed9b544e | 663 | for {set i 0} {$i < 100} {incr i} { |
43c9dc7b | 664 | $r lpush mylist $i |
665 | $r ltrim mylist 0 4 | |
ed9b544e | 666 | } |
43c9dc7b | 667 | $r lrange mylist 0 -1 |
ed9b544e | 668 | } {99 98 97 96 95} |
669 | ||
670 | test {LSET} { | |
43c9dc7b | 671 | $r lset mylist 1 foo |
672 | $r lset mylist -1 bar | |
673 | $r lrange mylist 0 -1 | |
ed9b544e | 674 | } {99 foo 97 96 bar} |
675 | ||
676 | test {LSET out of range index} { | |
43c9dc7b | 677 | catch {$r lset mylist 10 foo} err |
678 | format $err | |
c937aa89 | 679 | } {ERR*range*} |
ed9b544e | 680 | |
681 | test {LSET against non existing key} { | |
43c9dc7b | 682 | catch {$r lset nosuchkey 10 foo} err |
683 | format $err | |
c937aa89 | 684 | } {ERR*key*} |
ed9b544e | 685 | |
686 | test {LSET against non list value} { | |
43c9dc7b | 687 | $r set nolist foobar |
688 | catch {$r lset nolist 0 foo} err | |
689 | format $err | |
c937aa89 | 690 | } {ERR*value*} |
ed9b544e | 691 | |
692 | test {SADD, SCARD, SISMEMBER, SMEMBERS basics} { | |
43c9dc7b | 693 | $r sadd myset foo |
694 | $r sadd myset bar | |
695 | list [$r scard myset] [$r sismember myset foo] \ | |
696 | [$r sismember myset bar] [$r sismember myset bla] \ | |
697 | [lsort [$r smembers myset]] | |
ed9b544e | 698 | } {2 1 1 0 {bar foo}} |
699 | ||
700 | test {SADD adding the same element multiple times} { | |
43c9dc7b | 701 | $r sadd myset foo |
702 | $r sadd myset foo | |
703 | $r sadd myset foo | |
704 | $r scard myset | |
ed9b544e | 705 | } {2} |
706 | ||
707 | test {SADD against non set} { | |
43c9dc7b | 708 | catch {$r sadd mylist foo} err |
709 | format $err | |
c937aa89 | 710 | } {ERR*kind*} |
ed9b544e | 711 | |
712 | test {SREM basics} { | |
43c9dc7b | 713 | $r sadd myset ciao |
714 | $r srem myset foo | |
715 | lsort [$r smembers myset] | |
ed9b544e | 716 | } {bar ciao} |
717 | ||
718 | test {Mass SADD and SINTER with two sets} { | |
719 | for {set i 0} {$i < 1000} {incr i} { | |
43c9dc7b | 720 | $r sadd set1 $i |
721 | $r sadd set2 [expr $i+995] | |
ed9b544e | 722 | } |
43c9dc7b | 723 | lsort [$r sinter set1 set2] |
ed9b544e | 724 | } {995 996 997 998 999} |
40d224a9 | 725 | |
726 | test {SUNION with two sets} { | |
727 | lsort [$r sunion set1 set2] | |
728 | } [lsort -uniq "[$r smembers set1] [$r smembers set2]"] | |
ed9b544e | 729 | |
730 | test {SINTERSTORE with two sets} { | |
43c9dc7b | 731 | $r sinterstore setres set1 set2 |
732 | lsort [$r smembers setres] | |
ed9b544e | 733 | } {995 996 997 998 999} |
734 | ||
210e29f7 | 735 | test {SINTERSTORE with two sets, after a DEBUG RELOAD} { |
736 | $r debug reload | |
737 | $r sinterstore setres set1 set2 | |
738 | lsort [$r smembers setres] | |
739 | } {995 996 997 998 999} | |
740 | ||
40d224a9 | 741 | test {SUNIONSTORE with two sets} { |
742 | $r sunionstore setres set1 set2 | |
743 | lsort [$r smembers setres] | |
744 | } [lsort -uniq "[$r smembers set1] [$r smembers set2]"] | |
745 | ||
f4d9b3c6 | 746 | test {SUNIONSTORE against non existing keys} { |
747 | $r set setres xxx | |
748 | list [$r sunionstore setres foo111 bar222] [$r exists xxx] | |
749 | } {0 0} | |
750 | ||
ed9b544e | 751 | test {SINTER against three sets} { |
43c9dc7b | 752 | $r sadd set3 999 |
753 | $r sadd set3 995 | |
754 | $r sadd set3 1000 | |
755 | $r sadd set3 2000 | |
756 | lsort [$r sinter set1 set2 set3] | |
ed9b544e | 757 | } {995 999} |
758 | ||
759 | test {SINTERSTORE with three sets} { | |
43c9dc7b | 760 | $r sinterstore setres set1 set2 set3 |
761 | lsort [$r smembers setres] | |
ed9b544e | 762 | } {995 999} |
40d224a9 | 763 | |
764 | test {SUNION with non existing keys} { | |
765 | lsort [$r sunion nokey1 set1 set2 nokey2] | |
766 | } [lsort -uniq "[$r smembers set1] [$r smembers set2]"] | |
767 | ||
f4f56e1d | 768 | test {SDIFF with two sets} { |
769 | for {set i 5} {$i < 1000} {incr i} { | |
770 | $r sadd set4 $i | |
771 | } | |
772 | lsort [$r sdiff set1 set4] | |
773 | } {0 1 2 3 4} | |
774 | ||
775 | test {SDIFF with three sets} { | |
776 | $r sadd set5 0 | |
777 | lsort [$r sdiff set1 set4 set5] | |
778 | } {1 2 3 4} | |
779 | ||
780 | test {SDIFFSTORE with three sets} { | |
781 | $r sdiffstore sres set1 set4 set5 | |
782 | lsort [$r smembers sres] | |
783 | } {1 2 3 4} | |
784 | ||
12fea928 | 785 | test {SPOP basics} { |
786 | $r del myset | |
787 | $r sadd myset 1 | |
788 | $r sadd myset 2 | |
789 | $r sadd myset 3 | |
790 | list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset] | |
791 | } {{1 2 3} 0} | |
792 | ||
ed9b544e | 793 | test {SAVE - make sure there are all the types as values} { |
bbf44ecf | 794 | # Wait for a background saving in progress to terminate |
0d36ded0 | 795 | waitForBgsave $r |
43c9dc7b | 796 | $r lpush mysavelist hello |
797 | $r lpush mysavelist world | |
798 | $r set myemptykey {} | |
799 | $r set mynormalkey {blablablba} | |
d7f43c08 | 800 | $r zadd mytestzset a 10 |
801 | $r zadd mytestzset b 20 | |
802 | $r zadd mytestzset c 30 | |
43c9dc7b | 803 | $r save |
c937aa89 | 804 | } {OK} |
6cbfd2b3 | 805 | |
806 | test {SRANDMEMBER} { | |
807 | $r del myset | |
808 | $r sadd myset a | |
809 | $r sadd myset b | |
810 | $r sadd myset c | |
811 | unset -nocomplain myset | |
812 | array set myset {} | |
813 | for {set i 0} {$i < 100} {incr i} { | |
814 | set myset([$r srandmember myset]) 1 | |
815 | } | |
816 | lsort [array names myset] | |
817 | } {a b c} | |
ed9b544e | 818 | |
819 | test {Create a random list} { | |
820 | set tosort {} | |
821 | array set seenrand {} | |
822 | for {set i 0} {$i < 10000} {incr i} { | |
823 | while 1 { | |
824 | # Make sure all the weights are different because | |
825 | # Redis does not use a stable sort but Tcl does. | |
43c9dc7b | 826 | set rint [expr int(rand()*1000000)] |
827 | if {![info exists seenrand($rint)]} break | |
ed9b544e | 828 | } |
43c9dc7b | 829 | set seenrand($rint) x |
830 | $r lpush tosort $i | |
831 | $r set weight_$i $rint | |
832 | lappend tosort [list $i $rint] | |
ed9b544e | 833 | } |
834 | set sorted [lsort -index 1 -real $tosort] | |
835 | set res {} | |
836 | for {set i 0} {$i < 10000} {incr i} { | |
837 | lappend res [lindex $sorted $i 0] | |
838 | } | |
839 | format {} | |
840 | } {} | |
841 | ||
842 | test {SORT with BY against the newly created list} { | |
43c9dc7b | 843 | $r sort tosort {BY weight_*} |
ed9b544e | 844 | } $res |
845 | ||
846 | test {SORT direct, numeric, against the newly created list} { | |
43c9dc7b | 847 | $r sort tosort |
ed9b544e | 848 | } [lsort -integer $res] |
849 | ||
850 | test {SORT decreasing sort} { | |
43c9dc7b | 851 | $r sort tosort {DESC} |
ed9b544e | 852 | } [lsort -decreasing -integer $res] |
853 | ||
854 | test {SORT speed, sorting 10000 elements list using BY, 100 times} { | |
855 | set start [clock clicks -milliseconds] | |
856 | for {set i 0} {$i < 100} {incr i} { | |
43c9dc7b | 857 | set sorted [$r sort tosort {BY weight_* LIMIT 0 10}] |
ed9b544e | 858 | } |
859 | set elapsed [expr [clock clicks -milliseconds]-$start] | |
860 | puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds " | |
861 | flush stdout | |
862 | format {} | |
863 | } {} | |
864 | ||
865 | test {SORT speed, sorting 10000 elements list directly, 100 times} { | |
866 | set start [clock clicks -milliseconds] | |
867 | for {set i 0} {$i < 100} {incr i} { | |
43c9dc7b | 868 | set sorted [$r sort tosort {LIMIT 0 10}] |
ed9b544e | 869 | } |
870 | set elapsed [expr [clock clicks -milliseconds]-$start] | |
871 | puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds " | |
872 | flush stdout | |
873 | format {} | |
874 | } {} | |
875 | ||
876 | test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} { | |
877 | set start [clock clicks -milliseconds] | |
878 | for {set i 0} {$i < 100} {incr i} { | |
43c9dc7b | 879 | set sorted [$r sort tosort {BY nokey LIMIT 0 10}] |
ed9b544e | 880 | } |
881 | set elapsed [expr [clock clicks -milliseconds]-$start] | |
882 | puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds " | |
883 | flush stdout | |
884 | format {} | |
885 | } {} | |
886 | ||
887 | test {SORT regression for issue #19, sorting floats} { | |
43c9dc7b | 888 | $r flushdb |
ed9b544e | 889 | foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} { |
43c9dc7b | 890 | $r lpush mylist $x |
ed9b544e | 891 | } |
43c9dc7b | 892 | $r sort mylist |
ed9b544e | 893 | } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}] |
894 | ||
28173a49 | 895 | test {SORT with GET #} { |
896 | $r del mylist | |
897 | $r lpush mylist 1 | |
898 | $r lpush mylist 2 | |
899 | $r lpush mylist 3 | |
900 | $r mset weight_1 10 weight_2 5 weight_3 30 | |
901 | $r sort mylist BY weight_* GET # | |
902 | } {2 1 3} | |
903 | ||
ed9b544e | 904 | test {LREM, remove all the occurrences} { |
eea4baf7 | 905 | $r flushdb |
43c9dc7b | 906 | $r rpush mylist foo |
907 | $r rpush mylist bar | |
908 | $r rpush mylist foobar | |
909 | $r rpush mylist foobared | |
910 | $r rpush mylist zap | |
911 | $r rpush mylist bar | |
912 | $r rpush mylist test | |
913 | $r rpush mylist foo | |
914 | set res [$r lrem mylist 0 bar] | |
915 | list [$r lrange mylist 0 -1] $res | |
ed9b544e | 916 | } {{foo foobar foobared zap test foo} 2} |
917 | ||
918 | test {LREM, remove the first occurrence} { | |
43c9dc7b | 919 | set res [$r lrem mylist 1 foo] |
920 | list [$r lrange mylist 0 -1] $res | |
ed9b544e | 921 | } {{foobar foobared zap test foo} 1} |
922 | ||
923 | test {LREM, remove non existing element} { | |
43c9dc7b | 924 | set res [$r lrem mylist 1 nosuchelement] |
925 | list [$r lrange mylist 0 -1] $res | |
ed9b544e | 926 | } {{foobar foobared zap test foo} 0} |
927 | ||
928 | test {LREM, starting from tail with negative count} { | |
eea4baf7 | 929 | $r flushdb |
43c9dc7b | 930 | $r rpush mylist foo |
931 | $r rpush mylist bar | |
932 | $r rpush mylist foobar | |
933 | $r rpush mylist foobared | |
934 | $r rpush mylist zap | |
935 | $r rpush mylist bar | |
936 | $r rpush mylist test | |
937 | $r rpush mylist foo | |
938 | $r rpush mylist foo | |
939 | set res [$r lrem mylist -1 bar] | |
940 | list [$r lrange mylist 0 -1] $res | |
ed9b544e | 941 | } {{foo bar foobar foobared zap test foo foo} 1} |
942 | ||
943 | test {LREM, starting from tail with negative count (2)} { | |
43c9dc7b | 944 | set res [$r lrem mylist -2 foo] |
945 | list [$r lrange mylist 0 -1] $res | |
ed9b544e | 946 | } {{foo bar foobar foobared zap test} 2} |
947 | ||
724a51b1 | 948 | test {LREM, deleting objects that may be encoded as integers} { |
949 | $r lpush myotherlist 1 | |
950 | $r lpush myotherlist 2 | |
951 | $r lpush myotherlist 3 | |
952 | $r lrem myotherlist 1 2 | |
953 | $r llen myotherlist | |
954 | } {2} | |
955 | ||
5b19bd72 | 956 | test {MGET} { |
eea4baf7 | 957 | $r flushdb |
43c9dc7b | 958 | $r set foo BAR |
959 | $r set bar FOO | |
960 | $r mget foo bar | |
5b19bd72 | 961 | } {BAR FOO} |
962 | ||
963 | test {MGET against non existing key} { | |
43c9dc7b | 964 | $r mget foo baazz bar |
5b19bd72 | 965 | } {BAR {} FOO} |
966 | ||
967 | test {MGET against non-string key} { | |
43c9dc7b | 968 | $r sadd myset ciao |
969 | $r sadd myset bau | |
970 | $r mget foo baazz bar myset | |
5b19bd72 | 971 | } {BAR {} FOO {}} |
972 | ||
ce7bef07 | 973 | test {RANDOMKEY} { |
eea4baf7 | 974 | $r flushdb |
ce7bef07 | 975 | $r set foo x |
976 | $r set bar y | |
977 | set foo_seen 0 | |
978 | set bar_seen 0 | |
979 | for {set i 0} {$i < 100} {incr i} { | |
980 | set rkey [$r randomkey] | |
981 | if {$rkey eq {foo}} { | |
982 | set foo_seen 1 | |
983 | } | |
984 | if {$rkey eq {bar}} { | |
985 | set bar_seen 1 | |
986 | } | |
987 | } | |
988 | list $foo_seen $bar_seen | |
989 | } {1 1} | |
990 | ||
991 | test {RANDOMKEY against empty DB} { | |
eea4baf7 | 992 | $r flushdb |
ce7bef07 | 993 | $r randomkey |
994 | } {} | |
995 | ||
f5785ae9 | 996 | test {RANDOMKEY regression 1} { |
eea4baf7 | 997 | $r flushdb |
f5785ae9 | 998 | $r set x 10 |
999 | $r del x | |
1000 | $r randomkey | |
1001 | } {} | |
1002 | ||
7ac6d461 | 1003 | test {GETSET (set new value)} { |
1004 | list [$r getset foo xyz] [$r get foo] | |
1005 | } {{} xyz} | |
1006 | ||
1007 | test {GETSET (replace old value)} { | |
1008 | $r set foo bar | |
1009 | list [$r getset foo xyz] [$r get foo] | |
1010 | } {bar xyz} | |
1011 | ||
0eeb2a4b | 1012 | test {SMOVE basics} { |
1013 | $r sadd myset1 a | |
1014 | $r sadd myset1 b | |
1015 | $r sadd myset1 c | |
1016 | $r sadd myset2 x | |
1017 | $r sadd myset2 y | |
1018 | $r sadd myset2 z | |
1019 | $r smove myset1 myset2 a | |
1020 | list [lsort [$r smembers myset2]] [lsort [$r smembers myset1]] | |
1021 | } {{a x y z} {b c}} | |
1022 | ||
1023 | test {SMOVE non existing key} { | |
1024 | list [$r smove myset1 myset2 foo] [lsort [$r smembers myset2]] [lsort [$r smembers myset1]] | |
1025 | } {0 {a x y z} {b c}} | |
1026 | ||
1027 | test {SMOVE non existing src set} { | |
1028 | list [$r smove noset myset2 foo] [lsort [$r smembers myset2]] | |
1029 | } {0 {a x y z}} | |
1030 | ||
1031 | test {SMOVE non existing dst set} { | |
1032 | list [$r smove myset2 myset3 y] [lsort [$r smembers myset2]] [lsort [$r smembers myset3]] | |
1033 | } {1 {a x z} y} | |
1034 | ||
1035 | test {SMOVE wrong src key type} { | |
1036 | $r set x 10 | |
1037 | catch {$r smove x myset2 foo} err | |
1038 | format $err | |
1039 | } {ERR*} | |
1040 | ||
1041 | test {SMOVE wrong dst key type} { | |
1042 | $r set x 10 | |
1043 | catch {$r smove myset2 x foo} err | |
1044 | format $err | |
1045 | } {ERR*} | |
1046 | ||
f69f2cba | 1047 | test {MSET base case} { |
1048 | $r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n" | |
1049 | $r mget x y z | |
1050 | } [list 10 {foo bar} "x x x x x x x\n\n\r\n"] | |
1051 | ||
1052 | test {MSET wrong number of args} { | |
1053 | catch {$r mset x 10 y "foo bar" z} err | |
1054 | format $err | |
1055 | } {*wrong number*} | |
1056 | ||
1057 | test {MSETNX with already existent key} { | |
1058 | list [$r msetnx x1 xxx y2 yyy x 20] [$r exists x1] [$r exists y2] | |
1059 | } {0 0 0} | |
1060 | ||
1061 | test {MSETNX with not existing keys} { | |
1062 | list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2] | |
1063 | } {1 xxx yyy} | |
1064 | ||
5acdc75d | 1065 | test {MSETNX should remove all the volatile keys even on failure} { |
1066 | $r mset x 1 y 2 z 3 | |
1067 | $r expire y 10000 | |
1068 | $r expire z 10000 | |
1069 | list [$r msetnx x A y B z C] [$r mget x y z] | |
1070 | } {0 {1 {} {}}} | |
1071 | ||
d7f43c08 | 1072 | test {ZSET basic ZADD and score update} { |
1073 | $r zadd ztmp 10 x | |
1074 | $r zadd ztmp 20 y | |
1075 | $r zadd ztmp 30 z | |
1076 | set aux1 [$r zrange ztmp 0 -1] | |
1077 | $r zadd ztmp 1 y | |
1078 | set aux2 [$r zrange ztmp 0 -1] | |
1079 | list $aux1 $aux2 | |
1080 | } {{x y z} {y x z}} | |
1081 | ||
fc77604c | 1082 | test {ZCARD basics} { |
1083 | $r zcard ztmp | |
1084 | } {3} | |
1085 | ||
1086 | test {ZCARD non existing key} { | |
1087 | $r zcard ztmp-blabla | |
1088 | } {0} | |
1089 | ||
d7f43c08 | 1090 | test {ZSCORE} { |
eaa256ad | 1091 | set aux {} |
1092 | set err {} | |
1093 | for {set i 0} {$i < 1000} {incr i} { | |
1094 | set score [expr rand()] | |
1095 | lappend aux $score | |
1096 | $r zadd zscoretest $score $i | |
1097 | } | |
1098 | for {set i 0} {$i < 1000} {incr i} { | |
1099 | if {[$r zscore zscoretest $i] != [lindex $aux $i]} { | |
1100 | set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i" | |
1101 | break | |
1102 | } | |
1103 | } | |
1104 | set _ $err | |
1105 | } {} | |
d7f43c08 | 1106 | |
210e29f7 | 1107 | test {ZSCORE after a DEBUG RELOAD} { |
1108 | set aux {} | |
1109 | set err {} | |
1110 | $r del zscoretest | |
1111 | for {set i 0} {$i < 1000} {incr i} { | |
1112 | set score [expr rand()] | |
1113 | lappend aux $score | |
1114 | $r zadd zscoretest $score $i | |
1115 | } | |
1116 | $r debug reload | |
1117 | for {set i 0} {$i < 1000} {incr i} { | |
1118 | if {[$r zscore zscoretest $i] != [lindex $aux $i]} { | |
1119 | set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i" | |
1120 | break | |
1121 | } | |
1122 | } | |
1123 | set _ $err | |
1124 | } {} | |
1125 | ||
d7f43c08 | 1126 | test {ZRANGE and ZREVRANGE} { |
1127 | list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1] | |
1128 | } {{y x z} {z x y}} | |
1129 | ||
1130 | test {ZSETs stress tester - sorting is working well?} { | |
1131 | set delta 0 | |
1132 | for {set test 0} {$test < 2} {incr test} { | |
1133 | unset -nocomplain auxarray | |
1134 | array set auxarray {} | |
1135 | set auxlist {} | |
1136 | $r del myzset | |
1137 | for {set i 0} {$i < 1000} {incr i} { | |
1138 | if {$test == 0} { | |
1139 | set score [expr rand()] | |
1140 | } else { | |
1141 | set score [expr int(rand()*10)] | |
1142 | } | |
1143 | set auxarray($i) $score | |
1144 | $r zadd myzset $score $i | |
1145 | # Random update | |
1146 | if {[expr rand()] < .2} { | |
1147 | set j [expr int(rand()*1000)] | |
1148 | if {$test == 0} { | |
1149 | set score [expr rand()] | |
1150 | } else { | |
1151 | set score [expr int(rand()*10)] | |
1152 | } | |
1153 | set auxarray($j) $score | |
1154 | $r zadd myzset $score $j | |
1155 | } | |
1156 | } | |
1157 | foreach {item score} [array get auxarray] { | |
1158 | lappend auxlist [list $score $item] | |
1159 | } | |
1160 | set sorted [lsort -command zlistAlikeSort $auxlist] | |
1161 | set auxlist {} | |
1162 | foreach x $sorted { | |
1163 | lappend auxlist [lindex $x 1] | |
1164 | } | |
1165 | set fromredis [$r zrange myzset 0 -1] | |
1166 | set delta 0 | |
1167 | for {set i 0} {$i < [llength $fromredis]} {incr i} { | |
1168 | if {[lindex $fromredis $i] != [lindex $auxlist $i]} { | |
1169 | incr delta | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | format $delta | |
1174 | } {0} | |
1175 | ||
28173a49 | 1176 | test {ZINCRBY - can create a new sorted set} { |
1177 | $r del zset | |
1178 | $r zincrby zset 1 foo | |
1179 | list [$r zrange zset 0 -1] [$r zscore zset foo] | |
1180 | } {foo 1} | |
1181 | ||
1182 | test {ZINCRBY - increment and decrement} { | |
1183 | $r zincrby zset 2 foo | |
1184 | $r zincrby zset 1 bar | |
1185 | set v1 [$r zrange zset 0 -1] | |
1186 | $r zincrby zset 10 bar | |
1187 | $r zincrby zset -5 foo | |
1188 | $r zincrby zset -5 bar | |
1189 | set v2 [$r zrange zset 0 -1] | |
1190 | list $v1 $v2 [$r zscore zset foo] [$r zscore zset bar] | |
1191 | } {{bar foo} {foo bar} -2 6} | |
1192 | ||
c74e7c77 | 1193 | test {ZRANGEBYSCORE basics} { |
1194 | $r del zset | |
1195 | $r zadd zset 1 a | |
1196 | $r zadd zset 2 b | |
1197 | $r zadd zset 3 c | |
1198 | $r zadd zset 4 d | |
1199 | $r zadd zset 5 e | |
1200 | $r zrangebyscore zset 2 4 | |
1201 | } {b c d} | |
1202 | ||
5b1207c6 | 1203 | test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} { |
1204 | set err {} | |
1205 | $r del zset | |
1206 | for {set i 0} {$i < 1000} {incr i} { | |
1207 | $r zadd zset [expr rand()] $i | |
1208 | } | |
1209 | for {set i 0} {$i < 100} {incr i} { | |
1210 | set min [expr rand()] | |
1211 | set max [expr rand()] | |
1212 | if {$min > $max} { | |
1213 | set aux $min | |
1214 | set min $max | |
1215 | set max $aux | |
1216 | } | |
1217 | set low [$r zrangebyscore zset -inf $min] | |
1218 | set ok [$r zrangebyscore zset $min $max] | |
1219 | set high [$r zrangebyscore zset $max +inf] | |
1220 | foreach x $low { | |
1221 | set score [$r zscore zset $x] | |
1222 | if {$score > $min} { | |
1223 | append err "Error, score for $x is $score > $min\n" | |
1224 | } | |
1225 | } | |
1226 | foreach x $ok { | |
1227 | set score [$r zscore zset $x] | |
1228 | if {$score < $min || $score > $max} { | |
1229 | append err "Error, score for $x is $score outside $min-$max range\n" | |
1230 | } | |
1231 | } | |
1232 | foreach x $high { | |
1233 | set score [$r zscore zset $x] | |
1234 | if {$score < $max} { | |
1235 | append err "Error, score for $x is $score < $max\n" | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | set _ $err | |
1240 | } {} | |
1241 | ||
0b13687c | 1242 | test {ZRANGEBYSCORE with LIMIT} { |
1243 | $r del zset | |
1244 | $r zadd zset 1 a | |
1245 | $r zadd zset 2 b | |
1246 | $r zadd zset 3 c | |
1247 | $r zadd zset 4 d | |
1248 | $r zadd zset 5 e | |
1249 | list \ | |
1250 | [$r zrangebyscore zset 0 10 LIMIT 0 2] \ | |
1251 | [$r zrangebyscore zset 0 10 LIMIT 2 3] \ | |
1252 | [$r zrangebyscore zset 0 10 LIMIT 2 10] \ | |
1253 | [$r zrangebyscore zset 0 10 LIMIT 20 10] | |
1254 | } {{a b} {c d e} {c d e} {}} | |
1255 | ||
9c21a518 | 1256 | test {ZREMRANGE basics} { |
1257 | $r del zset | |
1258 | $r zadd zset 1 a | |
1259 | $r zadd zset 2 b | |
1260 | $r zadd zset 3 c | |
1261 | $r zadd zset 4 d | |
1262 | $r zadd zset 5 e | |
1263 | list [$r zremrangebyscore zset 2 4] [$r zrange zset 0 -1] | |
1264 | } {3 {a e}} | |
1265 | ||
1266 | test {ZREMRANGE from -inf to +inf} { | |
1267 | $r del zset | |
1268 | $r zadd zset 1 a | |
1269 | $r zadd zset 2 b | |
1270 | $r zadd zset 3 c | |
1271 | $r zadd zset 4 d | |
1272 | $r zadd zset 5 e | |
1273 | list [$r zremrangebyscore zset -inf +inf] [$r zrange zset 0 -1] | |
1274 | } {5 {}} | |
1275 | ||
0b13687c | 1276 | test {SORT against sorted sets} { |
1277 | $r del zset | |
1278 | $r zadd zset 1 a | |
1279 | $r zadd zset 5 b | |
1280 | $r zadd zset 2 c | |
1281 | $r zadd zset 10 d | |
1282 | $r zadd zset 3 e | |
1283 | $r sort zset alpha desc | |
1284 | } {e d c b a} | |
1285 | ||
c74e7c77 | 1286 | test {Sorted sets +inf and -inf handling} { |
1287 | $r del zset | |
1288 | $r zadd zset -100 a | |
1289 | $r zadd zset 200 b | |
1290 | $r zadd zset -300 c | |
1291 | $r zadd zset 1000000 d | |
1292 | $r zadd zset +inf max | |
1293 | $r zadd zset -inf min | |
1294 | $r zrange zset 0 -1 | |
1295 | } {min c a b d max} | |
1296 | ||
b9febaab | 1297 | test {EXPIRE - don't set timeouts multiple times} { |
1298 | $r set x foobar | |
1299 | set v1 [$r expire x 5] | |
1300 | set v2 [$r ttl x] | |
1301 | set v3 [$r expire x 10] | |
1302 | set v4 [$r ttl x] | |
1303 | list $v1 $v2 $v3 $v4 | |
1304 | } {1 5 0 5} | |
1305 | ||
1306 | test {EXPIRE - It should be still possible to read 'x'} { | |
1307 | $r get x | |
1308 | } {foobar} | |
1309 | ||
1310 | test {EXPIRE - After 6 seconds the key should no longer be here} { | |
1311 | after 6000 | |
1312 | list [$r get x] [$r exists x] | |
1313 | } {{} 0} | |
1314 | ||
1315 | test {EXPIRE - Delete on write policy} { | |
1316 | $r del x | |
1317 | $r lpush x foo | |
1318 | $r expire x 1000 | |
1319 | $r lpush x bar | |
1320 | $r lrange x 0 -1 | |
1321 | } {bar} | |
1322 | ||
5446315f | 1323 | test {EXPIREAT - Check for EXPIRE alike behavior} { |
1324 | $r del x | |
1325 | $r set x foo | |
1326 | $r expireat x [expr [clock seconds]+15] | |
1327 | $r ttl x | |
1328 | } {1[345]} | |
1329 | ||
d7f43c08 | 1330 | test {ZSETs skiplist implementation backlink consistency test} { |
1331 | set diff 0 | |
1332 | set elements 10000 | |
1333 | for {set j 0} {$j < $elements} {incr j} { | |
1334 | $r zadd myzset [expr rand()] "Element-$j" | |
1335 | $r zrem myzset "Element-[expr int(rand()*$elements)]" | |
1336 | } | |
1337 | set l1 [$r zrange myzset 0 -1] | |
1338 | set l2 [$r zrevrange myzset 0 -1] | |
1339 | for {set j 0} {$j < [llength $l1]} {incr j} { | |
1340 | if {[lindex $l1 $j] ne [lindex $l2 end-$j]} { | |
1341 | incr diff | |
1342 | } | |
1343 | } | |
1344 | format $diff | |
1345 | } {0} | |
1346 | ||
75398fbc | 1347 | foreach fuzztype {binary alpha compr} { |
1348 | test "FUZZ stresser with data model $fuzztype" { | |
1349 | set err 0 | |
f69f2cba | 1350 | for {set i 0} {$i < 10000} {incr i} { |
75398fbc | 1351 | set fuzz [randstring 0 512 $fuzztype] |
1352 | $r set foo $fuzz | |
1353 | set got [$r get foo] | |
1354 | if {$got ne $fuzz} { | |
f69f2cba | 1355 | set err [list $fuzz $got] |
75398fbc | 1356 | break |
1357 | } | |
1358 | } | |
f69f2cba | 1359 | set _ $err |
75398fbc | 1360 | } {0} |
1361 | } | |
1362 | ||
0d36ded0 | 1363 | test {BGSAVE} { |
1364 | $r flushdb | |
1365 | $r save | |
1366 | $r set x 10 | |
1367 | $r bgsave | |
1368 | waitForBgsave $r | |
1369 | $r debug reload | |
1370 | $r get x | |
1371 | } {10} | |
1372 | ||
7c49733c | 1373 | test {Handle an empty query well} { |
1374 | set fd [$r channel] | |
1375 | puts -nonewline $fd "\r\n" | |
1376 | flush $fd | |
1377 | $r ping | |
1378 | } {PONG} | |
1379 | ||
1380 | test {Negative multi bulk command does not create problems} { | |
1381 | set fd [$r channel] | |
1382 | puts -nonewline $fd "*-10\r\n" | |
1383 | flush $fd | |
1384 | $r ping | |
1385 | } {PONG} | |
1386 | ||
1387 | test {Negative multi bulk payload} { | |
1388 | set fd [$r channel] | |
1389 | puts -nonewline $fd "SET x -10\r\n" | |
1390 | flush $fd | |
1391 | gets $fd | |
1392 | } {*invalid bulk*} | |
1393 | ||
1394 | test {Too big bulk payload} { | |
1395 | set fd [$r channel] | |
1396 | puts -nonewline $fd "SET x 2000000000\r\n" | |
1397 | flush $fd | |
1398 | gets $fd | |
1399 | } {*invalid bulk*count*} | |
1400 | ||
1401 | test {Multi bulk request not followed by bulk args} { | |
1402 | set fd [$r channel] | |
1403 | puts -nonewline $fd "*1\r\nfoo\r\n" | |
1404 | flush $fd | |
1405 | gets $fd | |
1406 | } {*protocol error*} | |
1407 | ||
1408 | test {Generic wrong number of args} { | |
1409 | catch {$r ping x y z} err | |
1410 | set _ $err | |
1411 | } {*wrong*arguments*ping*} | |
1412 | ||
1413 | test {SELECT an out of range DB} { | |
1414 | catch {$r select 1000000} err | |
1415 | set _ $err | |
1416 | } {*invalid*} | |
1417 | ||
e054afda | 1418 | if {![catch {package require sha1}]} { |
1419 | test {Check consistency of different data types after a reload} { | |
1420 | $r flushdb | |
1421 | createComplexDataset $r 10000 | |
1422 | set sha1 [datasetDigest $r] | |
1423 | $r debug reload | |
1424 | set sha1_after [datasetDigest $r] | |
1425 | expr {$sha1 eq $sha1_after} | |
1426 | } {1} | |
e96e4fbf | 1427 | |
1428 | test {Same dataset digest if saving/reloading as AOF?} { | |
1429 | $r bgrewriteaof | |
1430 | waitForBgrewriteaof $r | |
1431 | $r debug loadaof | |
1432 | set sha1_after [datasetDigest $r] | |
1433 | expr {$sha1 eq $sha1_after} | |
1434 | } {1} | |
e054afda | 1435 | } |
1436 | ||
e96e4fbf | 1437 | test {EXPIRES after a reload (snapshot + append only file)} { |
71c2b467 | 1438 | $r flushdb |
1439 | $r set x 10 | |
1440 | $r expire x 1000 | |
1441 | $r save | |
1442 | $r debug reload | |
1443 | set ttl [$r ttl x] | |
e96e4fbf | 1444 | set e1 [expr {$ttl > 900 && $ttl <= 1000}] |
1445 | $r bgrewriteaof | |
1446 | waitForBgrewriteaof $r | |
1447 | set ttl [$r ttl x] | |
1448 | set e2 [expr {$ttl > 900 && $ttl <= 1000}] | |
1449 | list $e1 $e2 | |
1450 | } {1 1} | |
71c2b467 | 1451 | |
483049a7 | 1452 | test {PIPELINING stresser (also a regression for the old epoll bug)} { |
1453 | set fd2 [socket 127.0.0.1 6379] | |
1454 | fconfigure $fd2 -encoding binary -translation binary | |
1455 | ||
1456 | for {set i 0} {$i < 100000} {incr i} { | |
1457 | set q {} | |
1458 | set val "0000${i}0000" | |
1459 | append q "SET key:$i [string length $val]\r\n$val\r\n" | |
1460 | puts -nonewline $fd2 $q | |
1461 | set q {} | |
1462 | append q "GET key:$i\r\n" | |
1463 | puts -nonewline $fd2 $q | |
1464 | } | |
1465 | flush $fd2 | |
1466 | ||
1467 | for {set i 0} {$i < 100000} {incr i} { | |
1468 | gets $fd2 line | |
1469 | gets $fd2 count | |
1470 | set count [string range $count 1 end] | |
1471 | set val [read $fd2 $count] | |
1472 | read $fd2 2 | |
1473 | } | |
1474 | close $fd2 | |
1475 | set _ 1 | |
1476 | } {1} | |
1477 | ||
ed9b544e | 1478 | # Leave the user with a clean DB before to exit |
eea4baf7 | 1479 | test {FLUSHDB} { |
1480 | set aux {} | |
1481 | $r select 9 | |
1482 | $r flushdb | |
1483 | lappend aux [$r dbsize] | |
1484 | $r select 10 | |
1485 | $r flushdb | |
1486 | lappend aux [$r dbsize] | |
1487 | } {0 0} | |
ed9b544e | 1488 | |
c28b42ac | 1489 | test {Perform a final SAVE to leave a clean DB on disk} { |
1490 | $r save | |
1491 | } {OK} | |
1492 | ||
17511391 | 1493 | catch { |
1494 | if {[string match {*Darwin*} [exec uname -a]]} { | |
1495 | test {Check for memory leaks} { | |
1496 | exec leaks redis-server | |
1497 | } {*0 leaks*} | |
1498 | } | |
1499 | } | |
1500 | ||
ed9b544e | 1501 | puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed" |
1502 | if {$::failed > 0} { | |
1503 | puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n" | |
1504 | } | |
ed9b544e | 1505 | } |
1506 | ||
5a6948fb | 1507 | proc stress {} { |
43c9dc7b | 1508 | set r [redis] |
eea4baf7 | 1509 | $r select 9 |
1510 | $r flushdb | |
5a6948fb | 1511 | while 1 { |
1512 | set randkey [expr int(rand()*10000)] | |
1513 | set randval [expr int(rand()*10000)] | |
1514 | set randidx0 [expr int(rand()*10)] | |
1515 | set randidx1 [expr int(rand()*10)] | |
12f9d551 | 1516 | set cmd [expr int(rand()*20)] |
43c9dc7b | 1517 | catch { |
1518 | if {$cmd == 0} {$r set $randkey $randval} | |
1519 | if {$cmd == 1} {$r get $randkey} | |
1520 | if {$cmd == 2} {$r incr $randkey} | |
1521 | if {$cmd == 3} {$r lpush $randkey $randval} | |
1522 | if {$cmd == 4} {$r rpop $randkey} | |
1523 | if {$cmd == 5} {$r del $randkey} | |
12f9d551 | 1524 | if {$cmd == 6} {$r llen $randkey} |
1525 | if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1} | |
1526 | if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1} | |
1527 | if {$cmd == 9} {$r lindex $randkey $randidx0} | |
1528 | if {$cmd == 10} {$r lset $randkey $randidx0 $randval} | |
1529 | if {$cmd == 11} {$r sadd $randkey $randval} | |
1530 | if {$cmd == 12} {$r srem $randkey $randval} | |
1531 | if {$cmd == 13} {$r smove $randkey $randval} | |
1532 | if {$cmd == 14} {$r scard $randkey} | |
1533 | if {$cmd == 15} {$r expire $randkey [expr $randval%60]} | |
43c9dc7b | 1534 | } |
5a6948fb | 1535 | flush stdout |
1536 | } | |
eea4baf7 | 1537 | $r flushdb |
43c9dc7b | 1538 | $r close |
5a6948fb | 1539 | } |
1540 | ||
fc77604c | 1541 | # Set a few configuration defaults |
1542 | set ::host 127.0.0.1 | |
1543 | set ::port 6379 | |
1544 | set ::stress 0 | |
1545 | set ::flush 0 | |
1546 | set ::first 0 | |
1547 | set ::last 1000000 | |
1548 | ||
1549 | # Parse arguments | |
1550 | for {set j 0} {$j < [llength $argv]} {incr j} { | |
1551 | set opt [lindex $argv $j] | |
1552 | set arg [lindex $argv [expr $j+1]] | |
1553 | set lastarg [expr {$arg eq {}}] | |
1554 | if {$opt eq {-h} && !$lastarg} { | |
1555 | set ::host $arg | |
1556 | incr j | |
1557 | } elseif {$opt eq {-p} && !$lastarg} { | |
1558 | set ::port $arg | |
1559 | incr j | |
1560 | } elseif {$opt eq {-stress}} { | |
1561 | set ::stress 1 | |
1562 | } elseif {$opt eq {--flush}} { | |
1563 | set ::flush 1 | |
1564 | } elseif {$opt eq {--first} && !$lastarg} { | |
1565 | set ::first $arg | |
1566 | incr j | |
1567 | } elseif {$opt eq {--last} && !$lastarg} { | |
1568 | set ::last $arg | |
1569 | incr j | |
1570 | } else { | |
7c49733c | 1571 | puts "Wrong argument: $opt" |
fc77604c | 1572 | exit 1 |
1573 | } | |
1574 | } | |
1575 | ||
eea4baf7 | 1576 | # Before to run the test check if DB 9 and DB 10 are empty |
1577 | set r [redis] | |
fc77604c | 1578 | |
1579 | if {$::flush} { | |
1580 | $r flushall | |
1581 | } | |
1582 | ||
eea4baf7 | 1583 | $r select 9 |
1584 | set db9size [$r dbsize] | |
1585 | $r select 10 | |
1586 | set db10size [$r dbsize] | |
1587 | if {$db9size != 0 || $db10size != 0} { | |
1588 | puts "Can't run the tests against DB 9 and 10: DBs are not empty." | |
1589 | exit 1 | |
1590 | } | |
1591 | $r close | |
1592 | unset r | |
1593 | unset db9size | |
1594 | unset db10size | |
1595 | ||
fc77604c | 1596 | if {$::stress} { |
5a6948fb | 1597 | stress |
ed9b544e | 1598 | } else { |
fc77604c | 1599 | main $::host $::port |
ed9b544e | 1600 | } |