]> git.saurik.com Git - redis.git/blob - tests/unit/basic.tcl
Merge remote branch 'pietern/zfixes'
[redis.git] / tests / unit / basic.tcl
1 start_server {tags {"basic"}} {
2 test {DEL all keys to start with a clean DB} {
3 foreach key [r keys *] {r del $key}
4 r dbsize
5 } {0}
6
7 test {SET and GET an item} {
8 r set x foobar
9 r get x
10 } {foobar}
11
12 test {SET and GET an empty item} {
13 r set x {}
14 r get x
15 } {}
16
17 test {DEL against a single item} {
18 r del x
19 r get x
20 } {}
21
22 test {Vararg DEL} {
23 r set foo1 a
24 r set foo2 b
25 r set foo3 c
26 list [r del foo1 foo2 foo3 foo4] [r mget foo1 foo2 foo3]
27 } {3 {{} {} {}}}
28
29 test {KEYS with pattern} {
30 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
31 r set $key hello
32 }
33 lsort [r keys foo*]
34 } {foo_a foo_b foo_c}
35
36 test {KEYS to get all keys} {
37 lsort [r keys *]
38 } {foo_a foo_b foo_c key_x key_y key_z}
39
40 test {DBSIZE} {
41 r dbsize
42 } {6}
43
44 test {DEL all keys} {
45 foreach key [r keys *] {r del $key}
46 r dbsize
47 } {0}
48
49 test {Very big payload in GET/SET} {
50 set buf [string repeat "abcd" 1000000]
51 r set foo $buf
52 r get foo
53 } [string repeat "abcd" 1000000]
54
55 tags {"slow"} {
56 test {Very big payload random access} {
57 set err {}
58 array set payload {}
59 for {set j 0} {$j < 100} {incr j} {
60 set size [expr 1+[randomInt 100000]]
61 set buf [string repeat "pl-$j" $size]
62 set payload($j) $buf
63 r set bigpayload_$j $buf
64 }
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'"
70 break
71 }
72 }
73 unset payload
74 set _ $err
75 } {}
76
77 test {SET 10000 numeric keys and access all them in reverse order} {
78 set err {}
79 for {set x 0} {$x < 10000} {incr x} {
80 r set $x $x
81 }
82 set sum 0
83 for {set x 9999} {$x >= 0} {incr x -1} {
84 set val [r get $x]
85 if {$val ne $x} {
86 set err "Eleemnt at position $x is $val instead of $x"
87 break
88 }
89 }
90 set _ $err
91 } {}
92
93 test {DBSIZE should be 10101 now} {
94 r dbsize
95 } {10101}
96 }
97
98 test {INCR against non existing key} {
99 set res {}
100 append res [r incr novar]
101 append res [r get novar]
102 } {11}
103
104 test {INCR against key created by incr itself} {
105 r incr novar
106 } {2}
107
108 test {INCR against key originally set with SET} {
109 r set novar 100
110 r incr novar
111 } {101}
112
113 test {INCR over 32bit value} {
114 r set novar 17179869184
115 r incr novar
116 } {17179869185}
117
118 test {INCRBY over 32bit value with over 32bit increment} {
119 r set novar 17179869184
120 r incrby novar 17179869184
121 } {34359738368}
122
123 test {INCR fails against key with spaces (no integer encoded)} {
124 r set novar " 11 "
125 catch {r incr novar} err
126 format $err
127 } {ERR*}
128
129 test {INCR fails against a key holding a list} {
130 r rpush mylist 1
131 catch {r incr mylist} err
132 r rpop mylist
133 format $err
134 } {ERR*}
135
136 test {DECRBY over 32bit value with over 32bit increment, negative res} {
137 r set novar 17179869184
138 r decrby novar 17179869185
139 } {-1}
140
141 test {SETNX target key missing} {
142 r setnx novar2 foobared
143 r get novar2
144 } {foobared}
145
146 test {SETNX target key exists} {
147 r setnx novar2 blabla
148 r get novar2
149 } {foobared}
150
151 test {SETNX will overwrite EXPIREing key} {
152 r set x 10
153 r expire x 10000
154 r setnx x 20
155 r get x
156 } {20}
157
158 test {EXISTS} {
159 set res {}
160 r set newkey test
161 append res [r exists newkey]
162 r del newkey
163 append res [r exists newkey]
164 } {10}
165
166 test {Zero length value in key. SET/GET/EXISTS} {
167 r set emptykey {}
168 set res [r get emptykey]
169 append res [r exists emptykey]
170 r del emptykey
171 append res [r exists emptykey]
172 } {10}
173
174 test {Commands pipelining} {
175 set fd [r channel]
176 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
177 flush $fd
178 set res {}
179 append res [string match OK* [::redis::redis_read_reply $fd]]
180 append res [::redis::redis_read_reply $fd]
181 append res [string match PONG* [::redis::redis_read_reply $fd]]
182 format $res
183 } {1xyzk1}
184
185 test {Non existing command} {
186 catch {r foobaredcommand} err
187 string match ERR* $err
188 } {1}
189
190 test {RENAME basic usage} {
191 r set mykey hello
192 r rename mykey mykey1
193 r rename mykey1 mykey2
194 r get mykey2
195 } {hello}
196
197 test {RENAME source key should no longer exist} {
198 r exists mykey
199 } {0}
200
201 test {RENAME against already existing key} {
202 r set mykey a
203 r set mykey2 b
204 r rename mykey2 mykey
205 set res [r get mykey]
206 append res [r exists mykey2]
207 } {b0}
208
209 test {RENAMENX basic usage} {
210 r del mykey
211 r del mykey2
212 r set mykey foobar
213 r renamenx mykey mykey2
214 set res [r get mykey2]
215 append res [r exists mykey]
216 } {foobar0}
217
218 test {RENAMENX against already existing key} {
219 r set mykey foo
220 r set mykey2 bar
221 r renamenx mykey mykey2
222 } {0}
223
224 test {RENAMENX against already existing key (2)} {
225 set res [r get mykey]
226 append res [r get mykey2]
227 } {foobar}
228
229 test {RENAME against non existing source key} {
230 catch {r rename nokey foobar} err
231 format $err
232 } {ERR*}
233
234 test {RENAME where source and dest key is the same} {
235 catch {r rename mykey mykey} err
236 format $err
237 } {ERR*}
238
239 test {DEL all keys again (DB 0)} {
240 foreach key [r keys *] {
241 r del $key
242 }
243 r dbsize
244 } {0}
245
246 test {DEL all keys again (DB 1)} {
247 r select 10
248 foreach key [r keys *] {
249 r del $key
250 }
251 set res [r dbsize]
252 r select 9
253 format $res
254 } {0}
255
256 test {MOVE basic usage} {
257 r set mykey foobar
258 r move mykey 10
259 set res {}
260 lappend res [r exists mykey]
261 lappend res [r dbsize]
262 r select 10
263 lappend res [r get mykey]
264 lappend res [r dbsize]
265 r select 9
266 format $res
267 } [list 0 0 foobar 1]
268
269 test {MOVE against key existing in the target DB} {
270 r set mykey hello
271 r move mykey 10
272 } {0}
273
274 test {SET/GET keys in different DBs} {
275 r set a hello
276 r set b world
277 r select 10
278 r set a foo
279 r set b bared
280 r select 9
281 set res {}
282 lappend res [r get a]
283 lappend res [r get b]
284 r select 10
285 lappend res [r get a]
286 lappend res [r get b]
287 r select 9
288 format $res
289 } {hello world foo bared}
290
291 test {MGET} {
292 r flushdb
293 r set foo BAR
294 r set bar FOO
295 r mget foo bar
296 } {BAR FOO}
297
298 test {MGET against non existing key} {
299 r mget foo baazz bar
300 } {BAR {} FOO}
301
302 test {MGET against non-string key} {
303 r sadd myset ciao
304 r sadd myset bau
305 r mget foo baazz bar myset
306 } {BAR {} FOO {}}
307
308 test {RANDOMKEY} {
309 r flushdb
310 r set foo x
311 r set bar y
312 set foo_seen 0
313 set bar_seen 0
314 for {set i 0} {$i < 100} {incr i} {
315 set rkey [r randomkey]
316 if {$rkey eq {foo}} {
317 set foo_seen 1
318 }
319 if {$rkey eq {bar}} {
320 set bar_seen 1
321 }
322 }
323 list $foo_seen $bar_seen
324 } {1 1}
325
326 test {RANDOMKEY against empty DB} {
327 r flushdb
328 r randomkey
329 } {}
330
331 test {RANDOMKEY regression 1} {
332 r flushdb
333 r set x 10
334 r del x
335 r randomkey
336 } {}
337
338 test {GETSET (set new value)} {
339 list [r getset foo xyz] [r get foo]
340 } {{} xyz}
341
342 test {GETSET (replace old value)} {
343 r set foo bar
344 list [r getset foo xyz] [r get foo]
345 } {bar xyz}
346
347 test {MSET base case} {
348 r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n"
349 r mget x y z
350 } [list 10 {foo bar} "x x x x x x x\n\n\r\n"]
351
352 test {MSET wrong number of args} {
353 catch {r mset x 10 y "foo bar" z} err
354 format $err
355 } {*wrong number*}
356
357 test {MSETNX with already existent key} {
358 list [r msetnx x1 xxx y2 yyy x 20] [r exists x1] [r exists y2]
359 } {0 0 0}
360
361 test {MSETNX with not existing keys} {
362 list [r msetnx x1 xxx y2 yyy] [r get x1] [r get y2]
363 } {1 xxx yyy}
364
365 test {MSETNX should remove all the volatile keys even on failure} {
366 r mset x 1 y 2 z 3
367 r expire y 10000
368 r expire z 10000
369 list [r msetnx x A y B z C] [r mget x y z]
370 } {0 {1 {} {}}}
371 }