]> git.saurik.com Git - redis.git/blob - test-redis.tcl
TODO updated
[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 {SETNX target key missing} {
105 $r setnx novar2 foobared
106 $r get novar2
107 } {foobared}
108
109 test {SETNX target key exists} {
110 $r setnx novar2 blabla
111 $r get novar2
112 } {foobared}
113
114 test {EXISTS} {
115 set res {}
116 $r set newkey test
117 append res [$r exists newkey]
118 $r del newkey
119 append res [$r exists newkey]
120 } {10}
121
122 test {Zero length value in key. SET/GET/EXISTS} {
123 $r set emptykey {}
124 set res [$r get emptykey]
125 append res [$r exists emptykey]
126 $r del emptykey
127 append res [$r exists emptykey]
128 } {10}
129
130 test {Commands pipelining} {
131 set fd [$r channel]
132 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
133 flush $fd
134 set res {}
135 append res [string match OK* [::redis::redis_read_reply $fd]]
136 append res [::redis::redis_read_reply $fd]
137 append res [string match PONG* [::redis::redis_read_reply $fd]]
138 format $res
139 } {1xyzk1}
140
141 test {Non existing command} {
142 catch {$r foobaredcommand} err
143 string match ERR* $err
144 } {1}
145
146 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
147 $r lpush mylist a
148 $r lpush mylist b
149 $r rpush mylist c
150 set res [$r llen mylist]
151 append res [$r lindex mylist 0]
152 append res [$r lindex mylist 1]
153 append res [$r lindex mylist 2]
154 } {3bac}
155
156 test {DEL a list} {
157 $r del mylist
158 $r exists mylist
159 } {0}
160
161 test {Create a long list and check every single element with LINDEX} {
162 set ok 0
163 for {set i 0} {$i < 1000} {incr i} {
164 $r rpush mylist $i
165 }
166 for {set i 0} {$i < 1000} {incr i} {
167 if {[$r lindex mylist $i] eq $i} {incr ok}
168 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
169 incr ok
170 }
171 }
172 format $ok
173 } {2000}
174
175 test {Test elements with LINDEX in random access} {
176 set ok 0
177 for {set i 0} {$i < 1000} {incr i} {
178 set rint [expr int(rand()*1000)]
179 if {[$r lindex mylist $rint] eq $rint} {incr ok}
180 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
181 incr ok
182 }
183 }
184 format $ok
185 } {2000}
186
187 test {LLEN against non-list value error} {
188 $r del mylist
189 $r set mylist foobar
190 catch {$r llen mylist} err
191 format $err
192 } {ERR*}
193
194 test {LINDEX against non-list value error} {
195 catch {$r lindex mylist 0} err
196 format $err
197 } {ERR*}
198
199 test {LPUSH against non-list value error} {
200 catch {$r lpush mylist 0} err
201 format $err
202 } {ERR*}
203
204 test {RPUSH against non-list value error} {
205 catch {$r rpush mylist 0} err
206 format $err
207 } {ERR*}
208
209 test {RENAME basic usage} {
210 $r set mykey hello
211 $r rename mykey mykey1
212 $r rename mykey1 mykey2
213 $r get mykey2
214 } {hello}
215
216 test {RENAME source key should no longer exist} {
217 $r exists mykey
218 } {0}
219
220 test {RENAME against already existing key} {
221 $r set mykey a
222 $r set mykey2 b
223 $r rename mykey2 mykey
224 set res [$r get mykey]
225 append res [$r exists mykey2]
226 } {b0}
227
228 test {RENAMENX basic usage} {
229 $r del mykey
230 $r del mykey2
231 $r set mykey foobar
232 $r renamenx mykey mykey2
233 set res [$r get mykey2]
234 append res [$r exists mykey]
235 } {foobar0}
236
237 test {RENAMENX against already existing key} {
238 $r set mykey foo
239 $r set mykey2 bar
240 $r renamenx mykey mykey2
241 } {0}
242
243 test {RENAMENX against already existing key (2)} {
244 set res [$r get mykey]
245 append res [$r get mykey2]
246 } {foobar}
247
248 test {RENAME against non existing source key} {
249 catch {$r rename nokey foobar} err
250 format $err
251 } {ERR*}
252
253 test {RENAME where source and dest key is the same} {
254 catch {$r rename mykey mykey} err
255 format $err
256 } {ERR*}
257
258 test {DEL all keys again (DB 0)} {
259 foreach key [$r keys *] {
260 $r del $key
261 }
262 $r dbsize
263 } {0}
264
265 test {DEL all keys again (DB 1)} {
266 $r select 1
267 foreach key [$r keys *] {
268 $r del $key
269 }
270 set res [$r dbsize]
271 $r select 0
272 format $res
273 } {0}
274
275 test {MOVE basic usage} {
276 $r set mykey foobar
277 $r move mykey 1
278 set res {}
279 lappend res [$r exists mykey]
280 lappend res [$r dbsize]
281 $r select 1
282 lappend res [$r get mykey]
283 lappend res [$r dbsize]
284 $r select 0
285 format $res
286 } [list 0 0 foobar 1]
287
288 test {MOVE against key existing in the target DB} {
289 $r set mykey hello
290 $r move mykey 1
291 } {0}
292
293 test {SET/GET keys in different DBs} {
294 $r set a hello
295 $r set b world
296 $r select 1
297 $r set a foo
298 $r set b bared
299 $r select 0
300 set res {}
301 lappend res [$r get a]
302 lappend res [$r get b]
303 $r select 1
304 lappend res [$r get a]
305 lappend res [$r get b]
306 $r select 0
307 format $res
308 } {hello world foo bared}
309
310 test {Basic LPOP/RPOP} {
311 $r del mylist
312 $r rpush mylist 1
313 $r rpush mylist 2
314 $r lpush mylist 0
315 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
316 } [list 0 2 1 0]
317
318 test {LPOP/RPOP against empty list} {
319 $r lpop mylist
320 } {}
321
322 test {LPOP against non list value} {
323 $r set notalist foo
324 catch {$r lpop notalist} err
325 format $err
326 } {ERR*kind*}
327
328 test {Mass LPUSH/LPOP} {
329 set sum 0
330 for {set i 0} {$i < 1000} {incr i} {
331 $r lpush mylist $i
332 incr sum $i
333 }
334 set sum2 0
335 for {set i 0} {$i < 500} {incr i} {
336 incr sum2 [$r lpop mylist]
337 incr sum2 [$r rpop mylist]
338 }
339 expr $sum == $sum2
340 } {1}
341
342 test {LRANGE basics} {
343 for {set i 0} {$i < 10} {incr i} {
344 $r rpush mylist $i
345 }
346 list [$r lrange mylist 1 -2] \
347 [$r lrange mylist -3 -1] \
348 [$r lrange mylist 4 4]
349 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
350
351 test {LRANGE inverted indexes} {
352 $r lrange mylist 6 2
353 } {}
354
355 test {LRANGE out of range indexes including the full list} {
356 $r lrange mylist -1000 1000
357 } {0 1 2 3 4 5 6 7 8 9}
358
359 test {LRANGE against non existing key} {
360 $r lrange nosuchkey 0 1
361 } {}
362
363 test {LTRIM basics} {
364 $r del mylist
365 for {set i 0} {$i < 100} {incr i} {
366 $r lpush mylist $i
367 $r ltrim mylist 0 4
368 }
369 $r lrange mylist 0 -1
370 } {99 98 97 96 95}
371
372 test {LSET} {
373 $r lset mylist 1 foo
374 $r lset mylist -1 bar
375 $r lrange mylist 0 -1
376 } {99 foo 97 96 bar}
377
378 test {LSET out of range index} {
379 catch {$r lset mylist 10 foo} err
380 format $err
381 } {ERR*range*}
382
383 test {LSET against non existing key} {
384 catch {$r lset nosuchkey 10 foo} err
385 format $err
386 } {ERR*key*}
387
388 test {LSET against non list value} {
389 $r set nolist foobar
390 catch {$r lset nolist 0 foo} err
391 format $err
392 } {ERR*value*}
393
394 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
395 $r sadd myset foo
396 $r sadd myset bar
397 list [$r scard myset] [$r sismember myset foo] \
398 [$r sismember myset bar] [$r sismember myset bla] \
399 [lsort [$r smembers myset]]
400 } {2 1 1 0 {bar foo}}
401
402 test {SADD adding the same element multiple times} {
403 $r sadd myset foo
404 $r sadd myset foo
405 $r sadd myset foo
406 $r scard myset
407 } {2}
408
409 test {SADD against non set} {
410 catch {$r sadd mylist foo} err
411 format $err
412 } {ERR*kind*}
413
414 test {SREM basics} {
415 $r sadd myset ciao
416 $r srem myset foo
417 lsort [$r smembers myset]
418 } {bar ciao}
419
420 test {Mass SADD and SINTER with two sets} {
421 for {set i 0} {$i < 1000} {incr i} {
422 $r sadd set1 $i
423 $r sadd set2 [expr $i+995]
424 }
425 lsort [$r sinter set1 set2]
426 } {995 996 997 998 999}
427
428 test {SINTERSTORE with two sets} {
429 $r sinterstore setres set1 set2
430 lsort [$r smembers setres]
431 } {995 996 997 998 999}
432
433 test {SINTER against three sets} {
434 $r sadd set3 999
435 $r sadd set3 995
436 $r sadd set3 1000
437 $r sadd set3 2000
438 lsort [$r sinter set1 set2 set3]
439 } {995 999}
440
441 test {SINTERSTORE with three sets} {
442 $r sinterstore setres set1 set2 set3
443 lsort [$r smembers setres]
444 } {995 999}
445
446 test {SAVE - make sure there are all the types as values} {
447 $r lpush mysavelist hello
448 $r lpush mysavelist world
449 $r set myemptykey {}
450 $r set mynormalkey {blablablba}
451 $r save
452 } {OK}
453
454 test {Create a random list} {
455 set tosort {}
456 array set seenrand {}
457 for {set i 0} {$i < 10000} {incr i} {
458 while 1 {
459 # Make sure all the weights are different because
460 # Redis does not use a stable sort but Tcl does.
461 set rint [expr int(rand()*1000000)]
462 if {![info exists seenrand($rint)]} break
463 }
464 set seenrand($rint) x
465 $r lpush tosort $i
466 $r set weight_$i $rint
467 lappend tosort [list $i $rint]
468 }
469 set sorted [lsort -index 1 -real $tosort]
470 set res {}
471 for {set i 0} {$i < 10000} {incr i} {
472 lappend res [lindex $sorted $i 0]
473 }
474 format {}
475 } {}
476
477 test {SORT with BY against the newly created list} {
478 $r sort tosort {BY weight_*}
479 } $res
480
481 test {SORT direct, numeric, against the newly created list} {
482 $r sort tosort
483 } [lsort -integer $res]
484
485 test {SORT decreasing sort} {
486 $r sort tosort {DESC}
487 } [lsort -decreasing -integer $res]
488
489 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
490 set start [clock clicks -milliseconds]
491 for {set i 0} {$i < 100} {incr i} {
492 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
493 }
494 set elapsed [expr [clock clicks -milliseconds]-$start]
495 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
496 flush stdout
497 format {}
498 } {}
499
500 test {SORT speed, sorting 10000 elements list directly, 100 times} {
501 set start [clock clicks -milliseconds]
502 for {set i 0} {$i < 100} {incr i} {
503 set sorted [$r sort tosort {LIMIT 0 10}]
504 }
505 set elapsed [expr [clock clicks -milliseconds]-$start]
506 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
507 flush stdout
508 format {}
509 } {}
510
511 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
512 set start [clock clicks -milliseconds]
513 for {set i 0} {$i < 100} {incr i} {
514 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
515 }
516 set elapsed [expr [clock clicks -milliseconds]-$start]
517 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
518 flush stdout
519 format {}
520 } {}
521
522 test {SORT regression for issue #19, sorting floats} {
523 $r flushdb
524 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
525 $r lpush mylist $x
526 }
527 $r sort mylist
528 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
529
530 test {LREM, remove all the occurrences} {
531 $r flushall
532 $r rpush mylist foo
533 $r rpush mylist bar
534 $r rpush mylist foobar
535 $r rpush mylist foobared
536 $r rpush mylist zap
537 $r rpush mylist bar
538 $r rpush mylist test
539 $r rpush mylist foo
540 set res [$r lrem mylist 0 bar]
541 list [$r lrange mylist 0 -1] $res
542 } {{foo foobar foobared zap test foo} 2}
543
544 test {LREM, remove the first occurrence} {
545 set res [$r lrem mylist 1 foo]
546 list [$r lrange mylist 0 -1] $res
547 } {{foobar foobared zap test foo} 1}
548
549 test {LREM, remove non existing element} {
550 set res [$r lrem mylist 1 nosuchelement]
551 list [$r lrange mylist 0 -1] $res
552 } {{foobar foobared zap test foo} 0}
553
554 test {LREM, starting from tail with negative count} {
555 $r flushall
556 $r rpush mylist foo
557 $r rpush mylist bar
558 $r rpush mylist foobar
559 $r rpush mylist foobared
560 $r rpush mylist zap
561 $r rpush mylist bar
562 $r rpush mylist test
563 $r rpush mylist foo
564 $r rpush mylist foo
565 set res [$r lrem mylist -1 bar]
566 list [$r lrange mylist 0 -1] $res
567 } {{foo bar foobar foobared zap test foo foo} 1}
568
569 test {LREM, starting from tail with negative count (2)} {
570 set res [$r lrem mylist -2 foo]
571 list [$r lrange mylist 0 -1] $res
572 } {{foo bar foobar foobared zap test} 2}
573
574 test {MGET} {
575 $r flushall
576 $r set foo BAR
577 $r set bar FOO
578 $r mget foo bar
579 } {BAR FOO}
580
581 test {MGET against non existing key} {
582 $r mget foo baazz bar
583 } {BAR {} FOO}
584
585 test {MGET against non-string key} {
586 $r sadd myset ciao
587 $r sadd myset bau
588 $r mget foo baazz bar myset
589 } {BAR {} FOO {}}
590
591 # Leave the user with a clean DB before to exit
592 test {FLUSHALL} {
593 $r flushall
594 $r dbsize
595 } {0}
596
597 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
598 if {$::failed > 0} {
599 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
600 }
601 close $fd
602 }
603
604 proc stress {} {
605 set r [redis]
606 $r flushall
607 while 1 {
608 set randkey [expr int(rand()*10000)]
609 set randval [expr int(rand()*10000)]
610 set randidx0 [expr int(rand()*10)]
611 set randidx1 [expr int(rand()*10)]
612 set cmd [expr int(rand()*10)]
613 catch {
614 if {$cmd == 0} {$r set $randkey $randval}
615 if {$cmd == 1} {$r get $randkey}
616 if {$cmd == 2} {$r incr $randkey}
617 if {$cmd == 3} {$r lpush $randkey $randval}
618 if {$cmd == 4} {$r rpop $randkey}
619 if {$cmd == 5} {$r del $randkey}
620 if {$cmd == 6} {$r lrange $randkey $randidx0 $randidx1}
621 if {$cmd == 7} {$r ltrim $randkey $randidx0 $randidx1}
622 if {$cmd == 8} {$r lindex $randkey $randidx0}
623 if {$cmd == 9} {$r lset $randkey $randidx0 $randval}
624 }
625 flush stdout
626 }
627 $r close
628 }
629
630 if {[llength $argv] == 0} {
631 main 127.0.0.1 6379
632 } elseif {[llength $argv] == 1 && [lindex $argv 0] eq {stress}} {
633 stress
634 } else {
635 main [lindex $argv 0] [lindex $argv 1]
636 }