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