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