1 start_server
{tags
{"basic"}} {
2 test
{DEL all keys to start with a clean DB
} {
3 foreach key
[r keys
*] {r del
$key}
7 test
{SET and GET an item
} {
12 test
{SET and GET an empty item
} {
17 test
{DEL against a single item
} {
26 list [r del foo1 foo2 foo3 foo4
] [r mget foo1 foo2 foo3
]
29 test
{KEYS with pattern
} {
30 foreach key
{key_x key_y key_z foo_a foo_b foo_c
} {
36 test
{KEYS to get all keys
} {
38 } {foo_a foo_b foo_c key_x key_y key_z
}
45 foreach key
[r keys
*] {r del
$key}
49 test
{Very big payload in GET
/SET
} {
50 set buf
[string repeat
"abcd" 1000000]
53 } [string repeat
"abcd" 1000000]
56 test
{Very big payload random access
} {
59 for {set j
0} {$j < 100} {incr j
} {
60 set size
[expr 1+[randomInt
100000]]
61 set buf
[string repeat
"pl-$j" $size]
63 r
set bigpayload_
$j $buf
65 for {set j
0} {$j < 1000} {incr j
} {
66 set index
[randomInt
100]
67 set buf
[r get bigpayload_
$index]
68 if {$buf != $payload($index)} {
69 set err
"Values differ: I set '$payload($index)' but I read back '$buf'"
77 test
{SET
10000 numeric keys and access all them in reverse order
} {
79 for {set x
0} {$x < 10000} {incr x
} {
83 for {set x
9999} {$x >= 0} {incr x
-1} {
86 set err
"Eleemnt at position $x is $val instead of $x"
93 test
{DBSIZE should be
10101 now
} {
98 test
{INCR against non existing key
} {
100 append res
[r
incr novar
]
101 append res
[r get novar
]
104 test
{INCR against key created by
incr itself
} {
108 test
{INCR against key originally
set with SET
} {
113 test
{INCR over
32bit value
} {
114 r
set novar
17179869184
118 test
{INCRBY over
32bit value with over
32bit increment
} {
119 r
set novar
17179869184
120 r incrby novar
17179869184
123 test
{INCR fails against key with spaces
(no integer encoded
)} {
125 catch {r
incr novar
} err
129 test
{INCR fails against a key holding a
list} {
131 catch {r
incr mylist
} err
136 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
137 r
set novar
17179869184
138 r decrby novar
17179869185
141 test
{INCRBYFLOAT against non existing key
} {
143 list [r incrbyfloat novar
1] [r get novar
] [r incrbyfloat novar
0.25] \
147 test
{INCRBYFLOAT against key originally
set with SET
} {
149 r incrbyfloat novar
1.5
152 test
{INCRBYFLOAT over
32bit value
} {
153 r
set novar
17179869184
154 r incrbyfloat novar
1.5
157 test
{INCRBYFLOAT over
32bit value with over
32bit increment
} {
158 r
set novar
17179869184
159 r incrbyfloat novar
17179869184
162 test
{INCRBYFLOAT fails against key with spaces
(left
)} {
165 catch {r incrbyfloat novar
1.0} err
169 test
{INCRBYFLOAT fails against key with spaces
(right
)} {
172 catch {r incrbyfloat novar
1.0} err
176 test
{INCRBYFLOAT fails against key with spaces
(both
)} {
179 catch {r incrbyfloat novar
1.0} err
183 test
{INCRBYFLOAT fails against a key holding a
list} {
187 catch {r incrbyfloat mylist
1.0} err
192 test
{INCRBYFLOAT does not allow NaN or Infinity
} {
195 catch {r incrbyfloat foo
+inf
} err
197 # p.s. no way I can force NaN to test it from the API because
198 # there is no way to increment / decrement by infinity nor to
200 } {ERR
*would produce
*}
202 test
{INCRBYFLOAT decrement
} {
204 r incrbyfloat foo
-1.256
207 test
"SETNX target key missing" {
209 assert_equal
1 [r setnx novar foobared
]
210 assert_equal
"foobared" [r get novar
]
213 test
"SETNX target key exists" {
215 assert_equal
0 [r setnx novar blabla
]
216 assert_equal
"foobared" [r get novar
]
219 test
"SETNX against not-expired volatile key" {
222 assert_equal
0 [r setnx x
20]
223 assert_equal
10 [r get x
]
226 test
"SETNX against expired volatile key" {
227 # Make it very unlikely for the key this test uses to be expired by the
228 # active expiry cycle. This is tightly coupled to the implementation of
229 # active expiry and dbAdd() but currently the only way to test that
230 # SETNX expires a key when it should have been.
231 for {set x
0} {$x < 9999} {incr x
} {
232 r setex key-
$x 3600 value
235 # This will be one of 10000 expiring keys. A cycle is executed every
236 # 100ms, sampling 10 keys for being expired or not. This key will be
237 # expired for at most 1s when we wait 2s, resulting in a total sample
238 # of 100 keys. The probability of the success of this test being a
239 # false positive is therefore approx. 1%.
243 # Wait for the key to expire
246 assert_equal
1 [r setnx x
20]
247 assert_equal
20 [r get x
]
253 append res
[r exists newkey
]
255 append res
[r exists newkey
]
258 test
{Zero length value in key. SET
/GET
/EXISTS
} {
260 set res
[r get emptykey
]
261 append res
[r exists emptykey
]
263 append res
[r exists emptykey
]
266 test
{Commands pipelining
} {
268 puts -nonewline $fd "SET k1 xyzk\r\nGET k1\r\nPING\r\n"
271 append res
[string match OK
* [::redis::redis_read_reply $fd]]
272 append res
[::redis::redis_read_reply $fd]
273 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
277 test
{Non existing command
} {
278 catch {r foobaredcommand
} err
279 string match ERR
* $err
282 test
{RENAME basic usage
} {
284 r
rename mykey mykey1
285 r
rename mykey1 mykey2
289 test
{RENAME
source key should no longer exist
} {
293 test
{RENAME against already existing key
} {
296 r
rename mykey2 mykey
297 set res
[r get mykey
]
298 append res
[r exists mykey2
]
301 test
{RENAMENX basic usage
} {
305 r renamenx mykey mykey2
306 set res
[r get mykey2
]
307 append res
[r exists mykey
]
310 test
{RENAMENX against already existing key
} {
313 r renamenx mykey mykey2
316 test
{RENAMENX against already existing key
(2)} {
317 set res
[r get mykey
]
318 append res
[r get mykey2
]
321 test
{RENAME against non existing
source key
} {
322 catch {r
rename nokey foobar
} err
326 test
{RENAME where
source and dest key is the same
} {
327 catch {r
rename mykey mykey
} err
331 test
{RENAME with volatile key
, should move the TTL as well
} {
335 assert
{[r ttl mykey
] > 95 && [r ttl mykey
] <= 100}
336 r
rename mykey mykey2
337 assert
{[r ttl mykey2
] > 95 && [r ttl mykey2
] <= 100}
340 test
{RENAME with volatile key
, should not inherit TTL of target key
} {
345 assert
{[r ttl mykey
] == -1 && [r ttl mykey2
] > 0}
346 r
rename mykey mykey2
350 test
{DEL all keys again
(DB
0)} {
351 foreach key
[r keys
*] {
357 test
{DEL all keys again
(DB
1)} {
359 foreach key
[r keys
*] {
367 test
{MOVE basic usage
} {
371 lappend res
[r exists mykey
]
372 lappend res
[r dbsize
]
374 lappend res
[r get mykey
]
375 lappend res
[r dbsize
]
378 } [list 0 0 foobar
1]
380 test
{MOVE against key existing in the target DB
} {
385 test
{SET
/GET keys in different DBs
} {
393 lappend res
[r get a
]
394 lappend res
[r get b
]
396 lappend res
[r get a
]
397 lappend res
[r get b
]
400 } {hello world foo bared
}
409 test
{MGET against non existing key
} {
413 test
{MGET against non-string key
} {
416 r mget foo baazz bar myset
425 for {set i
0} {$i < 100} {incr i
} {
426 set rkey
[r randomkey
]
427 if {$rkey eq
{foo
}} {
430 if {$rkey eq
{bar
}} {
434 list $foo_seen $bar_seen
437 test
{RANDOMKEY against empty DB
} {
442 test
{RANDOMKEY regression
1} {
449 test
{GETSET
(set new value
)} {
450 list [r getset foo xyz
] [r get foo
]
453 test
{GETSET
(replace old value
)} {
455 list [r getset foo xyz
] [r get foo
]
458 test
{MSET base case
} {
459 r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
461 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
463 test
{MSET wrong number of args
} {
464 catch {r mset x
10 y
"foo bar" z
} err
468 test
{MSETNX with already existent key
} {
469 list [r msetnx x1 xxx y2 yyy x
20] [r exists x1
] [r exists y2
]
472 test
{MSETNX with not existing keys
} {
473 list [r msetnx x1 xxx y2 yyy
] [r get x1
] [r get y2
]
476 test
"STRLEN against non-existing key" {
477 assert_equal
0 [r strlen notakey
]
480 test
"STRLEN against integer-encoded value" {
482 assert_equal
4 [r strlen myinteger
]
485 test
"STRLEN against plain string" {
486 r
set mystring
"foozzz0123456789 baz"
487 assert_equal
20 [r strlen mystring
]
490 test
"SETBIT against non-existing key" {
492 assert_equal
0 [r setbit mykey
1 1]
493 assert_equal
[binary format B
* 01000000] [r get mykey
]
496 test
"SETBIT against string-encoded key" {
497 # Ascii "@" is integer 64 = 01 00 00 00
500 assert_equal
0 [r setbit mykey
2 1]
501 assert_equal
[binary format B
* 01100000] [r get mykey
]
502 assert_equal
1 [r setbit mykey
1 0]
503 assert_equal
[binary format B
* 00100000] [r get mykey
]
506 test
"SETBIT against integer-encoded key" {
507 # Ascii "1" is integer 49 = 00 11 00 01
509 assert_encoding int mykey
511 assert_equal
0 [r setbit mykey
6 1]
512 assert_equal
[binary format B
* 00110011] [r get mykey
]
513 assert_equal
1 [r setbit mykey
2 0]
514 assert_equal
[binary format B
* 00010011] [r get mykey
]
517 test
"SETBIT against key with wrong type" {
520 assert_error
"*wrong kind*" {r setbit mykey
0 1}
523 test
"SETBIT with out of range bit offset" {
525 assert_error
"*out of range*" {r setbit mykey
[expr 4*1024*1024*1024] 1}
526 assert_error
"*out of range*" {r setbit mykey
-1 1}
529 test
"SETBIT with non-bit argument" {
531 assert_error
"*out of range*" {r setbit mykey
0 -1}
532 assert_error
"*out of range*" {r setbit mykey
0 2}
533 assert_error
"*out of range*" {r setbit mykey
0 10}
534 assert_error
"*out of range*" {r setbit mykey
0 20}
537 test
"SETBIT fuzzing" {
542 for {set i
0} {$i < 2000} {incr i
} {
543 set bitnum
[randomInt
$len]
544 set bitval
[randomInt
2]
545 set fmt
[format "%%-%ds%%d%%-s" $bitnum]
546 set head
[string range
$str 0 $bitnum-1]
547 set tail
[string range
$str $bitnum+1 end
]
548 set str
[string map
{" " 0} [format $fmt $head $bitval $tail]]
550 r setbit mykey
$bitnum $bitval
551 assert_equal
[binary format B
* $str] [r get mykey
]
555 test
"GETBIT against non-existing key" {
557 assert_equal
0 [r getbit mykey
0]
560 test
"GETBIT against string-encoded key" {
561 # Single byte with 2nd and 3rd bit set
565 assert_equal
0 [r getbit mykey
0]
566 assert_equal
1 [r getbit mykey
1]
567 assert_equal
1 [r getbit mykey
2]
568 assert_equal
0 [r getbit mykey
3]
571 assert_equal
0 [r getbit mykey
8]
572 assert_equal
0 [r getbit mykey
100]
573 assert_equal
0 [r getbit mykey
10000]
576 test
"GETBIT against integer-encoded key" {
578 assert_encoding int mykey
580 # Ascii "1" is integer 49 = 00 11 00 01
581 assert_equal
0 [r getbit mykey
0]
582 assert_equal
0 [r getbit mykey
1]
583 assert_equal
1 [r getbit mykey
2]
584 assert_equal
1 [r getbit mykey
3]
587 assert_equal
0 [r getbit mykey
8]
588 assert_equal
0 [r getbit mykey
100]
589 assert_equal
0 [r getbit mykey
10000]
592 test
"SETRANGE against non-existing key" {
594 assert_equal
3 [r setrange mykey
0 foo
]
595 assert_equal
"foo" [r get mykey
]
598 assert_equal
0 [r setrange mykey
0 ""]
599 assert_equal
0 [r exists mykey
]
602 assert_equal
4 [r setrange mykey
1 foo
]
603 assert_equal
"\000foo" [r get mykey
]
606 test
"SETRANGE against string-encoded key" {
608 assert_equal
3 [r setrange mykey
0 b
]
609 assert_equal
"boo" [r get mykey
]
612 assert_equal
3 [r setrange mykey
0 ""]
613 assert_equal
"foo" [r get mykey
]
616 assert_equal
3 [r setrange mykey
1 b
]
617 assert_equal
"fbo" [r get mykey
]
620 assert_equal
7 [r setrange mykey
4 bar
]
621 assert_equal
"foo\000bar" [r get mykey
]
624 test
"SETRANGE against integer-encoded key" {
626 assert_encoding int mykey
627 assert_equal
4 [r setrange mykey
0 2]
628 assert_encoding raw mykey
629 assert_equal
2234 [r get mykey
]
631 # Shouldn't change encoding when nothing is set
633 assert_encoding int mykey
634 assert_equal
4 [r setrange mykey
0 ""]
635 assert_encoding int mykey
636 assert_equal
1234 [r get mykey
]
639 assert_encoding int mykey
640 assert_equal
4 [r setrange mykey
1 3]
641 assert_encoding raw mykey
642 assert_equal
1334 [r get mykey
]
645 assert_encoding int mykey
646 assert_equal
6 [r setrange mykey
5 2]
647 assert_encoding raw mykey
648 assert_equal
"1234\0002" [r get mykey
]
651 test
"SETRANGE against key with wrong type" {
654 assert_error
"*wrong kind*" {r setrange mykey
0 bar
}
657 test
"SETRANGE with out of range offset" {
659 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
662 assert_error
"*out of range*" {r setrange mykey
-1 world
}
663 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
666 test
"GETRANGE against non-existing key" {
668 assert_equal
"" [r getrange mykey
0 -1]
671 test
"GETRANGE against string value" {
672 r
set mykey
"Hello World"
673 assert_equal
"Hell" [r getrange mykey
0 3]
674 assert_equal
"Hello World" [r getrange mykey
0 -1]
675 assert_equal
"orld" [r getrange mykey
-4 -1]
676 assert_equal
"" [r getrange mykey
5 3]
677 assert_equal
" World" [r getrange mykey
5 5000]
678 assert_equal
"Hello World" [r getrange mykey
-5000 10000]
681 test
"GETRANGE against integer-encoded value" {
683 assert_equal
"123" [r getrange mykey
0 2]
684 assert_equal
"1234" [r getrange mykey
0 -1]
685 assert_equal
"234" [r getrange mykey
-3 -1]
686 assert_equal
"" [r getrange mykey
5 3]
687 assert_equal
"4" [r getrange mykey
3 5000]
688 assert_equal
"1234" [r getrange mykey
-5000 10000]
691 test
"GETRANGE fuzzing" {
692 for {set i
0} {$i < 1000} {incr i
} {
693 r
set bin
[set bin
[randstring
0 1024 binary]]
694 set _start
[set start
[randomInt
1500]]
695 set _end
[set end
[randomInt
1500]]
696 if {$_start < 0} {set _start
"end-[abs($_start)-1]"}
697 if {$_end < 0} {set _end
"end-[abs($_end)-1]"}
698 assert_equal
[string range
$bin $_start $_end] [r getrange bin
$start $end]