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