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