]> git.saurik.com Git - redis.git/blob - test-redis.tcl
GETSET implemented
[redis.git] / test-redis.tcl
1 # TODO # test pipelining
2
3 source client-libraries/tcl/redis.tcl
4
5 set ::passed 0
6 set ::failed 0
7
8 proc test {name code okpattern} {
9 puts -nonewline [format "%-70s " $name]
10 flush stdout
11 set retval [uplevel 1 $code]
12 if {$okpattern eq $retval || [string match $okpattern $retval]} {
13 puts "PASSED"
14 incr ::passed
15 } else {
16 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
17 incr ::failed
18 }
19 }
20
21 proc main {server port} {
22 set r [redis $server $port]
23 set err ""
24
25 # The following AUTH test should be enabled only when requirepass
26 # <PASSWORD> is set in redis.conf and redis-server was started with
27 # redis.conf as the first argument.
28
29 #test {AUTH with requirepass in redis.conf} {
30 # $r auth foobared
31 #} {OK}
32
33 test {DEL all keys to start with a clean DB} {
34 foreach key [$r keys *] {$r del $key}
35 $r dbsize
36 } {0}
37
38 test {SET and GET an item} {
39 $r set x foobar
40 $r get x
41 } {foobar}
42
43 test {DEL against a single item} {
44 $r del x
45 $r get x
46 } {}
47
48 test {KEYS with pattern} {
49 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
50 $r set $key hello
51 }
52 lsort [$r keys foo*]
53 } {foo_a foo_b foo_c}
54
55 test {KEYS to get all keys} {
56 lsort [$r keys *]
57 } {foo_a foo_b foo_c key_x key_y key_z}
58
59 test {DBSIZE} {
60 $r dbsize
61 } {6}
62
63 test {DEL all keys} {
64 foreach key [$r keys *] {$r del $key}
65 $r dbsize
66 } {0}
67
68 test {Very big payload in GET/SET} {
69 set buf [string repeat "abcd" 1000000]
70 $r set foo $buf
71 $r get foo
72 } [string repeat "abcd" 1000000]
73
74 test {SET 10000 numeric keys and access all them in reverse order} {
75 for {set x 0} {$x < 10000} {incr x} {
76 $r set $x $x
77 }
78 set sum 0
79 for {set x 9999} {$x >= 0} {incr x -1} {
80 incr sum [$r get $x]
81 }
82 format $sum
83 } {49995000}
84
85 test {DBSIZE should be 10001 now} {
86 $r dbsize
87 } {10001}
88
89 test {INCR against non existing key} {
90 set res {}
91 append res [$r incr novar]
92 append res [$r get novar]
93 } {11}
94
95 test {INCR against key created by incr itself} {
96 $r incr novar
97 } {2}
98
99 test {INCR against key originally set with SET} {
100 $r set novar 100
101 $r incr novar
102 } {101}
103
104 test {INCR over 32bit value} {
105 $r set novar 17179869184
106 $r incr novar
107 } {17179869185}
108
109 test {INCRBY over 32bit value with over 32bit increment} {
110 $r set novar 17179869184
111 $r incrby novar 17179869184
112 } {34359738368}
113
114 test {DECRBY over 32bit value with over 32bit increment, negative res} {
115 $r set novar 17179869184
116 $r decrby novar 17179869185
117 } {-1}
118
119 test {SETNX target key missing} {
120 $r setnx novar2 foobared
121 $r get novar2
122 } {foobared}
123
124 test {SETNX target key exists} {
125 $r setnx novar2 blabla
126 $r get novar2
127 } {foobared}
128
129 test {EXISTS} {
130 set res {}
131 $r set newkey test
132 append res [$r exists newkey]
133 $r del newkey
134 append res [$r exists newkey]
135 } {10}
136
137 test {Zero length value in key. SET/GET/EXISTS} {
138 $r set emptykey {}
139 set res [$r get emptykey]
140 append res [$r exists emptykey]
141 $r del emptykey
142 append res [$r exists emptykey]
143 } {10}
144
145 test {Commands pipelining} {
146 set fd [$r channel]
147 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
148 flush $fd
149 set res {}
150 append res [string match OK* [::redis::redis_read_reply $fd]]
151 append res [::redis::redis_read_reply $fd]
152 append res [string match PONG* [::redis::redis_read_reply $fd]]
153 format $res
154 } {1xyzk1}
155
156 test {Non existing command} {
157 catch {$r foobaredcommand} err
158 string match ERR* $err
159 } {1}
160
161 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
162 $r lpush mylist a
163 $r lpush mylist b
164 $r rpush mylist c
165 set res [$r llen mylist]
166 append res [$r lindex mylist 0]
167 append res [$r lindex mylist 1]
168 append res [$r lindex mylist 2]
169 } {3bac}
170
171 test {DEL a list} {
172 $r del mylist
173 $r exists mylist
174 } {0}
175
176 test {Create a long list and check every single element with LINDEX} {
177 set ok 0
178 for {set i 0} {$i < 1000} {incr i} {
179 $r rpush mylist $i
180 }
181 for {set i 0} {$i < 1000} {incr i} {
182 if {[$r lindex mylist $i] eq $i} {incr ok}
183 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
184 incr ok
185 }
186 }
187 format $ok
188 } {2000}
189
190 test {Test elements with LINDEX in random access} {
191 set ok 0
192 for {set i 0} {$i < 1000} {incr i} {
193 set rint [expr int(rand()*1000)]
194 if {[$r lindex mylist $rint] eq $rint} {incr ok}
195 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
196 incr ok
197 }
198 }
199 format $ok
200 } {2000}
201
202 test {LLEN against non-list value error} {
203 $r del mylist
204 $r set mylist foobar
205 catch {$r llen mylist} err
206 format $err
207 } {ERR*}
208
209 test {LINDEX against non-list value error} {
210 catch {$r lindex mylist 0} err
211 format $err
212 } {ERR*}
213
214 test {LPUSH against non-list value error} {
215 catch {$r lpush mylist 0} err
216 format $err
217 } {ERR*}
218
219 test {RPUSH against non-list value error} {
220 catch {$r rpush mylist 0} err
221 format $err
222 } {ERR*}
223
224 test {RENAME basic usage} {
225 $r set mykey hello
226 $r rename mykey mykey1
227 $r rename mykey1 mykey2
228 $r get mykey2
229 } {hello}
230
231 test {RENAME source key should no longer exist} {
232 $r exists mykey
233 } {0}
234
235 test {RENAME against already existing key} {
236 $r set mykey a
237 $r set mykey2 b
238 $r rename mykey2 mykey
239 set res [$r get mykey]
240 append res [$r exists mykey2]
241 } {b0}
242
243 test {RENAMENX basic usage} {
244 $r del mykey
245 $r del mykey2
246 $r set mykey foobar
247 $r renamenx mykey mykey2
248 set res [$r get mykey2]
249 append res [$r exists mykey]
250 } {foobar0}
251
252 test {RENAMENX against already existing key} {
253 $r set mykey foo
254 $r set mykey2 bar
255 $r renamenx mykey mykey2
256 } {0}
257
258 test {RENAMENX against already existing key (2)} {
259 set res [$r get mykey]
260 append res [$r get mykey2]
261 } {foobar}
262
263 test {RENAME against non existing source key} {
264 catch {$r rename nokey foobar} err
265 format $err
266 } {ERR*}
267
268 test {RENAME where source and dest key is the same} {
269 catch {$r rename mykey mykey} err
270 format $err
271 } {ERR*}
272
273 test {DEL all keys again (DB 0)} {
274 foreach key [$r keys *] {
275 $r del $key
276 }
277 $r dbsize
278 } {0}
279
280 test {DEL all keys again (DB 1)} {
281 $r select 1
282 foreach key [$r keys *] {
283 $r del $key
284 }
285 set res [$r dbsize]
286 $r select 0
287 format $res
288 } {0}
289
290 test {MOVE basic usage} {
291 $r set mykey foobar
292 $r move mykey 1
293 set res {}
294 lappend res [$r exists mykey]
295 lappend res [$r dbsize]
296 $r select 1
297 lappend res [$r get mykey]
298 lappend res [$r dbsize]
299 $r select 0
300 format $res
301 } [list 0 0 foobar 1]
302
303 test {MOVE against key existing in the target DB} {
304 $r set mykey hello
305 $r move mykey 1
306 } {0}
307
308 test {SET/GET keys in different DBs} {
309 $r set a hello
310 $r set b world
311 $r select 1
312 $r set a foo
313 $r set b bared
314 $r select 0
315 set res {}
316 lappend res [$r get a]
317 lappend res [$r get b]
318 $r select 1
319 lappend res [$r get a]
320 lappend res [$r get b]
321 $r select 0
322 format $res
323 } {hello world foo bared}
324
325 test {Basic LPOP/RPOP} {
326 $r del mylist
327 $r rpush mylist 1
328 $r rpush mylist 2
329 $r lpush mylist 0
330 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
331 } [list 0 2 1 0]
332
333 test {LPOP/RPOP against empty list} {
334 $r lpop mylist
335 } {}
336
337 test {LPOP against non list value} {
338 $r set notalist foo
339 catch {$r lpop notalist} err
340 format $err
341 } {ERR*kind*}
342
343 test {Mass LPUSH/LPOP} {
344 set sum 0
345 for {set i 0} {$i < 1000} {incr i} {
346 $r lpush mylist $i
347 incr sum $i
348 }
349 set sum2 0
350 for {set i 0} {$i < 500} {incr i} {
351 incr sum2 [$r lpop mylist]
352 incr sum2 [$r rpop mylist]
353 }
354 expr $sum == $sum2
355 } {1}
356
357 test {LRANGE basics} {
358 for {set i 0} {$i < 10} {incr i} {
359 $r rpush mylist $i
360 }
361 list [$r lrange mylist 1 -2] \
362 [$r lrange mylist -3 -1] \
363 [$r lrange mylist 4 4]
364 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
365
366 test {LRANGE inverted indexes} {
367 $r lrange mylist 6 2
368 } {}
369
370 test {LRANGE out of range indexes including the full list} {
371 $r lrange mylist -1000 1000
372 } {0 1 2 3 4 5 6 7 8 9}
373
374 test {LRANGE against non existing key} {
375 $r lrange nosuchkey 0 1
376 } {}
377
378 test {LTRIM basics} {
379 $r del mylist
380 for {set i 0} {$i < 100} {incr i} {
381 $r lpush mylist $i
382 $r ltrim mylist 0 4
383 }
384 $r lrange mylist 0 -1
385 } {99 98 97 96 95}
386
387 test {LSET} {
388 $r lset mylist 1 foo
389 $r lset mylist -1 bar
390 $r lrange mylist 0 -1
391 } {99 foo 97 96 bar}
392
393 test {LSET out of range index} {
394 catch {$r lset mylist 10 foo} err
395 format $err
396 } {ERR*range*}
397
398 test {LSET against non existing key} {
399 catch {$r lset nosuchkey 10 foo} err
400 format $err
401 } {ERR*key*}
402
403 test {LSET against non list value} {
404 $r set nolist foobar
405 catch {$r lset nolist 0 foo} err
406 format $err
407 } {ERR*value*}
408
409 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
410 $r sadd myset foo
411 $r sadd myset bar
412 list [$r scard myset] [$r sismember myset foo] \
413 [$r sismember myset bar] [$r sismember myset bla] \
414 [lsort [$r smembers myset]]
415 } {2 1 1 0 {bar foo}}
416
417 test {SADD adding the same element multiple times} {
418 $r sadd myset foo
419 $r sadd myset foo
420 $r sadd myset foo
421 $r scard myset
422 } {2}
423
424 test {SADD against non set} {
425 catch {$r sadd mylist foo} err
426 format $err
427 } {ERR*kind*}
428
429 test {SREM basics} {
430 $r sadd myset ciao
431 $r srem myset foo
432 lsort [$r smembers myset]
433 } {bar ciao}
434
435 test {Mass SADD and SINTER with two sets} {
436 for {set i 0} {$i < 1000} {incr i} {
437 $r sadd set1 $i
438 $r sadd set2 [expr $i+995]
439 }
440 lsort [$r sinter set1 set2]
441 } {995 996 997 998 999}
442
443 test {SUNION with two sets} {
444 lsort [$r sunion set1 set2]
445 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
446
447 test {SINTERSTORE with two sets} {
448 $r sinterstore setres set1 set2
449 lsort [$r smembers setres]
450 } {995 996 997 998 999}
451
452 test {SUNIONSTORE with two sets} {
453 $r sunionstore setres set1 set2
454 lsort [$r smembers setres]
455 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
456
457 test {SINTER against three sets} {
458 $r sadd set3 999
459 $r sadd set3 995
460 $r sadd set3 1000
461 $r sadd set3 2000
462 lsort [$r sinter set1 set2 set3]
463 } {995 999}
464
465 test {SINTERSTORE with three sets} {
466 $r sinterstore setres set1 set2 set3
467 lsort [$r smembers setres]
468 } {995 999}
469
470 test {SUNION with non existing keys} {
471 lsort [$r sunion nokey1 set1 set2 nokey2]
472 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
473
474 test {SAVE - make sure there are all the types as values} {
475 $r lpush mysavelist hello
476 $r lpush mysavelist world
477 $r set myemptykey {}
478 $r set mynormalkey {blablablba}
479 $r save
480 } {OK}
481
482 test {Create a random list} {
483 set tosort {}
484 array set seenrand {}
485 for {set i 0} {$i < 10000} {incr i} {
486 while 1 {
487 # Make sure all the weights are different because
488 # Redis does not use a stable sort but Tcl does.
489 set rint [expr int(rand()*1000000)]
490 if {![info exists seenrand($rint)]} break
491 }
492 set seenrand($rint) x
493 $r lpush tosort $i
494 $r set weight_$i $rint
495 lappend tosort [list $i $rint]
496 }
497 set sorted [lsort -index 1 -real $tosort]
498 set res {}
499 for {set i 0} {$i < 10000} {incr i} {
500 lappend res [lindex $sorted $i 0]
501 }
502 format {}
503 } {}
504
505 test {SORT with BY against the newly created list} {
506 $r sort tosort {BY weight_*}
507 } $res
508
509 test {SORT direct, numeric, against the newly created list} {
510 $r sort tosort
511 } [lsort -integer $res]
512
513 test {SORT decreasing sort} {
514 $r sort tosort {DESC}
515 } [lsort -decreasing -integer $res]
516
517 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
518 set start [clock clicks -milliseconds]
519 for {set i 0} {$i < 100} {incr i} {
520 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
521 }
522 set elapsed [expr [clock clicks -milliseconds]-$start]
523 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
524 flush stdout
525 format {}
526 } {}
527
528 test {SORT speed, sorting 10000 elements list directly, 100 times} {
529 set start [clock clicks -milliseconds]
530 for {set i 0} {$i < 100} {incr i} {
531 set sorted [$r sort tosort {LIMIT 0 10}]
532 }
533 set elapsed [expr [clock clicks -milliseconds]-$start]
534 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
535 flush stdout
536 format {}
537 } {}
538
539 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
540 set start [clock clicks -milliseconds]
541 for {set i 0} {$i < 100} {incr i} {
542 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
543 }
544 set elapsed [expr [clock clicks -milliseconds]-$start]
545 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
546 flush stdout
547 format {}
548 } {}
549
550 test {SORT regression for issue #19, sorting floats} {
551 $r flushdb
552 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
553 $r lpush mylist $x
554 }
555 $r sort mylist
556 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
557
558 test {LREM, remove all the occurrences} {
559 $r flushall
560 $r rpush mylist foo
561 $r rpush mylist bar
562 $r rpush mylist foobar
563 $r rpush mylist foobared
564 $r rpush mylist zap
565 $r rpush mylist bar
566 $r rpush mylist test
567 $r rpush mylist foo
568 set res [$r lrem mylist 0 bar]
569 list [$r lrange mylist 0 -1] $res
570 } {{foo foobar foobared zap test foo} 2}
571
572 test {LREM, remove the first occurrence} {
573 set res [$r lrem mylist 1 foo]
574 list [$r lrange mylist 0 -1] $res
575 } {{foobar foobared zap test foo} 1}
576
577 test {LREM, remove non existing element} {
578 set res [$r lrem mylist 1 nosuchelement]
579 list [$r lrange mylist 0 -1] $res
580 } {{foobar foobared zap test foo} 0}
581
582 test {LREM, starting from tail with negative count} {
583 $r flushall
584 $r rpush mylist foo
585 $r rpush mylist bar
586 $r rpush mylist foobar
587 $r rpush mylist foobared
588 $r rpush mylist zap
589 $r rpush mylist bar
590 $r rpush mylist test
591 $r rpush mylist foo
592 $r rpush mylist foo
593 set res [$r lrem mylist -1 bar]
594 list [$r lrange mylist 0 -1] $res
595 } {{foo bar foobar foobared zap test foo foo} 1}
596
597 test {LREM, starting from tail with negative count (2)} {
598 set res [$r lrem mylist -2 foo]
599 list [$r lrange mylist 0 -1] $res
600 } {{foo bar foobar foobared zap test} 2}
601
602 test {MGET} {
603 $r flushall
604 $r set foo BAR
605 $r set bar FOO
606 $r mget foo bar
607 } {BAR FOO}
608
609 test {MGET against non existing key} {
610 $r mget foo baazz bar
611 } {BAR {} FOO}
612
613 test {MGET against non-string key} {
614 $r sadd myset ciao
615 $r sadd myset bau
616 $r mget foo baazz bar myset
617 } {BAR {} FOO {}}
618
619 test {RANDOMKEY} {
620 $r flushall
621 $r set foo x
622 $r set bar y
623 set foo_seen 0
624 set bar_seen 0
625 for {set i 0} {$i < 100} {incr i} {
626 set rkey [$r randomkey]
627 if {$rkey eq {foo}} {
628 set foo_seen 1
629 }
630 if {$rkey eq {bar}} {
631 set bar_seen 1
632 }
633 }
634 list $foo_seen $bar_seen
635 } {1 1}
636
637 test {RANDOMKEY against empty DB} {
638 $r flushall
639 $r randomkey
640 } {}
641
642 test {RANDOMKEY regression 1} {
643 $r flushall
644 $r set x 10
645 $r del x
646 $r randomkey
647 } {}
648
649 # Leave the user with a clean DB before to exit
650 test {FLUSHALL} {
651 $r flushall
652 $r dbsize
653 } {0}
654
655 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
656 if {$::failed > 0} {
657 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
658 }
659 close $fd
660 }
661
662 proc stress {} {
663 set r [redis]
664 $r flushall
665 while 1 {
666 set randkey [expr int(rand()*10000)]
667 set randval [expr int(rand()*10000)]
668 set randidx0 [expr int(rand()*10)]
669 set randidx1 [expr int(rand()*10)]
670 set cmd [expr int(rand()*10)]
671 catch {
672 if {$cmd == 0} {$r set $randkey $randval}
673 if {$cmd == 1} {$r get $randkey}
674 if {$cmd == 2} {$r incr $randkey}
675 if {$cmd == 3} {$r lpush $randkey $randval}
676 if {$cmd == 4} {$r rpop $randkey}
677 if {$cmd == 5} {$r del $randkey}
678 if {$cmd == 6} {$r lrange $randkey $randidx0 $randidx1}
679 if {$cmd == 7} {$r ltrim $randkey $randidx0 $randidx1}
680 if {$cmd == 8} {$r lindex $randkey $randidx0}
681 if {$cmd == 9} {$r lset $randkey $randidx0 $randval}
682 }
683 flush stdout
684 }
685 $r close
686 }
687
688 if {[llength $argv] == 0} {
689 main 127.0.0.1 6379
690 } elseif {[llength $argv] == 1 && [lindex $argv 0] eq {stress}} {
691 stress
692 } else {
693 main [lindex $argv 0] [lindex $argv 1]
694 }