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
{SETNX target key missing
} {
142 r setnx novar2 foobared
146 test
{SETNX target key exists
} {
147 r setnx novar2 blabla
151 test
{SETNX against volatile key
} {
154 list [r setnx x
20] [r get x
]
160 append res
[r exists newkey
]
162 append res
[r exists newkey
]
165 test
{Zero length value in key. SET
/GET
/EXISTS
} {
167 set res
[r get emptykey
]
168 append res
[r exists emptykey
]
170 append res
[r exists emptykey
]
173 test
{Commands pipelining
} {
175 puts -nonewline $fd "SET k1 xyzk\r\nGET k1\r\nPING\r\n"
178 append res
[string match OK
* [::redis::redis_read_reply $fd]]
179 append res
[::redis::redis_read_reply $fd]
180 append res
[string match PONG
* [::redis::redis_read_reply $fd]]
184 test
{Non existing command
} {
185 catch {r foobaredcommand
} err
186 string match ERR
* $err
189 test
{RENAME basic usage
} {
191 r
rename mykey mykey1
192 r
rename mykey1 mykey2
196 test
{RENAME
source key should no longer exist
} {
200 test
{RENAME against already existing key
} {
203 r
rename mykey2 mykey
204 set res
[r get mykey
]
205 append res
[r exists mykey2
]
208 test
{RENAMENX basic usage
} {
212 r renamenx mykey mykey2
213 set res
[r get mykey2
]
214 append res
[r exists mykey
]
217 test
{RENAMENX against already existing key
} {
220 r renamenx mykey mykey2
223 test
{RENAMENX against already existing key
(2)} {
224 set res
[r get mykey
]
225 append res
[r get mykey2
]
228 test
{RENAME against non existing
source key
} {
229 catch {r
rename nokey foobar
} err
233 test
{RENAME where
source and dest key is the same
} {
234 catch {r
rename mykey mykey
} err
238 test
{DEL all keys again
(DB
0)} {
239 foreach key
[r keys
*] {
245 test
{DEL all keys again
(DB
1)} {
247 foreach key
[r keys
*] {
255 test
{MOVE basic usage
} {
259 lappend res
[r exists mykey
]
260 lappend res
[r dbsize
]
262 lappend res
[r get mykey
]
263 lappend res
[r dbsize
]
266 } [list 0 0 foobar
1]
268 test
{MOVE against key existing in the target DB
} {
273 test
{SET
/GET keys in different DBs
} {
281 lappend res
[r get a
]
282 lappend res
[r get b
]
284 lappend res
[r get a
]
285 lappend res
[r get b
]
288 } {hello world foo bared
}
297 test
{MGET against non existing key
} {
301 test
{MGET against non-string key
} {
304 r mget foo baazz bar myset
313 for {set i
0} {$i < 100} {incr i
} {
314 set rkey
[r randomkey
]
315 if {$rkey eq
{foo
}} {
318 if {$rkey eq
{bar
}} {
322 list $foo_seen $bar_seen
325 test
{RANDOMKEY against empty DB
} {
330 test
{RANDOMKEY regression
1} {
337 test
{GETSET
(set new value
)} {
338 list [r getset foo xyz
] [r get foo
]
341 test
{GETSET
(replace old value
)} {
343 list [r getset foo xyz
] [r get foo
]
346 test
{MSET base case
} {
347 r mset x
10 y
"foo bar" z
"x x x x x x x\n\n\r\n"
349 } [list 10 {foo bar
} "x x x x x x x\n\n\r\n"]
351 test
{MSET wrong number of args
} {
352 catch {r mset x
10 y
"foo bar" z
} err
356 test
{MSETNX with already existent key
} {
357 list [r msetnx x1 xxx y2 yyy x
20] [r exists x1
] [r exists y2
]
360 test
{MSETNX with not existing keys
} {
361 list [r msetnx x1 xxx y2 yyy
] [r get x1
] [r get y2
]
364 test
"STRLEN against non-existing key" {
365 assert_equal
0 [r strlen notakey
]
368 test
"STRLEN against integer-encoded value" {
370 assert_equal
4 [r strlen myinteger
]
373 test
"STRLEN against plain string" {
374 r
set mystring
"foozzz0123456789 baz"
375 assert_equal
20 [r strlen mystring
]
378 test
"SETBIT against non-existing key" {
380 assert_equal
0 [r setbit mykey
1 1]
381 assert_equal
[binary format B
* 01000000] [r get mykey
]
384 test
"SETBIT against string-encoded key" {
385 # Ascii "@" is integer 64 = 01 00 00 00
388 assert_equal
0 [r setbit mykey
2 1]
389 assert_equal
[binary format B
* 01100000] [r get mykey
]
390 assert_equal
1 [r setbit mykey
1 0]
391 assert_equal
[binary format B
* 00100000] [r get mykey
]
394 test
"SETBIT against integer-encoded key" {
395 # Ascii "1" is integer 49 = 00 11 00 01
397 assert_encoding int mykey
399 assert_equal
0 [r setbit mykey
6 1]
400 assert_equal
[binary format B
* 00110011] [r get mykey
]
401 assert_equal
1 [r setbit mykey
2 0]
402 assert_equal
[binary format B
* 00010011] [r get mykey
]
405 test
"SETBIT against key with wrong type" {
408 assert_error
"*wrong kind*" {r setbit mykey
0 1}
411 test
"SETBIT with out of range bit offset" {
413 assert_error
"*out of range*" {r setbit mykey
[expr 4*1024*1024*1024] 1}
414 assert_error
"*out of range*" {r setbit mykey
-1 1}
417 test
"SETBIT with non-bit argument" {
419 assert_error
"*out of range*" {r setbit mykey
0 -1}
420 assert_error
"*out of range*" {r setbit mykey
0 2}
421 assert_error
"*out of range*" {r setbit mykey
0 10}
422 assert_error
"*out of range*" {r setbit mykey
0 20}
425 test
"SETBIT fuzzing" {
430 for {set i
0} {$i < 2000} {incr i
} {
431 set bitnum
[randomInt
$len]
432 set bitval
[randomInt
2]
433 set fmt
[format "%%-%ds%%d%%-s" $bitnum]
434 set head
[string range
$str 0 $bitnum-1]
435 set tail
[string range
$str $bitnum+1 end
]
436 set str
[string map
{" " 0} [format $fmt $head $bitval $tail]]
438 r setbit mykey
$bitnum $bitval
439 assert_equal
[binary format B
* $str] [r get mykey
]
443 test
"GETBIT against non-existing key" {
445 assert_equal
0 [r getbit mykey
0]
448 test
"GETBIT against string-encoded key" {
449 # Single byte with 2nd and 3rd bit set
453 assert_equal
0 [r getbit mykey
0]
454 assert_equal
1 [r getbit mykey
1]
455 assert_equal
1 [r getbit mykey
2]
456 assert_equal
0 [r getbit mykey
3]
459 assert_equal
0 [r getbit mykey
8]
460 assert_equal
0 [r getbit mykey
100]
461 assert_equal
0 [r getbit mykey
10000]
464 test
"GETBIT against integer-encoded key" {
466 assert_encoding int mykey
468 # Ascii "1" is integer 49 = 00 11 00 01
469 assert_equal
0 [r getbit mykey
0]
470 assert_equal
0 [r getbit mykey
1]
471 assert_equal
1 [r getbit mykey
2]
472 assert_equal
1 [r getbit mykey
3]
475 assert_equal
0 [r getbit mykey
8]
476 assert_equal
0 [r getbit mykey
100]
477 assert_equal
0 [r getbit mykey
10000]
480 test
"SETRANGE against non-existing key" {
482 assert_equal
3 [r setrange mykey
0 foo
]
483 assert_equal
"foo" [r get mykey
]
486 assert_equal
0 [r setrange mykey
0 ""]
487 assert_equal
0 [r exists mykey
]
490 assert_equal
4 [r setrange mykey
1 foo
]
491 assert_equal
"\000foo" [r get mykey
]
494 test
"SETRANGE against string-encoded key" {
496 assert_equal
3 [r setrange mykey
0 b
]
497 assert_equal
"boo" [r get mykey
]
500 assert_equal
3 [r setrange mykey
0 ""]
501 assert_equal
"foo" [r get mykey
]
504 assert_equal
3 [r setrange mykey
1 b
]
505 assert_equal
"fbo" [r get mykey
]
508 assert_equal
7 [r setrange mykey
4 bar
]
509 assert_equal
"foo\000bar" [r get mykey
]
512 test
"SETRANGE against integer-encoded key" {
514 assert_encoding int mykey
515 assert_equal
4 [r setrange mykey
0 2]
516 assert_encoding raw mykey
517 assert_equal
2234 [r get mykey
]
519 # Shouldn't change encoding when nothing is set
521 assert_encoding int mykey
522 assert_equal
4 [r setrange mykey
0 ""]
523 assert_encoding int mykey
524 assert_equal
1234 [r get mykey
]
527 assert_encoding int mykey
528 assert_equal
4 [r setrange mykey
1 3]
529 assert_encoding raw mykey
530 assert_equal
1334 [r get mykey
]
533 assert_encoding int mykey
534 assert_equal
6 [r setrange mykey
5 2]
535 assert_encoding raw mykey
536 assert_equal
"1234\0002" [r get mykey
]
539 test
"SETRANGE against key with wrong type" {
542 assert_error
"*wrong kind*" {r setrange mykey
0 bar
}
545 test
"SETRANGE with out of range offset" {
547 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
550 assert_error
"*out of range*" {r setrange mykey
-1 world
}
551 assert_error
"*maximum allowed size*" {r setrange mykey
[expr 512*1024*1024-4] world
}
554 test
"GETRANGE against non-existing key" {
556 assert_equal
"" [r getrange mykey
0 -1]
559 test
"GETRANGE against string value" {
560 r
set mykey
"Hello World"
561 assert_equal
"Hell" [r getrange mykey
0 3]
562 assert_equal
"Hello World" [r getrange mykey
0 -1]
563 assert_equal
"orld" [r getrange mykey
-4 -1]
564 assert_equal
"" [r getrange mykey
5 3]
565 assert_equal
" World" [r getrange mykey
5 5000]
566 assert_equal
"Hello World" [r getrange mykey
-5000 10000]
569 test
"GETRANGE against integer-encoded value" {
571 assert_equal
"123" [r getrange mykey
0 2]
572 assert_equal
"1234" [r getrange mykey
0 -1]
573 assert_equal
"234" [r getrange mykey
-3 -1]
574 assert_equal
"" [r getrange mykey
5 3]
575 assert_equal
"4" [r getrange mykey
3 5000]
576 assert_equal
"1234" [r getrange mykey
-5000 10000]
579 test
"GETRANGE fuzzing" {
580 for {set i
0} {$i < 1000} {incr i
} {
581 r
set bin
[set bin
[randstring
0 1024 binary]]
582 set _start
[set start
[randomInt
1500]]
583 set _end
[set end
[randomInt
1500]]
584 if {$_start < 0} {set _start
"end-[abs($_start)-1]"}
585 if {$_end < 0} {set _end
"end-[abs($_end)-1]"}
586 assert_equal
[string range
$bin $_start $_end] [r getrange bin
$start $end]