-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredis.test
342 lines (310 loc) · 10.9 KB
/
redis.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env tclsh
package require tcltest
source $::env(REDISTCL)
namespace eval redis::test {
proc CLEANUP {} {
set ::redis::id 0
array set ::redis::fd {}
array set ::redis::blocking {}
array set ::redis::deferred {}
array set ::redis::subscribed {}
array set ::redis::callback {}
array set ::redis::state {} ;# State in non-blocking reply reading
array set ::redis::statestack {} ;# Stack of states, for nested mbulks
catch {unset ::redis::test::id}
catch {unset ::redis::test::val}
catch {unset ::redis::test::buf}
catch {close $::redis::test::fd}
}
namespace import ::tcltest::test
proc ::socket { server port } { open /tmp/redistestfile.sock w+ }
test clientInit {Test client retuns ::redis::redisHandle} -body {
set r [redis]
} -cleanup CLEANUP -match regexp -result {::redis::redisHandle\d+}
test __method__blocking-variable {Check that blocking sets blocking variable} -setup {
set id 1
set ::redis::blocking($id) 0
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set val 1
} -body {
::redis::__method__blocking $id $fd $val
return $::redis::blocking($id)
} -cleanup CLEANUP -result 1
test __method__blocking-fconfigure {Check that blocking sets blocking on channel} -setup {
set id 1
set ::redis::blocking($id) 0
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set val 1
} -body {
::redis::__method__blocking $id $fd $val
return [fconfigure $fd -blocking]
} -cleanup CLEANUP -result 1
test __method__blocking-fconfigure {Check that blocking sets blocking on channel} -setup {
set id 1
set ::redis::blocking($id) 0
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set val 1
} -body {
::redis::__method__blocking $id $fd $val
return [fconfigure $fd -blocking]
} -cleanup CLEANUP -result 1
test redis_write {Check that redis_write outputs properly} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
} -body {
::redis::redis_write $fd $buf
seek $fd 0
set rbuf [read $fd]
if { $rbuf eq $buf } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_writenl {Check that redis_writenl outputs properly} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
} -body {
::redis::redis_writenl $fd $buf
seek $fd 0
set rbuf [read $fd]
if { $rbuf eq "$buf\r\n" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_readnl {Check that redis_readnl outputs properly} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
puts -nonewline $fd $buf
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_readnl $fd [string length $buf] ]
if { $rbuf eq $buf } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_read_line {Check that redis_read_line outputs properly} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
puts -nonewline $fd $buf
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_read_line $fd ]
if { $rbuf eq "$buf" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_bulk_read {Check that redis_bulk_read outputs properly} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
puts -nonewline $fd "[string length $buf]"
puts -nonewline $fd "\r\n"
puts -nonewline $fd $buf
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_bulk_read $fd ]
if { $rbuf eq "$buf" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_read_reply-integer {Check that redis_read_reply returns properly for integer} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf 1
puts -nonewline $fd ":$buf"
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_read_reply $fd ]
if { $rbuf eq "$buf" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_read_reply-status {Check that redis_read_reply returns properly for status message} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf OK
puts -nonewline $fd "+$buf"
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_read_reply $fd ]
if { $rbuf eq "$buf" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_read_reply-error {Check that redis_read_reply returns properly for status error} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf ERR
puts -nonewline $fd "-$buf"
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_read_reply $fd ]
} -cleanup CLEANUP -result ERR -returnCodes error
test redis_read_reply-bulk {Check that redis_read_reply returns properly for bulk read} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set buf {lwkjeflwkj slk 'slsj\n slkdj}
puts -nonewline $fd "$[string length $buf]"
puts -nonewline $fd "\r\n"
puts -nonewline $fd $buf
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
set rbuf [::redis::redis_read_reply $fd ]
if { $rbuf eq "$buf" } {
return 1
}
return 0
} -cleanup CLEANUP -result 1
test redis_read_reply-mbulk {Check that redis_read_reply returns properly for mbulk read} -setup {
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
#puts -nonewline $fd [::redis::redis_format_message abcd xyzf 0]
puts -nonewline $fd [::redis::redis_format_message xxxxxxx yyyyyyyyyyyy z]
seek $fd 0
} -body {
::redis::redis_read_reply $fd
} -cleanup CLEANUP -result "xxxxxxx yyyyyyyyyyyy z"
test redis_call_callback {Check that redis_call_callback calls callback properly} -setup {
set id 1
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
} -body {
::redis::redis_call_callback $id test reply
} -cleanup { CLEANUP; rename ::CB {} } -output "::redis::redisHandle1 test reply"
test redis_readable-eof {Check that redis_readable returns properly for eof read} -setup {
set fd [open /tmp/redistestfile.sock w+]
set id 1
fconfigure $fd -translation binary -blocking 0
gets $fd
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
} -body {
::redis::redis_readable $fd $id
} -cleanup CLEANUP -output {::redis::redisHandle1 eof }
test redis_readable-status {Check that redis_readable returns properly for status line} -setup {
set fd [open /tmp/redistestfile.sock w+]
set id 1
::redis::redis_reset_state $id
fconfigure $fd -translation binary -blocking 0
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
puts -nonewline $fd "+OK"
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
::redis::redis_readable $fd $id
} -cleanup CLEANUP -output {::redis::redisHandle1 reply OK}
test redis_readable-bulk {Check that redis_readable returns properly for bulk line} -setup {
set fd [open /tmp/redistestfile.sock w+]
set id 1
::redis::redis_reset_state $id
fconfigure $fd -translation binary -blocking 0
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
puts -nonewline $fd {$8}
puts -nonewline $fd "\r\n"
puts -nonewline $fd "abcdefgh"
puts -nonewline $fd "\r\n"
seek $fd 0
} -body {
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
} -cleanup CLEANUP -output {::redis::redisHandle1 reply abcdefgh}
test redis_readable-mbulk {Check that redis_readable returns properly for mbulk line} -setup {
set fd [open /tmp/redistestfile.sock w+]
set id 1
::redis::redis_reset_state $id
fconfigure $fd -translation binary -blocking 0
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
puts -nonewline $fd [::redis::redis_format_message abcdefgh]
seek $fd 0
} -body {
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
} -cleanup CLEANUP -output {::redis::redisHandle1 reply abcdefgh}
test redis_readable-mbulk3 {Check that redis_readable returns properly for 3mbulk line} -setup {
set fd [open /tmp/redistestfile.sock w+]
set id 1
::redis::redis_reset_state $id
fconfigure $fd -translation binary -blocking 0
proc ::CB { id type reply } {
puts -nonewline "$id $type $reply"
}
set ::redis::callback($id) [list CB]
set ::redis::subscribed($id) 0
puts -nonewline $fd [::redis::redis_format_message abcd xyzf 0]
seek $fd 0
} -body {
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
::redis::redis_readable $fd $id
} -cleanup CLEANUP -output {::redis::redisHandle1 reply abcd xyzf 0}
test __method__subscribe-nocallback {Ensure subscribe fails if no callback} -setup {
set id 1
set ::redis::callback($id) {}
} -body {
::redis::__method__subscribe $id y x
} -cleanup CLEANUP -returnCodes error -result "A callback must be set prior to subscribing"
test __method__subscribe-setssubscribedflag {Ensure subscribe sets subscribed flag} -setup {
set id 1
set ::redis::callback($id) call
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
} -body {
::redis::__method__subscribe $id $fd x
return $::redis::subscribed($id)
} -cleanup CLEANUP -result 1
test __method__unsubscribe-singleSub {Ensure unsubscribe handles a single unsubscription} -setup {
set id 1
set fd [open /tmp/redistestfile.sock w+]
fconfigure $fd -translation binary
set ::redis::callback($id) [list CB]
puts -nonewline $fd [::redis::redis_format_message abcd xyzf 0]
seek $fd 0
proc ::redis::redis_write {fd args} {}
} -body {
::redis::__method__unsubscribe $id $fd x
} -result "abcd xyzf 0"
}
::tcltest::cleanupTests