File Coverage

blib/lib/Protocol/Redis/Test.pm
Criterion Covered Total %
statement 105 106 99.0
branch 1 2 50.0
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 120 122 98.3


line stmt bran cond sub pod time code
1             package Protocol::Redis::Test;
2              
3 1     1   13466 use strict;
  1         2  
  1         23  
4 1     1   5 use warnings;
  1         2  
  1         50  
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         5  
  1         5  
12             require Carp;
13              
14             sub protocol_redis_ok($$) {
15 1     1 1 10 my ($redis_class, $api_version) = @_;
16              
17 1 50       4 if ($api_version == 1) {
18 1         3 _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   728 plan tests => 39;
30              
31 1     1   531 use_ok $redis_class;
  1         3  
  1         2  
  1         15  
  1         107  
32              
33 1         377 my $redis = new_ok $redis_class, [api => 1];
34              
35 1         689 can_ok $redis, 'parse', 'api', 'on_message', 'encode';
36              
37 1         445 is $redis->api, 1, '$redis->api';
38              
39             # Parsing method tests
40 1         305 $redis->on_message(undef);
41 1         4 _parse_string_ok($redis);
42 1         880 _parse_bulk_ok($redis);
43 1         898 _parse_multi_bulk_ok($redis);
44              
45             # on_message works
46 1         905 _on_message_ok($redis);
47              
48             # Encoding method tests
49 1         6 _encode_ok($redis);
50             }
51 1         12 }
52              
53             sub _parse_string_ok {
54 1     1   3 my $redis = shift;
55              
56             # Simple test
57 1         4 $redis->parse("+test\r\n");
58              
59 1         5 is_deeply $redis->get_message,
60             {type => '+', data => 'test'},
61             'simple message';
62              
63 1         600 is_deeply $redis->get_message, undef, 'queue is empty';
64              
65 1         372 $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         562 $redis->parse(join("\r\n", '$4', pack('C4', 0, 1, 2, 3), ''));
72              
73 1         5 is_deeply [unpack('C4', $redis->get_message->{data})],
74             [0, 1, 2, 3],
75             'binary data';
76              
77             # Chunked message
78 1         573 $redis->parse('-tes');
79 1         5 $redis->parse("t2\r\n");
80 1         4 is_deeply $redis->get_message,
81             {type => '-', data => 'test2'},
82             'chunked string';
83              
84             # Two chunked messages together
85 1         553 $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         927 $redis->parse("+OK\r\n-ERROR\r\n");
95 1         4 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         4 $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         573 $redis->parse("\$5\r\ntes");
111 1         4 $redis->parse("t2\r\n");
112 1         8 is_deeply $redis->get_message,
113             {type => '$', data => 'test2'},
114             'chunked bulk message';
115              
116             # Nil bulk message
117 1         580 $redis->parse("\$-1\r\n");
118              
119 1         5 is_deeply $redis->get_message,
120             {type => '$', data => undef},
121             'nil bulk message';
122              
123             # Two chunked bulk messages
124 1         546 $redis->parse(join("\r\n", '$4', 'test', '+OK'));
125 1         4 $redis->parse("\r\n");
126 1         3 is_deeply $redis->get_message,
127             {type => '$', data => 'test'}, 'two chunked bulk messages';
128 1         586 is_deeply $redis->get_message, {type => '+', data => 'OK'};
129              
130             # Pipelined bulk message
131 1         536 $redis->parse(join("\r\n", ('$3', 'ok1'), ('$3', 'ok2'), ''));
132 1         4 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   2 my $redis = shift;
139              
140             # Multi bulk message!
141 1         5 $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         902 $redis->parse("*3\r\n\$5\r\ntest1\r\n");
149 1         4 $redis->parse("\$5\r\ntest2\r\n");
150 1         3 $redis->parse("\$5\r\ntest3\r\n");
151              
152 1         3 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         1275 $redis->parse("*0\r\n");
163 1         5 is_deeply $redis->get_message,
164             {type => '*', data => []},
165             'multi-bulk empty result';
166              
167 1         701 $redis->parse("*-1\r\n");
168 1         3 is_deeply $redis->get_message,
169             {type => '*', data => undef},
170             'multi-bulk nil result';
171              
172             # Does it work?
173 1         580 $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         547 $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         1082 $redis->parse(join("\r\n", ('*1', '$4', 'test'), '+OK'));
187 1         4 $redis->parse("\r\n");
188              
189 1         3 is_deeply $redis->get_message,
190             {type => '*', data => [{type => '$', data => 'test'}]};
191 1         904 is_deeply $redis->get_message, {type => '+', data => 'OK'};
192              
193             # pipelined multi-bulk
194 1         543 $redis->parse(
195             join("\r\n",
196             ('*2', '$3', 'ok1', '$3', 'ok2'),
197             ('*1', '$3', 'ok3'), '')
198             );
199              
200 1         4 is_deeply $redis->get_message,
201             { type => '*',
202             data => [{type => '$', data => 'ok1'}, {type => '$', data => 'ok2'}]
203             };
204 1         1093 is_deeply $redis->get_message,
205             {type => '*', data => [{type => '$', data => 'ok3'}]};
206              
207             }
208              
209             sub _on_message_ok {
210 1     1   2 my $redis = shift;
211              
212             # Parsing with cb
213 1         2 my $r = [];
214             $redis->on_message(
215             sub {
216 4     4   7 my ($redis, $message) = @_;
217              
218 4         9 push @$r, $message;
219             }
220 1         27 );
221              
222 1         5 $redis->parse("+foo\r\n");
223 1         4 $redis->parse("\$3\r\nbar\r\n");
224              
225 1         7 is_deeply $r,
226             [{type => '+', data => 'foo'}, {type => '$', data => 'bar'}],
227             'parsing with callback';
228              
229 1         899 $r = [];
230 1         7 $redis->parse(join("\r\n", ('+foo'), ('$3', 'bar'), ''));
231              
232 1         7 is_deeply $r,
233             [{type => '+', data => 'foo'}, {type => '$', data => 'bar'}],
234             'pipelined parsing with callback';
235              
236 1         907 $redis->on_message(undef);
237             }
238              
239             sub _encode_ok {
240 1     1   3 my $redis = shift;
241              
242             # Encode message
243 1         6 is $redis->encode({type => '+', data => 'OK'}), "+OK\r\n",
244             'encode status';
245 1         306 is $redis->encode({type => '-', data => 'ERROR'}), "-ERROR\r\n",
246             'encode error';
247 1         306 is $redis->encode({type => ':', data => '5'}), ":5\r\n", 'encode integer';
248              
249             # Encode bulk message
250 1         303 is $redis->encode({type => '$', data => 'test'}), "\$4\r\ntest\r\n",
251             'encode bulk';
252 1         300 is $redis->encode({type => '$', data => undef}), "\$-1\r\n",
253             'encode nil bulk';
254              
255             # Encode multi-bulk
256 1         301 is $redis->encode({type => '*', data => [{type => '$', data => 'test'}]}),
257             join("\r\n", ('*1', '$4', 'test'), ''),
258             'encode multi-bulk';
259              
260 1         303 is $redis->encode(
261             { type => '*',
262             data => [
263             {type => '$', data => 'test1'}, {type => '$', data => 'test2'}
264             ]
265             }
266             ),
267             join("\r\n", ('*2', '$5', 'test1', '$5', 'test2'), ''),
268             'encode multi-bulk';
269              
270 1         299 is $redis->encode({type => '*', data => []}), "\*0\r\n",
271             'encode empty multi-bulk';
272              
273 1         312 is $redis->encode({type => '*', data => undef}), "\*-1\r\n",
274             'encode nil multi-bulk';
275              
276 1         302 is $redis->encode(
277             { type => '*',
278             data => [
279             {type => '$', data => 'foo'},
280             {type => '$', data => undef},
281             {type => '$', data => 'bar'}
282             ]
283             }
284             ),
285             join("\r\n", ('*3', '$3', 'foo', '$-1', '$3', 'bar'), ''),
286             'encode multi-bulk with nil element';
287             }
288              
289             1;
290             __END__