1 # TODO # test pipelining
3 source client-libraries
/tcl
/redis.tcl
8 proc test
{name code okpattern
} {
9 puts -nonewline [format "%-70s " $name]
11 set retval
[uplevel 1 $code]
12 if {$okpattern eq
$retval ||
[string match
$okpattern $retval]} {
16 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
21 proc main
{server port
} {
22 set r
[redis
$server $port]
25 test
{DEL all keys to start with a clean DB
} {
26 foreach key
[$r keys
*] {$r del
$key}
30 test
{SET and GET an item
} {
35 test
{DEL against a single item
} {
40 test
{KEYS with pattern
} {
41 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
47 test
{KEYS to get all keys
} {
49 } {foo_a foo_b foo_c key_x key_y key_z
}
56 foreach key
[$r keys
*] {$r del
$key}
60 test
{Very big payload in GET
/SET
} {
61 set buf
[string repeat
"abcd" 1000000]
64 } [string repeat
"abcd" 1000000]
66 test
{SET
10000 numeric keys and access all them in reverse order
} {
67 for {set x
0} {$x < 10000} {incr x
} {
71 for {set x
9999} {$x >= 0} {incr x
-1} {
77 test
{DBSIZE should be
10001 now
} {
81 test
{INCR against non existing key
} {
83 append res
[$r incr novar
]
84 append res
[$r get novar
]
87 test
{INCR against key created by
incr itself
} {
91 test
{INCR against key originally
set with SET
} {
96 test
{SETNX target key missing
} {
97 $r setnx novar2 foobared
101 test
{SETNX target key exists
} {
102 $r setnx novar2 blabla
109 append res
[$r exists newkey
]
111 append res
[$r exists newkey
]
114 test
{Zero length value in key. SET
/GET
/EXISTS
} {
116 set res
[$r get emptykey
]
117 append res
[$r exists emptykey
]
119 append res
[$r exists emptykey
]
122 test
{Commands pipelining
} {
124 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
127 append res
[string match OK
* [::redis::redis_read_reply $fd]]
128 append res
[::redis::redis_read_reply $fd]
129 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
133 test
{Non existing command
} {
134 catch {$r foobaredcommand
} err
135 string match ERR
* $err
138 test
{Basic LPUSH
, RPUSH
, LLENGTH
, LINDEX
} {
142 set res
[$r llen mylist
]
143 append res
[$r lindex mylist
0]
144 append res
[$r lindex mylist
1]
145 append res
[$r lindex mylist
2]
153 test
{Create a long
list and check every single element with LINDEX
} {
155 for {set i
0} {$i < 1000} {incr i
} {
158 for {set i
0} {$i < 1000} {incr i
} {
159 if {[$r lindex mylist
$i] eq
$i} {incr ok
}
160 if {[$r lindex mylist
[expr (-$i)-1]] eq
[expr 999-$i]} {
167 test
{Test elements with LINDEX in random access
} {
169 for {set i
0} {$i < 1000} {incr i
} {
170 set rint
[expr int
(rand
()*1000)]
171 if {[$r lindex mylist
$rint] eq
$rint} {incr ok
}
172 if {[$r lindex mylist
[expr (-$rint)-1]] eq
[expr 999-$rint]} {
179 test
{LLEN against non-list value
error} {
182 catch {$r llen mylist
} err
186 test
{LINDEX against non-list value
error} {
187 catch {$r lindex mylist
0} err
191 test
{LPUSH against non-list value
error} {
192 catch {$r lpush mylist
0} err
196 test
{RPUSH against non-list value
error} {
197 catch {$r rpush mylist
0} err
201 test
{RENAME basic usage
} {
203 $r rename mykey mykey1
204 $r rename mykey1 mykey2
208 test
{RENAME
source key should no longer exist
} {
212 test
{RENAME against already existing key
} {
215 $r rename mykey2 mykey
216 set res
[$r get mykey
]
217 append res
[$r exists mykey2
]
220 test
{RENAMENX basic usage
} {
224 $r renamenx mykey mykey2
225 set res
[$r get mykey2
]
226 append res
[$r exists mykey
]
229 test
{RENAMENX against already existing key
} {
232 $r renamenx mykey mykey2
235 test
{RENAMENX against already existing key
(2)} {
236 set res
[$r get mykey
]
237 append res
[$r get mykey2
]
240 test
{RENAME against non existing
source key
} {
241 catch {$r rename nokey foobar
} err
245 test
{RENAME where
source and dest key is the same
} {
246 catch {$r rename mykey mykey
} err
250 test
{DEL all keys again
(DB
0)} {
251 foreach key
[$r keys
*] {
257 test
{DEL all keys again
(DB
1)} {
259 foreach key
[$r keys
*] {
267 test
{MOVE basic usage
} {
271 lappend res
[$r exists mykey
]
272 lappend res
[$r dbsize
]
274 lappend res
[$r get mykey
]
275 lappend res
[$r dbsize
]
278 } [list 0 0 foobar
1]
280 test
{MOVE against key existing in the target DB
} {
285 test
{SET
/GET keys in different DBs
} {
293 lappend res
[$r get a
]
294 lappend res
[$r get b
]
296 lappend res
[$r get a
]
297 lappend res
[$r get b
]
300 } {hello world foo bared
}
302 test
{Basic LPOP
/RPOP
} {
307 list [$r lpop mylist
] [$r rpop mylist
] [$r lpop mylist
] [$r llen mylist
]
310 test
{LPOP
/RPOP against empty
list} {
314 test
{LPOP against non
list value
} {
316 catch {$r lpop notalist
} err
320 test
{Mass LPUSH
/LPOP
} {
322 for {set i
0} {$i < 1000} {incr i
} {
327 for {set i
0} {$i < 500} {incr i
} {
328 incr sum2
[$r lpop mylist
]
329 incr sum2
[$r rpop mylist
]
334 test
{LRANGE basics
} {
335 for {set i
0} {$i < 10} {incr i
} {
338 list [$r lrange mylist
1 -2] \
339 [$r lrange mylist
-3 -1] \
340 [$r lrange mylist
4 4]
341 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
343 test
{LRANGE inverted indexes
} {
347 test
{LRANGE out of range indexes including the full
list} {
348 $r lrange mylist
-1000 1000
349 } {0 1 2 3 4 5 6 7 8 9}
351 test
{LRANGE against non existing key
} {
352 $r lrange nosuchkey
0 1
355 test
{LTRIM basics
} {
357 for {set i
0} {$i < 100} {incr i
} {
361 $r lrange mylist
0 -1
366 $r lset mylist
-1 bar
367 $r lrange mylist
0 -1
370 test
{LSET out of range index
} {
371 catch {$r lset mylist
10 foo
} err
375 test
{LSET against non existing key
} {
376 catch {$r lset nosuchkey
10 foo
} err
380 test
{LSET against non
list value
} {
382 catch {$r lset nolist
0 foo
} err
386 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
} {
389 list [$r scard myset
] [$r sismember myset foo
] \
390 [$r sismember myset bar
] [$r sismember myset bla
] \
391 [lsort [$r smembers myset
]]
392 } {2 1 1 0 {bar foo
}}
394 test
{SADD adding the same element multiple times
} {
401 test
{SADD against non
set} {
402 catch {$r sadd mylist foo
} err
409 lsort [$r smembers myset
]
412 test
{Mass SADD and SINTER with two sets
} {
413 for {set i
0} {$i < 1000} {incr i
} {
415 $r sadd set2
[expr $i+995]
417 lsort [$r sinter set1 set2
]
418 } {995 996 997 998 999}
420 test
{SINTERSTORE with two sets
} {
421 $r sinterstore setres set1 set2
422 lsort [$r smembers setres
]
423 } {995 996 997 998 999}
425 test
{SINTER against three sets
} {
430 lsort [$r sinter set1 set2 set3
]
433 test
{SINTERSTORE with three sets
} {
434 $r sinterstore setres set1 set2 set3
435 lsort [$r smembers setres
]
438 test
{SAVE
- make sure there are all the types as values
} {
439 $r lpush mysavelist hello
440 $r lpush mysavelist world
442 $r set mynormalkey
{blablablba
}
446 test
{Create a random
list} {
448 array set seenrand
{}
449 for {set i
0} {$i < 10000} {incr i
} {
451 # Make sure all the weights are different because
452 # Redis does not use a stable sort but Tcl does.
453 set rint
[expr int
(rand
()*1000000)]
454 if {![info exists seenrand
($rint)]} break
456 set seenrand
($rint) x
458 $r set weight_
$i $rint
459 lappend tosort
[list $i $rint]
461 set sorted
[lsort -index 1 -real $tosort]
463 for {set i
0} {$i < 10000} {incr i
} {
464 lappend res
[lindex $sorted $i 0]
469 test
{SORT with BY against the newly created
list} {
470 $r sort tosort
{BY weight_
*}
473 test
{SORT direct
, numeric
, against the newly created
list} {
475 } [lsort -integer $res]
477 test
{SORT decreasing sort
} {
478 $r sort tosort
{DESC
}
479 } [lsort -decreasing -integer $res]
481 test
{SORT speed
, sorting
10000 elements
list using BY
, 100 times
} {
482 set start
[clock clicks
-milliseconds]
483 for {set i
0} {$i < 100} {incr i
} {
484 set sorted
[$r sort tosort
{BY weight_
* LIMIT
0 10}]
486 set elapsed
[expr [clock clicks
-milliseconds]-$start]
487 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
492 test
{SORT speed
, sorting
10000 elements
list directly
, 100 times
} {
493 set start
[clock clicks
-milliseconds]
494 for {set i
0} {$i < 100} {incr i
} {
495 set sorted
[$r sort tosort
{LIMIT
0 10}]
497 set elapsed
[expr [clock clicks
-milliseconds]-$start]
498 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
503 test
{SORT speed
, pseudo-sorting
10000 elements
list, BY
<const
>, 100 times
} {
504 set start
[clock clicks
-milliseconds]
505 for {set i
0} {$i < 100} {incr i
} {
506 set sorted
[$r sort tosort
{BY nokey LIMIT
0 10}]
508 set elapsed
[expr [clock clicks
-milliseconds]-$start]
509 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
514 test
{SORT regression
for issue
#19, sorting floats} {
516 foreach x
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
520 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
522 test
{LREM
, remove all the occurrences
} {
526 $r rpush mylist foobar
527 $r rpush mylist foobared
532 set res
[$r lrem mylist
0 bar
]
533 list [$r lrange mylist
0 -1] $res
534 } {{foo foobar foobared zap test foo
} 2}
536 test
{LREM
, remove the first occurrence
} {
537 set res
[$r lrem mylist
1 foo
]
538 list [$r lrange mylist
0 -1] $res
539 } {{foobar foobared zap test foo
} 1}
541 test
{LREM
, remove non existing element
} {
542 set res
[$r lrem mylist
1 nosuchelement
]
543 list [$r lrange mylist
0 -1] $res
544 } {{foobar foobared zap test foo
} 0}
546 test
{LREM
, starting from tail with negative count
} {
550 $r rpush mylist foobar
551 $r rpush mylist foobared
557 set res
[$r lrem mylist
-1 bar
]
558 list [$r lrange mylist
0 -1] $res
559 } {{foo bar foobar foobared zap test foo foo
} 1}
561 test
{LREM
, starting from tail with negative count
(2)} {
562 set res
[$r lrem mylist
-2 foo
]
563 list [$r lrange mylist
0 -1] $res
564 } {{foo bar foobar foobared zap test
} 2}
573 test
{MGET against non existing key
} {
574 $r mget foo baazz bar
577 test
{MGET against non-string key
} {
580 $r mget foo baazz bar myset
583 # Leave the user with a clean DB before to exit
589 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
591 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
600 set randkey
[expr int
(rand
()*10000)]
601 set randval
[expr int
(rand
()*10000)]
602 set randidx0
[expr int
(rand
()*10)]
603 set randidx1
[expr int
(rand
()*10)]
604 set cmd
[expr int
(rand
()*10)]
606 if {$cmd == 0} {$r set $randkey $randval}
607 if {$cmd == 1} {$r get
$randkey}
608 if {$cmd == 2} {$r incr $randkey}
609 if {$cmd == 3} {$r lpush
$randkey $randval}
610 if {$cmd == 4} {$r rpop
$randkey}
611 if {$cmd == 5} {$r del
$randkey}
612 if {$cmd == 6} {$r lrange $randkey $randidx0 $randidx1}
613 if {$cmd == 7} {$r ltrim
$randkey $randidx0 $randidx1}
614 if {$cmd == 8} {$r lindex $randkey $randidx0}
615 if {$cmd == 9} {$r lset $randkey $randidx0 $randval}
622 if {[llength $argv] == 0} {
624 } elseif
{[llength $argv] == 1 && [lindex $argv 0] eq
{stress
}} {
627 main
[lindex $argv 0] [lindex $argv 1]