]> git.saurik.com Git - redis.git/blob - tests/unit/other.tcl
Merge master with resolved conflict in src/redis-cli.c
[redis.git] / tests / unit / other.tcl
1 start_server {tags {"other"}} {
2 test {SAVE - make sure there are all the types as values} {
3 # Wait for a background saving in progress to terminate
4 waitForBgsave r
5 r lpush mysavelist hello
6 r lpush mysavelist world
7 r set myemptykey {}
8 r set mynormalkey {blablablba}
9 r zadd mytestzset 10 a
10 r zadd mytestzset 20 b
11 r zadd mytestzset 30 c
12 r save
13 } {OK}
14
15 tags {"slow"} {
16 foreach fuzztype {binary alpha compr} {
17 test "FUZZ stresser with data model $fuzztype" {
18 set err 0
19 for {set i 0} {$i < 10000} {incr i} {
20 set fuzz [randstring 0 512 $fuzztype]
21 r set foo $fuzz
22 set got [r get foo]
23 if {$got ne $fuzz} {
24 set err [list $fuzz $got]
25 break
26 }
27 }
28 set _ $err
29 } {0}
30 }
31 }
32
33 test {BGSAVE} {
34 waitForBgsave r
35 r flushdb
36 r save
37 r set x 10
38 r bgsave
39 waitForBgsave r
40 r debug reload
41 r get x
42 } {10}
43
44 test {SELECT an out of range DB} {
45 catch {r select 1000000} err
46 set _ $err
47 } {*invalid*}
48
49 tags {consistency} {
50 if {![catch {package require sha1}]} {
51 test {Check consistency of different data types after a reload} {
52 r flushdb
53 createComplexDataset r 10000
54 set dump [csvdump r]
55 set sha1 [r debug digest]
56 r debug reload
57 set sha1_after [r debug digest]
58 if {$sha1 eq $sha1_after} {
59 set _ 1
60 } else {
61 set newdump [csvdump r]
62 puts "Consistency test failed!"
63 puts "You can inspect the two dumps in /tmp/repldump*.txt"
64
65 set fd [open /tmp/repldump1.txt w]
66 puts $fd $dump
67 close $fd
68 set fd [open /tmp/repldump2.txt w]
69 puts $fd $newdump
70 close $fd
71
72 set _ 0
73 }
74 } {1}
75
76 test {Same dataset digest if saving/reloading as AOF?} {
77 r bgrewriteaof
78 waitForBgrewriteaof r
79 r debug loadaof
80 set sha1_after [r debug digest]
81 if {$sha1 eq $sha1_after} {
82 set _ 1
83 } else {
84 set newdump [csvdump r]
85 puts "Consistency test failed!"
86 puts "You can inspect the two dumps in /tmp/aofdump*.txt"
87
88 set fd [open /tmp/aofdump1.txt w]
89 puts $fd $dump
90 close $fd
91 set fd [open /tmp/aofdump2.txt w]
92 puts $fd $newdump
93 close $fd
94
95 set _ 0
96 }
97 } {1}
98 }
99 }
100
101 test {EXPIRES after a reload (snapshot + append only file)} {
102 r flushdb
103 r set x 10
104 r expire x 1000
105 r save
106 r debug reload
107 set ttl [r ttl x]
108 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
109 r bgrewriteaof
110 waitForBgrewriteaof r
111 set ttl [r ttl x]
112 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
113 list $e1 $e2
114 } {1 1}
115
116 test {PIPELINING stresser (also a regression for the old epoll bug)} {
117 set fd2 [socket $::host $::port]
118 fconfigure $fd2 -encoding binary -translation binary
119 puts -nonewline $fd2 "SELECT 9\r\n"
120 flush $fd2
121 gets $fd2
122
123 for {set i 0} {$i < 100000} {incr i} {
124 set q {}
125 set val "0000${i}0000"
126 append q "SET key:$i [string length $val]\r\n$val\r\n"
127 puts -nonewline $fd2 $q
128 set q {}
129 append q "GET key:$i\r\n"
130 puts -nonewline $fd2 $q
131 }
132 flush $fd2
133
134 for {set i 0} {$i < 100000} {incr i} {
135 gets $fd2 line
136 gets $fd2 count
137 set count [string range $count 1 end]
138 set val [read $fd2 $count]
139 read $fd2 2
140 }
141 close $fd2
142 set _ 1
143 } {1}
144
145 test {MUTLI / EXEC basics} {
146 r del mylist
147 r rpush mylist a
148 r rpush mylist b
149 r rpush mylist c
150 r multi
151 set v1 [r lrange mylist 0 -1]
152 set v2 [r ping]
153 set v3 [r exec]
154 list $v1 $v2 $v3
155 } {QUEUED QUEUED {{a b c} PONG}}
156
157 test {DISCARD} {
158 r del mylist
159 r rpush mylist a
160 r rpush mylist b
161 r rpush mylist c
162 r multi
163 set v1 [r del mylist]
164 set v2 [r discard]
165 set v3 [r lrange mylist 0 -1]
166 list $v1 $v2 $v3
167 } {QUEUED OK {a b c}}
168
169 test {Nested MULTI are not allowed} {
170 set err {}
171 r multi
172 catch {[r multi]} err
173 r exec
174 set _ $err
175 } {*ERR MULTI*}
176
177 test {WATCH inside MULTI is not allowed} {
178 set err {}
179 r multi
180 catch {[r watch x]} err
181 r exec
182 set _ $err
183 } {*ERR WATCH*}
184
185 test {APPEND basics} {
186 list [r append foo bar] [r get foo] \
187 [r append foo 100] [r get foo]
188 } {3 bar 6 bar100}
189
190 test {APPEND basics, integer encoded values} {
191 set res {}
192 r del foo
193 r append foo 1
194 r append foo 2
195 lappend res [r get foo]
196 r set foo 1
197 r append foo 2
198 lappend res [r get foo]
199 } {12 12}
200
201 test {APPEND fuzzing} {
202 set err {}
203 foreach type {binary alpha compr} {
204 set buf {}
205 r del x
206 for {set i 0} {$i < 1000} {incr i} {
207 set bin [randstring 0 10 $type]
208 append buf $bin
209 r append x $bin
210 }
211 if {$buf != [r get x]} {
212 set err "Expected '$buf' found '[r get x]'"
213 break
214 }
215 }
216 set _ $err
217 } {}
218
219 test {SUBSTR basics} {
220 set res {}
221 r set foo "Hello World"
222 lappend res [r substr foo 0 3]
223 lappend res [r substr foo 0 -1]
224 lappend res [r substr foo -4 -1]
225 lappend res [r substr foo 5 3]
226 lappend res [r substr foo 5 5000]
227 lappend res [r substr foo -5000 10000]
228 set _ $res
229 } {Hell {Hello World} orld {} { World} {Hello World}}
230
231 test {SUBSTR against integer encoded values} {
232 r set foo 123
233 r substr foo 0 -2
234 } {12}
235
236 test {SUBSTR fuzzing} {
237 set err {}
238 for {set i 0} {$i < 1000} {incr i} {
239 set bin [randstring 0 1024 binary]
240 set _start [set start [randomInt 1500]]
241 set _end [set end [randomInt 1500]]
242 if {$_start < 0} {set _start "end-[abs($_start)-1]"}
243 if {$_end < 0} {set _end "end-[abs($_end)-1]"}
244 set s1 [string range $bin $_start $_end]
245 r set bin $bin
246 set s2 [r substr bin $start $end]
247 if {$s1 != $s2} {
248 set err "String mismatch"
249 break
250 }
251 }
252 set _ $err
253 } {}
254
255 # Leave the user with a clean DB before to exit
256 test {FLUSHDB} {
257 set aux {}
258 r select 9
259 r flushdb
260 lappend aux [r dbsize]
261 r select 10
262 r flushdb
263 lappend aux [r dbsize]
264 } {0 0}
265
266 test {Perform a final SAVE to leave a clean DB on disk} {
267 r save
268 } {OK}
269 }