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
(left
)} {
125 catch {r
incr novar
} err
129 test
{INCR fails against key with spaces
(right
)} {
131 catch {r
incr novar
} err
135 test
{INCR fails against key with spaces
(both
)} {
137 catch {r
incr novar
} err
141 test
{INCR fails against a key holding a
list} {
143 catch {r
incr mylist
} err
148 test
{DECRBY over
32bit value with over
32bit increment
, negative res
} {
149 r
set novar
17179869184
150 r decrby novar
17179869185
153 test
{INCRBYFLOAT against non existing key
} {
155 list [r incrbyfloat novar
1] [r get novar
] [r incrbyfloat novar
0.25] \
159 test
{INCRBYFLOAT against key originally
set with SET
} {
161 r incrbyfloat novar
1.5
164 test
{INCRBYFLOAT over
32bit value
} {
165 r
set novar
17179869184
166 r incrbyfloat novar
1.5
169 test
{INCRBYFLOAT over
32bit value with over
32bit increment
} {
170 r
set novar
17179869184
171 r incrbyfloat novar
17179869184
174 test
{INCRBYFLOAT fails against key with spaces
(left
)} {
177 catch {r incrbyfloat novar
1.0} err
181 test
{INCRBYFLOAT fails against key with spaces
(right
)} {
184 catch {r incrbyfloat novar
1.0} err
188 test
{INCRBYFLOAT fails against key with spaces
(both
)} {
191 catch {r incrbyfloat novar
1.0} err
195 test
{INCRBYFLOAT fails against a key holding a
list} {
199 catch {r incrbyfloat mylist
1.0} err
204 test
{INCRBYFLOAT does not allow NaN or Infinity
} {
207 catch {r incrbyfloat foo
+inf
} err
209 # p.s. no way I can force NaN to test it from the API because
210 # there is no way to increment / decrement by infinity nor to
212 } {ERR
*would produce
*}
214 test
{INCRBYFLOAT decrement
} {
216 r incrbyfloat foo
-1.256
219 test
"SETNX target key missing" {
221 assert_equal
1 [r setnx novar foobared
]
222 assert_equal
"foobared" [r get novar
]
225 test
"SETNX target key exists" {
227 assert_equal
0 [r setnx novar blabla
]
228 assert_equal
"foobared" [r get novar
]
231 test
"SETNX against not-expired volatile key" {
234 assert_equal
0 [r setnx x
20]
235 assert_equal
10 [r get x
]
238 test
"SETNX against expired volatile key" {
239 # Make it very unlikely for the key this test uses to be expired by the
240 # active expiry cycle. This is tightly coupled to the implementation of
241 # active expiry and dbAdd() but currently the only way to test that
242 # SETNX expires a key when it should have been.
243 for {set x
0} {$x < 9999} {incr x
} {
244 r setex key-
$x 3600 value
247 # This will be one of 10000 expiring keys. A cycle is executed every
248 # 100ms, sampling 10 keys for being expired or not. This key will be
249 # expired for at most 1s when we wait 2s, resulting in a total sample
250 # of 100 keys. The probability of the success of this test being a
251 # false positive is therefore approx. 1%.
255 # Wait for the key to expire
258 assert_equal
1 [r setnx x
20]
259 assert_equal
20 [r get x
]
265 append res
[r exists newkey
]
267 append res
[r exists newkey
]
270 test
{Zero length value in key. SET
/GET
/EXISTS
} {
272 set res
[r get emptykey
]
273 append res
[r exists emptykey
]
275 append res
[r exists emptykey
]
278 test
{Commands pipelining
} {
280 puts -nonewline $fd "SET k1 xyzk\r\nGET k1\r\nPING\r\n"
283 append res
[string match OK
* [::redis::redis_read_reply $fd]]
284 append res
[::redis::redis_read_reply $fd]
285 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
289 test
{Non existing command
} {
290 catch {r foobaredcommand
} err
291 string match ERR
* $err
294 test
{RENAME basic usage
} {
296 r
rename mykey mykey1
297 r
rename mykey1 mykey2
301 test
{RENAME
source key should no longer exist
} {
305 test
{RENAME against already existing key
} {
308 r
rename mykey2 mykey
309 set res
[r get mykey
]
310 append res
[r exists mykey2
]
313 test
{RENAMENX basic usage
} {
317 r renamenx mykey mykey2
318 set res
[r get mykey2
]
319 append res
[r exists mykey
]
322 test
{RENAMENX against already existing key
} {
325 r renamenx mykey mykey2
328 test
{RENAMENX against already existing key
(2)} {
329 set res
[r get mykey
]
330 append res
[r get mykey2
]
333 test
{RENAME against non existing
source key
} {
334 catch {r
rename nokey foobar
} err
338 test
{RENAME where
source and dest key is the same
} {
339 catch {r
rename mykey mykey
} err
343 test
{RENAME with volatile key
, should move the TTL as well
} {
347 assert
{[r ttl mykey
] > 95 && [r ttl mykey
] <= 100}
348 r
rename mykey mykey2
349 assert
{[r ttl mykey2
] > 95 && [r ttl mykey2
] <= 100}
352 test
{RENAME with volatile key
, should not inherit TTL of target key
} {
357 assert
{[r ttl mykey
] == -1 && [r ttl mykey2
] > 0}
358 r
rename mykey mykey2
362 test
{DEL all keys again
(DB
0)} {
363 foreach key
[r keys
*] {
369 test
{DEL all keys again
(DB
1)} {
371 foreach key
[r keys
*] {
379 test
{MOVE basic usage
} {
383 lappend res
[r exists mykey
]
384 lappend res
[r dbsize
]
386 lappend res
[r get mykey
]
387 lappend res
[r dbsize
]
390 } [list 0 0 foobar
1]
392 test
{MOVE against key existing in the target DB
} {
397 test
{SET
/GET keys in different DBs
} {
405 lappend res
[r get a
]
406 lappend res
[r get b
]
408 lappend res
[r get a
]
409 lappend res
[r get b
]
412 } {hello world foo bared
}
421 test
{MGET against non existing key
} {
425 test
{MGET against non-string key
} {
428 r mget foo baazz bar myset
437 for {set i
0} {$i < 100} {incr i
} {
438 set rkey
[r randomkey
]
439 if {$rkey eq
{foo
}} {
442 if {$rkey eq
{bar
}} {
446 list $foo_seen $bar_seen
449 test
{RANDOMKEY against empty DB
} {
454 test
{RANDOMKEY regression
1} {
461 test
{GETSET
(set new value
)} {
462 list [r getset foo xyz
] [r get foo
]
465 test
{GETSET
(replace old value
)} {
467 list [r getset foo xyz
] [r get foo
]
470 test
{MSET base case
} {
471 r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
473 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
475 test
{MSET wrong number of args
} {
476 catch {r mset x
10 y
"foo bar" z
} err
480 test
{MSETNX with already existent key
} {
481 list [r msetnx x1 xxx y2 yyy x
20] [r exists x1
] [r exists y2
]
484 test
{MSETNX with not existing keys
} {
485 list [r msetnx x1 xxx y2 yyy
] [r get x1
] [r get y2
]
488 test
"STRLEN against non-existing key" {
489 assert_equal
0 [r strlen notakey
]
492 test
"STRLEN against integer-encoded value" {
494 assert_equal
4 [r strlen myinteger
]
497 test
"STRLEN against plain string" {
498 r
set mystring
"foozzz0123456789 baz"
499 assert_equal
20 [r strlen mystring
]
502 test
"SETBIT against non-existing key" {
504 assert_equal
0 [r setbit mykey
1 1]
505 assert_equal
[binary format B
* 01000000] [r get mykey
]
508 test
"SETBIT against string-encoded key" {
509 # Ascii "@" is integer 64 = 01 00 00 00
512 assert_equal
0 [r setbit mykey
2 1]
513 assert_equal
[binary format B
* 01100000] [r get mykey
]
514 assert_equal
1 [r setbit mykey
1 0]
515 assert_equal
[binary format B
* 00100000] [r get mykey
]
518 test
"SETBIT against integer-encoded key" {
519 # Ascii "1" is integer 49 = 00 11 00 01
521 assert_encoding int mykey
523 assert_equal
0 [r setbit mykey
6 1]
524 assert_equal
[binary format B
* 00110011] [r get mykey
]
525 assert_equal
1 [r setbit mykey
2 0]
526 assert_equal
[binary format B
* 00010011] [r get mykey
]
529 test
"SETBIT against key with wrong type" {
532 assert_error
"*wrong kind*" {r setbit mykey
0 1}
535 test
"SETBIT with out of range bit offset" {
537 assert_error
"*out of range*" {r setbit mykey
[expr 4*1024*1024*1024] 1}
538 assert_error
"*out of range*" {r setbit mykey
-1 1}
541 test
"SETBIT with non-bit argument" {
543 assert_error
"*out of range*" {r setbit mykey
0 -1}
544 assert_error
"*out of range*" {r setbit mykey
0 2}
545 assert_error
"*out of range*" {r setbit mykey
0 10}
546 assert_error
"*out of range*" {r setbit mykey
0 20}
549 test
"SETBIT fuzzing" {
554 for {set i
0} {$i < 2000} {incr i
} {
555 set bitnum
[randomInt
$len]
556 set bitval
[randomInt
2]
557 set fmt
[format "%%-%ds%%d%%-s" $bitnum]
558 set head
[string range
$str 0 $bitnum-1]
559 set tail
[string range
$str $bitnum+1 end
]
560 set str
[string map
{" " 0} [format $fmt $head $bitval $tail]]
562 r setbit mykey
$bitnum $bitval
563 assert_equal
[binary format B
* $str] [r get mykey
]
567 test
"GETBIT against non-existing key" {
569 assert_equal
0 [r getbit mykey
0]
572 test
"GETBIT against string-encoded key" {
573 # Single byte with 2nd and 3rd bit set
577 assert_equal
0 [r getbit mykey
0]
578 assert_equal
1 [r getbit mykey
1]
579 assert_equal
1 [r getbit mykey
2]
580 assert_equal
0 [r getbit mykey
3]
583 assert_equal
0 [r getbit mykey
8]
584 assert_equal
0 [r getbit mykey
100]
585 assert_equal
0 [r getbit mykey
10000]
588 test
"GETBIT against integer-encoded key" {
590 assert_encoding int mykey
592 # Ascii "1" is integer 49 = 00 11 00 01
593 assert_equal
0 [r getbit mykey
0]
594 assert_equal
0 [r getbit mykey
1]
595 assert_equal
1 [r getbit mykey
2]
596 assert_equal
1 [r getbit mykey
3]
599 assert_equal
0 [r getbit mykey
8]
600 assert_equal
0 [r getbit mykey
100]
601 assert_equal
0 [r getbit mykey
10000]
604 test
"SETRANGE against non-existing key" {
606 assert_equal
3 [r setrange mykey
0 foo
]
607 assert_equal
"foo" [r get mykey
]
610 assert_equal
0 [r setrange mykey
0 ""]
611 assert_equal
0 [r exists mykey
]
614 assert_equal
4 [r setrange mykey
1 foo
]
615 assert_equal
"\000foo" [r get mykey
]
618 test
"SETRANGE against string-encoded key" {
620 assert_equal
3 [r setrange mykey
0 b
]
621 assert_equal
"boo" [r get mykey
]
624 assert_equal
3 [r setrange mykey
0 ""]
625 assert_equal
"foo" [r get mykey
]
628 assert_equal
3 [r setrange mykey
1 b
]
629 assert_equal
"fbo" [r get mykey
]
632 assert_equal
7 [r setrange mykey
4 bar
]
633 assert_equal
"foo\000bar" [r get mykey
]
636 test
"SETRANGE against integer-encoded key" {
638 assert_encoding int mykey
639 assert_equal
4 [r setrange mykey
0 2]
640 assert_encoding raw mykey
641 assert_equal
2234 [r get mykey
]
643 # Shouldn't change encoding when nothing is set
645 assert_encoding int mykey
646 assert_equal
4 [r setrange mykey
0 ""]
647 assert_encoding int mykey
648 assert_equal
1234 [r get mykey
]
651 assert_encoding int mykey
652 assert_equal
4 [r setrange mykey
1 3]
653 assert_encoding raw mykey
654 assert_equal
1334 [r get mykey
]
657 assert_encoding int mykey
658 assert_equal
6 [r setrange mykey
5 2]
659 assert_encoding raw mykey
660 assert_equal
"1234\0002" [r get mykey
]
663 test
"SETRANGE against key with wrong type" {
666 assert_error
"*wrong kind*" {r setrange mykey
0 bar
}
669 test
"SETRANGE with out of range offset" {
671 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
674 assert_error
"*out of range*" {r setrange mykey
-1 world
}
675 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
678 test
"GETRANGE against non-existing key" {
680 assert_equal
"" [r getrange mykey
0 -1]
683 test
"GETRANGE against string value" {
684 r
set mykey
"Hello World"
685 assert_equal
"Hell" [r getrange mykey
0 3]
686 assert_equal
"Hello World" [r getrange mykey
0 -1]
687 assert_equal
"orld" [r getrange mykey
-4 -1]
688 assert_equal
"" [r getrange mykey
5 3]
689 assert_equal
" World" [r getrange mykey
5 5000]
690 assert_equal
"Hello World" [r getrange mykey
-5000 10000]
693 test
"GETRANGE against integer-encoded value" {
695 assert_equal
"123" [r getrange mykey
0 2]
696 assert_equal
"1234" [r getrange mykey
0 -1]
697 assert_equal
"234" [r getrange mykey
-3 -1]
698 assert_equal
"" [r getrange mykey
5 3]
699 assert_equal
"4" [r getrange mykey
3 5000]
700 assert_equal
"1234" [r getrange mykey
-5000 10000]
703 test
"GETRANGE fuzzing" {
704 for {set i
0} {$i < 1000} {incr i
} {
705 r
set bin
[set bin
[randstring
0 1024 binary]]
706 set _start
[set start
[randomInt
1500]]
707 set _end
[set end
[randomInt
1500]]
708 if {$_start < 0} {set _start
"end-[abs($_start)-1]"}
709 if {$_end < 0} {set _end
"end-[abs($_end)-1]"}
710 assert_equal
[string range
$bin $_start $_end] [r getrange bin
$start $end]