line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Protocol::Redis::Test; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
71875
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
62
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw(protocol_redis_ok); |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Test::More; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
12
|
|
|
|
|
|
|
require Carp; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub protocol_redis_ok($$) { |
15
|
1
|
|
|
1
|
1
|
87
|
my ($redis_class, $api_version) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
5
|
if ($api_version == 1) { |
18
|
1
|
|
|
|
|
4
|
_apiv1_ok($redis_class); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else { |
21
|
0
|
|
|
|
|
0
|
Carp::croak(qq/Unknown Protocol::Redis API version $api_version/); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _apiv1_ok { |
26
|
1
|
|
|
1
|
|
2
|
my $redis_class = shift; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
subtest 'Protocol::Redis APIv1 ok' => sub { |
29
|
1
|
|
|
1
|
|
1217
|
plan tests => 43; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
1
|
|
698
|
use_ok $redis_class; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
|
1
|
|
|
|
|
721
|
|
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
596
|
my $redis = new_ok $redis_class, [api => 1]; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
526
|
can_ok $redis, 'parse', 'api', 'on_message', 'encode'; |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
479
|
is $redis->api, 1, '$redis->api'; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Parsing method tests |
40
|
1
|
|
|
|
|
385
|
$redis->on_message(undef); |
41
|
1
|
|
|
|
|
6
|
_parse_string_ok($redis); |
42
|
1
|
|
|
|
|
919
|
_parse_bulk_ok($redis); |
43
|
1
|
|
|
|
|
969
|
_parse_multi_bulk_ok($redis); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# on_message works |
46
|
1
|
|
|
|
|
900
|
_on_message_ok($redis); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Encoding method tests |
49
|
1
|
|
|
|
|
5
|
_encode_ok($redis); |
50
|
|
|
|
|
|
|
} |
51
|
1
|
|
|
|
|
11
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _parse_string_ok { |
54
|
1
|
|
|
1
|
|
2
|
my $redis = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Simple test |
57
|
1
|
|
|
|
|
5
|
$redis->parse("+test\r\n"); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
5
|
is_deeply $redis->get_message, |
60
|
|
|
|
|
|
|
{type => '+', data => 'test'}, |
61
|
|
|
|
|
|
|
'simple message'; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
677
|
is_deeply $redis->get_message, undef, 'queue is empty'; |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
461
|
$redis->parse(":1\r\n"); |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
3
|
is_deeply $redis->get_message, {type => ':', data => '1'}, |
68
|
|
|
|
|
|
|
'simple number'; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Binary test |
71
|
1
|
|
|
|
|
578
|
$redis->parse(join("\r\n", '$4', pack('C4', 0, 1, 2, 3), '')); |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
4
|
is_deeply [unpack('C4', $redis->get_message->{data})], |
74
|
|
|
|
|
|
|
[0, 1, 2, 3], |
75
|
|
|
|
|
|
|
'binary data'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Chunked message |
78
|
1
|
|
|
|
|
602
|
$redis->parse('-tes'); |
79
|
1
|
|
|
|
|
4
|
$redis->parse("t2\r\n"); |
80
|
1
|
|
|
|
|
5
|
is_deeply $redis->get_message, |
81
|
|
|
|
|
|
|
{type => '-', data => 'test2'}, |
82
|
|
|
|
|
|
|
'chunked string'; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Two chunked messages together |
85
|
1
|
|
|
|
|
705
|
$redis->parse("+test"); |
86
|
1
|
|
|
|
|
4
|
$redis->parse("1\r\n-test"); |
87
|
1
|
|
|
|
|
4
|
$redis->parse("2\r\n"); |
88
|
1
|
|
|
|
|
4
|
is_deeply |
89
|
|
|
|
|
|
|
[$redis->get_message, $redis->get_message], |
90
|
|
|
|
|
|
|
[{type => '+', data => 'test1'}, {type => '-', data => 'test2'}], |
91
|
|
|
|
|
|
|
'first stick message'; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Pipelined |
94
|
1
|
|
|
|
|
934
|
$redis->parse("+OK\r\n-ERROR\r\n"); |
95
|
1
|
|
|
|
|
5
|
is_deeply |
96
|
|
|
|
|
|
|
[$redis->get_message, $redis->get_message], |
97
|
|
|
|
|
|
|
[{type => '+', data => 'OK'}, {type => '-', data => 'ERROR'}], |
98
|
|
|
|
|
|
|
'pipelined status messages'; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _parse_bulk_ok { |
102
|
1
|
|
|
1
|
|
3
|
my $redis = shift; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Bulk message |
105
|
1
|
|
|
|
|
5
|
$redis->parse("\$4\r\ntest\r\n"); |
106
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
107
|
|
|
|
|
|
|
{type => '$', data => 'test'}, |
108
|
|
|
|
|
|
|
'simple bulk message'; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
568
|
$redis->parse("\$5\r\ntes"); |
111
|
1
|
|
|
|
|
4
|
$redis->parse("t2\r\n"); |
112
|
1
|
|
|
|
|
3
|
is_deeply $redis->get_message, |
113
|
|
|
|
|
|
|
{type => '$', data => 'test2'}, |
114
|
|
|
|
|
|
|
'chunked bulk message'; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Nil bulk message |
117
|
1
|
|
|
|
|
670
|
$redis->parse("\$-1\r\n"); |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
4
|
my $message = $redis->get_message; |
120
|
1
|
|
33
|
|
|
14
|
ok defined($message) && !defined($message->{data}), |
121
|
|
|
|
|
|
|
'nil bulk message'; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Two chunked bulk messages |
124
|
1
|
|
|
|
|
370
|
$redis->parse(join("\r\n", '$4', 'test', '+OK')); |
125
|
1
|
|
|
|
|
4
|
$redis->parse("\r\n"); |
126
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
127
|
|
|
|
|
|
|
{type => '$', data => 'test'}, 'two chunked bulk messages'; |
128
|
1
|
|
|
|
|
620
|
is_deeply $redis->get_message, {type => '+', data => 'OK'}; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Pipelined bulk message |
131
|
1
|
|
|
|
|
577
|
$redis->parse(join("\r\n", ('$3', 'ok1'), ('$3', 'ok2'), '')); |
132
|
1
|
|
|
|
|
5
|
is_deeply [$redis->get_message, $redis->get_message], |
133
|
|
|
|
|
|
|
[{type => '$', data => 'ok1'}, {type => '$', data => 'ok2'}], |
134
|
|
|
|
|
|
|
'piplined bulk message'; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub _parse_multi_bulk_ok { |
138
|
1
|
|
|
1
|
|
3
|
my $redis = shift; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Multi bulk message! |
141
|
1
|
|
|
|
|
6
|
$redis->parse("*1\r\n\$4\r\ntest\r\n"); |
142
|
|
|
|
|
|
|
|
143
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
144
|
|
|
|
|
|
|
{type => '*', data => [{type => '$', data => 'test'}]}, |
145
|
|
|
|
|
|
|
'simple multibulk message'; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Multi bulk message with multiple arguments |
148
|
1
|
|
|
|
|
1013
|
$redis->parse("*3\r\n\$5\r\ntest1\r\n"); |
149
|
1
|
|
|
|
|
5
|
$redis->parse("\$5\r\ntest2\r\n"); |
150
|
1
|
|
|
|
|
4
|
$redis->parse("\$5\r\ntest3\r\n"); |
151
|
|
|
|
|
|
|
|
152
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
153
|
|
|
|
|
|
|
{ type => '*', |
154
|
|
|
|
|
|
|
data => [ |
155
|
|
|
|
|
|
|
{type => '$', data => 'test1'}, |
156
|
|
|
|
|
|
|
{type => '$', data => 'test2'}, |
157
|
|
|
|
|
|
|
{type => '$', data => 'test3'} |
158
|
|
|
|
|
|
|
] |
159
|
|
|
|
|
|
|
}, |
160
|
|
|
|
|
|
|
'multi argument multi-bulk message'; |
161
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
1280
|
$redis->parse("*0\r\n"); |
163
|
1
|
|
|
|
|
5
|
is_deeply $redis->get_message, |
164
|
|
|
|
|
|
|
{type => '*', data => []}, |
165
|
|
|
|
|
|
|
'multi-bulk empty result'; |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
711
|
$redis->parse("*-1\r\n"); |
168
|
1
|
|
|
|
|
3
|
my $message = $redis->get_message; |
169
|
1
|
|
33
|
|
|
12
|
ok defined($message) && !defined($message->{data}), |
170
|
|
|
|
|
|
|
'multi-bulk nil result'; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Does it work? |
173
|
1
|
|
|
|
|
346
|
$redis->parse("\$4\r\ntest\r\n"); |
174
|
1
|
|
|
|
|
5
|
is_deeply $redis->get_message, |
175
|
|
|
|
|
|
|
{type => '$', data => 'test'}, |
176
|
|
|
|
|
|
|
'everything still works'; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Multi bulk message with status items |
179
|
1
|
|
|
|
|
580
|
$redis->parse(join("\r\n", ('*2', '+OK', '$4', 'test'), '')); |
180
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
181
|
|
|
|
|
|
|
{ type => '*', |
182
|
|
|
|
|
|
|
data => [{type => '+', data => 'OK'}, {type => '$', data => 'test'}] |
183
|
|
|
|
|
|
|
}; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# splitted multi-bulk |
186
|
1
|
|
|
|
|
1191
|
$redis->parse(join("\r\n", ('*1', '$4', 'test'), '+OK')); |
187
|
1
|
|
|
|
|
4
|
$redis->parse("\r\n"); |
188
|
|
|
|
|
|
|
|
189
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
190
|
|
|
|
|
|
|
{type => '*', data => [{type => '$', data => 'test'}]}; |
191
|
1
|
|
|
|
|
869
|
is_deeply $redis->get_message, {type => '+', data => 'OK'}; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# Another splitted multi-bulk message |
194
|
1
|
|
|
|
|
579
|
$redis->parse("*4\r\n\$-1\r\n\$-1"); |
195
|
1
|
|
|
|
|
4
|
$redis->parse("\r\n\$5\r\ntest2\r\n"); |
196
|
1
|
|
|
|
|
4
|
$redis->parse("\$5\r\ntest3\r"); |
197
|
1
|
|
|
|
|
3
|
$redis->parse("\n"); |
198
|
1
|
|
|
|
|
3
|
is_deeply $redis->get_message, { |
199
|
|
|
|
|
|
|
type => '*', |
200
|
|
|
|
|
|
|
data => [ |
201
|
|
|
|
|
|
|
{type => '$', data => undef}, |
202
|
|
|
|
|
|
|
{type => '$', data => undef}, |
203
|
|
|
|
|
|
|
{type => '$', data => 'test2'}, |
204
|
|
|
|
|
|
|
{type => '$', data => 'test3'} |
205
|
|
|
|
|
|
|
] |
206
|
|
|
|
|
|
|
}; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# Complex string |
209
|
1
|
|
|
|
|
1365
|
$redis->parse("\*4\r\n"); |
210
|
1
|
|
|
|
|
3
|
$redis->parse("\$5\r\ntest1\r\n\$-1\r\n:42\r\n+test3\r\n\$5\r\n123"); |
211
|
1
|
|
|
|
|
4
|
$redis->parse("45\r\n"); |
212
|
1
|
|
|
|
|
3
|
is_deeply $redis->get_message, { |
213
|
|
|
|
|
|
|
type => '*', |
214
|
|
|
|
|
|
|
data => [ |
215
|
|
|
|
|
|
|
{type => '$', data => 'test1'}, |
216
|
|
|
|
|
|
|
{type => '$', data => undef}, |
217
|
|
|
|
|
|
|
{type => ':', data => 42}, |
218
|
|
|
|
|
|
|
{type => '+', data => 'test3'} |
219
|
|
|
|
|
|
|
] |
220
|
|
|
|
|
|
|
}; |
221
|
1
|
|
|
|
|
1492
|
is_deeply $redis->get_message, { |
222
|
|
|
|
|
|
|
type => '$', |
223
|
|
|
|
|
|
|
data => '12345', |
224
|
|
|
|
|
|
|
}; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# pipelined multi-bulk |
227
|
1
|
|
|
|
|
564
|
$redis->parse( |
228
|
|
|
|
|
|
|
join("\r\n", |
229
|
|
|
|
|
|
|
('*2', '$3', 'ok1', '$3', 'ok2'), |
230
|
|
|
|
|
|
|
('*1', '$3', 'ok3'), '') |
231
|
|
|
|
|
|
|
); |
232
|
|
|
|
|
|
|
|
233
|
1
|
|
|
|
|
4
|
is_deeply $redis->get_message, |
234
|
|
|
|
|
|
|
{ type => '*', |
235
|
|
|
|
|
|
|
data => [{type => '$', data => 'ok1'}, {type => '$', data => 'ok2'}] |
236
|
|
|
|
|
|
|
}; |
237
|
1
|
|
|
|
|
1027
|
is_deeply $redis->get_message, |
238
|
|
|
|
|
|
|
{type => '*', data => [{type => '$', data => 'ok3'}]}; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub _on_message_ok { |
243
|
1
|
|
|
1
|
|
3
|
my $redis = shift; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# Parsing with cb |
246
|
1
|
|
|
|
|
4
|
my $r = []; |
247
|
|
|
|
|
|
|
$redis->on_message( |
248
|
|
|
|
|
|
|
sub { |
249
|
4
|
|
|
4
|
|
9
|
my ($redis, $message) = @_; |
250
|
|
|
|
|
|
|
|
251
|
4
|
|
|
|
|
10
|
push @$r, $message; |
252
|
|
|
|
|
|
|
} |
253
|
1
|
|
|
|
|
9
|
); |
254
|
|
|
|
|
|
|
|
255
|
1
|
|
|
|
|
5
|
$redis->parse("+foo\r\n"); |
256
|
1
|
|
|
|
|
42
|
$redis->parse("\$3\r\nbar\r\n"); |
257
|
|
|
|
|
|
|
|
258
|
1
|
|
|
|
|
27
|
is_deeply $r, |
259
|
|
|
|
|
|
|
[{type => '+', data => 'foo'}, {type => '$', data => 'bar'}], |
260
|
|
|
|
|
|
|
'parsing with callback'; |
261
|
|
|
|
|
|
|
|
262
|
1
|
|
|
|
|
963
|
$r = []; |
263
|
1
|
|
|
|
|
5
|
$redis->parse(join("\r\n", ('+foo'), ('$3', 'bar'), '')); |
264
|
|
|
|
|
|
|
|
265
|
1
|
|
|
|
|
23
|
is_deeply $r, |
266
|
|
|
|
|
|
|
[{type => '+', data => 'foo'}, {type => '$', data => 'bar'}], |
267
|
|
|
|
|
|
|
'pipelined parsing with callback'; |
268
|
|
|
|
|
|
|
|
269
|
1
|
|
|
|
|
964
|
$redis->on_message(undef); |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub _encode_ok { |
273
|
1
|
|
|
1
|
|
3
|
my $redis = shift; |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
# Encode message |
276
|
1
|
|
|
|
|
7
|
is $redis->encode({type => '+', data => 'OK'}), "+OK\r\n", |
277
|
|
|
|
|
|
|
'encode status'; |
278
|
1
|
|
|
|
|
389
|
is $redis->encode({type => '-', data => 'ERROR'}), "-ERROR\r\n", |
279
|
|
|
|
|
|
|
'encode error'; |
280
|
1
|
|
|
|
|
435
|
is $redis->encode({type => ':', data => '5'}), ":5\r\n", 'encode integer'; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Encode bulk message |
283
|
1
|
|
|
|
|
397
|
is $redis->encode({type => '$', data => 'test'}), "\$4\r\ntest\r\n", |
284
|
|
|
|
|
|
|
'encode bulk'; |
285
|
1
|
|
|
|
|
371
|
is $redis->encode({type => '$', data => "\0\r\n"}), "\$3\r\n\0\r\n\r\n", |
286
|
|
|
|
|
|
|
'encode binary bulk'; |
287
|
1
|
|
|
|
|
363
|
is $redis->encode({type => '$', data => undef}), "\$-1\r\n", |
288
|
|
|
|
|
|
|
'encode nil bulk'; |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
# Encode multi-bulk |
291
|
1
|
|
|
|
|
361
|
is $redis->encode({type => '*', data => [{type => '$', data => 'test'}]}), |
292
|
|
|
|
|
|
|
join("\r\n", ('*1', '$4', 'test'), ''), |
293
|
|
|
|
|
|
|
'encode multi-bulk'; |
294
|
|
|
|
|
|
|
|
295
|
1
|
|
|
|
|
371
|
is $redis->encode( |
296
|
|
|
|
|
|
|
{ type => '*', |
297
|
|
|
|
|
|
|
data => [ |
298
|
|
|
|
|
|
|
{type => '$', data => 'test1'}, {type => '$', data => 'test2'} |
299
|
|
|
|
|
|
|
] |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
), |
302
|
|
|
|
|
|
|
join("\r\n", ('*2', '$5', 'test1', '$5', 'test2'), ''), |
303
|
|
|
|
|
|
|
'encode multi-bulk'; |
304
|
|
|
|
|
|
|
|
305
|
1
|
|
|
|
|
416
|
is $redis->encode({type => '*', data => []}), "\*0\r\n", |
306
|
|
|
|
|
|
|
'encode empty multi-bulk'; |
307
|
|
|
|
|
|
|
|
308
|
1
|
|
|
|
|
384
|
is $redis->encode({type => '*', data => undef}), "\*-1\r\n", |
309
|
|
|
|
|
|
|
'encode nil multi-bulk'; |
310
|
|
|
|
|
|
|
|
311
|
1
|
|
|
|
|
367
|
is $redis->encode( |
312
|
|
|
|
|
|
|
{ type => '*', |
313
|
|
|
|
|
|
|
data => [ |
314
|
|
|
|
|
|
|
{type => '$', data => 'foo'}, |
315
|
|
|
|
|
|
|
{type => '$', data => undef}, |
316
|
|
|
|
|
|
|
{type => '$', data => 'bar'} |
317
|
|
|
|
|
|
|
] |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
), |
320
|
|
|
|
|
|
|
join("\r\n", ('*3', '$3', 'foo', '$-1', '$3', 'bar'), ''), |
321
|
|
|
|
|
|
|
'encode multi-bulk with nil element'; |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
1; |
325
|
|
|
|
|
|
|
__END__ |