blib/lib/AnyEvent/RabbitMQ/Simple.pm

filter: Statementstmt 14.4% 32 / 222 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 9.4% 5 / 53 Podpod 100% 2 / 2 totaltotal 10.6% 39 / 367 CC 41 SCAR 33.2
CC
41
Cov
11%
_declare_exchanges
72.0
_declare_queues
72.0
_bind_exchanges
72.0
CRAP 27.6
↑ Summary
1 # ABSTRACT: Easy to use asynchronous AMQP client
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package AnyEvent::RabbitMQ::Simple;
5 1 our $AUTHORITY = 'cpan:AJGB';
6 1 $AnyEvent::RabbitMQ::Simple::VERSION = '0.02';
7 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use AnyEvent::RabbitMQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has 'host' => (
12 is => 'ro',
13 default => '127.0.0.1',
14 );
15  
16 1 has 'port' => (
17 is => 'ro',
18 default => 5672,
19 );
20  
21 1 has 'vhost' => (
22 is => 'ro',
23 default => '/',
24 );
25  
26 1 has 'user' => (
27 is => 'ro',
28 default => 'guest'
29 );
30  
31 1 has 'pass' => (
32 is => 'ro',
33 default => '',
34 );
35  
36 1 has 'failure_cb' => (
37 is => 'ro',
38 required => 1,
39 );
40  
41 1 has [qw( tls tune )] => (
42 is => 'ro',
43 );
44  
45 has $_ => (
46 is => 'ro',
47 predicate => "_has_$_",
48 1 ) for qw(exchange exchanges queue queues bind_exchanges bind_queues);
49  
50 1 has 'timeout' => (
51 is => 'ro',
52 default => 0,
53 );
54  
55 1 has 'prefetch_count' => (
56 is => 'ro',
57 default => 0,
58 );
59  
60 1 has 'confirm_publish' => (
61 is => 'ro',
62 default => 0,
63 );
64  
65 1 has 'gen_queue' => (
66 is => 'rw',
67 );
68  
69 1 has 'verbose' => (
70 is => 'ro',
71 default => 0,
72 );
73  
74 has '_guard' => (
75 is => 'rw',
76 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 1 );
78  
79 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=1 SCAR=6.9
80 0 my $self = shift;
81  
82 0 my $guard = $self->_guard;
83  
84 # cancel pending actions
85 0 delete $guard->{flows};
86  
87 # exec failure callback
88 0 $self->failure_cb->( @_ );
89  
90 # send undef
91 0 $guard->{flow}->send();
92 }
93  
94 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
95 0 my ($self) = @_;
96 0 my $guard = $self->_guard;
97 0 my $cv = $guard->{cv} = AE::cv;
98 0 my $flow = $guard->{flow} = AE::cv;
99 $flow->begin(
100 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 $cv->send($guard->{channel});
102 }
103 0 );
104 $guard->{conn} = AnyEvent::RabbitMQ->new(verbose=>$self->verbose)->load_xml_spec()->connect(
105 host => $self->host,
106 port => $self->port,
107 user => $self->user,
108 pass => $self->pass,
109 vhost => $self->vhost,
110 timeout => $self->timeout,
111 tls => $self->tls,
112 tune => $self->tune,
113 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 my $conn = shift;
115 0 $self->_open_channel($conn);
116 0 $flow->end;
117 },
118 0 ▶ on_failure => sub { $self->_handle_error( 'ConnectOnFailure', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ on_read_failure => sub { $self->_handle_error( 'ConnectOnReadFailure', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 ▶ on_return => sub { $self->_handle_error( 'ConnectOnReturn', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 ▶ on_close => sub { $self->_handle_error( 'ConnectOnClose', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 );
123  
124 0 return $cv;
125 }
126  
127 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self) = @_;
129  
130 0 delete $self->_guard->{conn};
131 }
132  
133 0 ▶sub _open_channel {
Subroutine _open_channel: not called CC=1 SCAR=6.9
134 0 my ($self, $conn) = @_;
135 0 $self->_guard->{flow}->begin;
136 $conn->open_channel(
137 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 my $channel = shift;
139 0 $self->_guard->{channel} = $channel;
140  
141 0 my $cv_dec_ex = $self->_guard->{flows}->{cv_dec_ex} = AE::cv;
142 0 my $cv_dec_q = $self->_guard->{flows}->{cv_dec_q} = AE::cv;
143 0 my $cv_bind_ex = $self->_guard->{flows}->{cv_bind_ex} = AE::cv;
144 0 my $cv_bind_q = $self->_guard->{flows}->{cv_bind_q} = AE::cv;
145 0 my $cv_confirm_channel = $self->_guard->{flows}->{cv_confirm_channel} = AE::cv;
146 0 my $cv_qos_channel = $self->_guard->{flows}->{cv_qos_channel} = AE::cv;
147  
148 $cv_dec_ex->cb(
149 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
150 0 my $done = shift->recv;
151 0 ▶ $self->_declare_queues($cv_dec_q) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
152 }
153 0 );
154 $cv_dec_q->cb(
155 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
156 0 my $done = shift->recv;
157 0 ▶ $self->_bind_exchanges($cv_bind_ex) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
158 }
159 0 );
160 $cv_bind_ex->cb(
161 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
162 0 my $done = shift->recv;
163 0 ▶ $self->_bind_queues($cv_bind_q) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
164 }
165 0 );
166 $cv_bind_q->cb(
167 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
168 0 my $done = shift->recv;
169 0 ▶ $self->_confirm_channel( $cv_confirm_channel ) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
170 }
171 0 );
172 $cv_confirm_channel->cb(
173 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 my $done = shift->recv;
175 0 ▶ $self->_qos_channel( $cv_qos_channel ) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
176 }
177 0 );
178 $cv_qos_channel->cb(
179 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 my $done = shift->recv;
181 0 $self->_guard->{flow}->end;
182 }
183 0 );
184  
185 0 $self->_declare_exchanges($cv_dec_ex);
186  
187 },
188 0 ▶ on_failure => sub { $self->_handle_error( 'OpenChannelOnFailure', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 ▶ on_return => sub { $self->_handle_error( 'OpenChannelOnReturn', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 ▶ on_close => sub { $self->_handle_error( 'OpenChannelOnClose', '', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 );
192 }
193  
194 0 ▶sub _confirm_channel {
Subroutine _confirm_channel: not called CC=2 SCAR=17.9
195 0 my ($self, $cv) = @_;
196 0 ▶ if ( $self->confirm_publish ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->confirm_publish) { } 0 0 0
197 0 $self->_guard->{flow}->begin;
198 $self->_guard->{channel}->confirm(
199 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 $self->_guard->{flow}->end;
201 0 $cv->send(1);
202 },
203 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 $self->_handle_error( 'ConfirmChannelOnFailure', '', @_ );
205 0 $cv->send;
206 },
207 0 );
208 } else {
209 0 $cv->send(1);
210 }
211 }
212  
213 0 ▶sub _qos_channel {
Subroutine _qos_channel: not called CC=2 SCAR=17.9
214 0 my ($self, $cv) = @_;
215 0 ▶ if ( $self->prefetch_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prefetch_count) { } 0 0 0
216 0 $self->_guard->{flow}->begin;
217 $self->_guard->{channel}->qos(
218 prefetch_count => $self->prefetch_count,
219 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 $self->_guard->{flow}->end;
221 0 $cv->send(1);
222 },
223 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
224 0 $self->_handle_error( 'QosChannelOnFailure', '', @_ );
225 0 $cv->send;
226 },
227 0 );
228 } else {
229 0 $cv->send(1);
230 }
231 }
232  
233 0 ▶sub _declare_exchanges {
Subroutine _declare_exchanges: not called CC=8 SCAR=42.8
234 0 my ($self, $cv) = @_;
235  
236 0 ▶ $cv->begin( sub { shift->send(1) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
237  
238 0 ▶ if ( $self->_has_exchange ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_exchange) 0 0 0
239 0 $self->_declare_exchange($cv, $self->exchange);
240 }
241 0 ▶ if ( $self->_has_exchanges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_exchanges) 0 0 0
242 0 ▶ my @exchanges = @{ $self->exchanges || [] };
Condition0%0/2
$self->exchanges || []
l!l
00
MC/DC0%0/1
$self->exchanges || []
$self->exchanges
Truth table0%0/2
A
A= $self->exchanges
Aresult
11
00
243 0 ▶ for ( my $i = 0; $i < scalar @exchanges; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @exchanges) 0 0 0
244 0 my $name = $exchanges[$i];
245 0 my $opts = $exchanges[$i+1];
246  
247 # another name
248 0 ▶ if ( defined $opts && ref $opts ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts and ref $opts ne 'HASH') { } 0 0 0
Condition0%0/3
defined $opts and ref $opts ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts and ref $opts ne "HASH"
defined $opts ref $opts ne "HASH"
Truth table0%0/3
A and B
A= defined $optsB= ref $opts ne "HASH"
ABresult
0X0
100
111
249 0 $self->_declare_exchange($cv, $name);
250 0 $self->_declare_exchange($cv, $opts);
251 } else {
252 0 ▶ $self->_declare_exchange($cv, $name, %{ $opts || {} });
Condition0%0/2
$opts || {}
l!l
00
MC/DC0%0/1
$opts || {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
253 }
254 }
255 }
256 0 $cv->end;
257 }
258  
259 0 ▶sub _declare_exchange {
Subroutine _declare_exchange: not called CC=1 SCAR=6.9
260 0 my ($self, $cv, $name, %options) = @_;
261  
262 0 $self->_guard->{flow}->begin;
263 0 $cv->begin;
264 $self->_guard->{channel}->declare_exchange(
265 %options,
266 exchange => $name,
267 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 $self->_guard->{flow}->end;
269 0 $cv->end;
270 },
271 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 0 $self->_handle_error( 'DeclareExchangeOnFailure', "exchange:$name", @_ );
273 0 $cv->end;
274 }
275 0 );
276 }
277  
278 0 ▶sub _declare_queues {
Subroutine _declare_queues: not called CC=8 SCAR=42.8
279 0 my ($self, $cv) = @_;
280  
281 0 ▶ $cv->begin( sub { shift->send(1) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
282  
283 0 ▶ if ( $self->_has_queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_queue) 0 0 0
284 0 $self->_declare_queue($cv, $self->queue);
285 }
286 0 ▶ if ( $self->_has_queues ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_queues) { } 0 0 0
287 0 ▶ my @queues = @{ $self->queues || [] };
Condition0%0/2
$self->queues || []
l!l
00
MC/DC0%0/1
$self->queues || []
$self->queues
Truth table0%0/2
A
A= $self->queues
Aresult
11
00
288 0 ▶ for ( my $i = 0; $i < scalar @queues; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @queues) 0 0 0
289 0 my $name = $queues[$i];
290 0 my $opts = $queues[$i+1];
291  
292 # another name
293 0 ▶ if ( defined $opts && ref $opts ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts and ref $opts ne 'HASH') { } 0 0 0
Condition0%0/3
defined $opts and ref $opts ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts and ref $opts ne "HASH"
defined $opts ref $opts ne "HASH"
Truth table0%0/3
A and B
A= defined $optsB= ref $opts ne "HASH"
ABresult
0X0
100
111
294 0 $self->_declare_queue($cv, $name);
295 0 $self->_declare_queue($cv, $opts);
296 } else {
297 0 ▶ $self->_declare_queue($cv, $name, %{ $opts || {} });
Condition0%0/2
$opts || {}
l!l
00
MC/DC0%0/1
$opts || {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
298 }
299 }
300 } else {
301 0 $self->_declare_queue($cv, '');
302 }
303 0 $cv->end;
304 }
305  
306 0 ▶sub _declare_queue {
Subroutine _declare_queue: not called CC=2 SCAR=17.9
307 0 my ($self, $cv, $name, %options) = @_;
308  
309 0 $self->_guard->{flow}->begin;
310 0 $cv->begin;
311 $self->_guard->{channel}->declare_queue(
312 %options,
313 queue => $name || '',
314 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
315 0 my $method = shift;
316 0 ▶ if ( ! $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
317 0 $self->gen_queue( $method->method_frame->queue );
318 }
319 0 $self->_guard->{flow}->end;
320 0 $cv->end;
321 },
322 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 $self->_handle_error( 'DeclareQueueOnFailure', "queue:$name", @_ );
324 0 $cv->send;
325 },
326 0 ▶ );
Condition0%0/2
$name || ''
l!l
00
MC/DC0%0/1
$name || ''
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
327 }
328  
329 0 ▶sub _make_pair {
Subroutine _make_pair: not called CC=4 SCAR=30.0
330 0 my ($pairs) = @_;
331  
332 0 my @list;
333 0 ▶ while (my ($l,$r) = each %{ $pairs || {} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$pairs or {};}) 0 0 0
Condition0%0/2
$pairs || {}
l!l
00
MC/DC0%0/1
$pairs || {}
$pairs
Truth table0%0/2
A
A= $pairs
Aresult
11
00
334 0 ▶ push @list, $l, ref $r eq 'ARRAY' ? $r : [ $r, undef ];
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 0 0
335 }
336  
337 0 return @list;
338 }
339  
340 0 ▶sub _bind_exchanges {
Subroutine _bind_exchanges: not called CC=8 SCAR=42.8
341 0 my ($self, $cv) = @_;
342  
343 0 ▶ $cv->begin( sub { shift->send(1) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
344  
345 0 ▶ if ( $self->_has_bind_exchanges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_bind_exchanges) 0 0 0
346 0 my @pairs;
347 0 my $bind_exchanges = $self->bind_exchanges;
348 0 ▶ if ( ref $bind_exchanges eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bind_exchanges eq 'ARRAY') { } 0 0 0
elsif (ref $bind_exchanges eq 'HASH') { } 0 0 0
349 0 ▶ for my $pair ( @{ $bind_exchanges || [] } ) {
Condition0%0/2
$bind_exchanges || []
l!l
00
MC/DC0%0/1
$bind_exchanges || []
$bind_exchanges
Truth table0%0/2
A
A= $bind_exchanges
Aresult
11
00
350 0 push @pairs, _make_pair($pair);
351 }
352 } elsif ( ref $bind_exchanges eq 'HASH' ) {
353 0 push @pairs, _make_pair($bind_exchanges);
354 }
355  
356 0 ▶ for ( my $i = 0; $i < scalar @pairs; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @pairs) 0 0 0
357 0 my $destination = $pairs[$i];
358 0 my ($source, $routing_key) = @{ $pairs[$i+1] };
359 0 my %opts;
360 0 ▶ if ( $routing_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($routing_key) 0 0 0
361 0 $opts{routing_key} = $routing_key;
362 }
363  
364 0 $self->_bind_exchange($cv, $source, $destination, %opts);
365 }
366 }
367 0 $cv->end;
368 }
369  
370 0 ▶sub _bind_exchange {
Subroutine _bind_exchange: not called CC=1 SCAR=6.9
371 0 my ($self, $cv, $source, $destination, %options ) = @_;
372  
373 0 $self->_guard->{flow}->begin;
374 0 $cv->begin;
375 $self->_guard->{channel}->bind_exchange(
376 %options,
377 source => $source,
378 destination => $destination,
379 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 $self->_guard->{flow}->end;
381 0 $cv->end;
382 },
383 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
384 0 $self->_handle_error( 'BindExchangeOnFailure', "source:$source, destination:$destination", @_ );
385 0 $cv->send;
386 },
387 0 );
388 }
389  
390 0 ▶sub _bind_queues {
Subroutine _bind_queues: not called CC=8 SCAR=42.8
391 0 my ($self, $cv) = @_;
392  
393 0 ▶ $cv->begin( sub { shift->send(1) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
394  
395 0 ▶ if ( $self->_has_bind_queues ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_bind_queues) 0 0 0
396 0 my @pairs;
397 0 my $bind_queues = $self->bind_queues;
398 0 ▶ if ( ref $bind_queues eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bind_queues eq 'ARRAY') { } 0 0 0
elsif (ref $bind_queues eq 'HASH') { } 0 0 0
399 0 ▶ for my $pair ( @{ $bind_queues || [] } ) {
Condition0%0/2
$bind_queues || []
l!l
00
MC/DC0%0/1
$bind_queues || []
$bind_queues
Truth table0%0/2
A
A= $bind_queues
Aresult
11
00
400 0 push @pairs, _make_pair($pair);
401 }
402 } elsif ( ref $bind_queues eq 'HASH' ) {
403 0 push @pairs, _make_pair($bind_queues);
404 }
405  
406 0 ▶ for ( my $i = 0; $i < scalar @pairs; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @pairs) 0 0 0
407 0 my $queue = $pairs[$i];
408 0 my ($exchange, $routing_key) = @{ $pairs[$i+1] };
409 0 my %opts;
410 0 ▶ if ( $routing_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($routing_key) 0 0 0
411 0 $opts{routing_key} = $routing_key;
412 }
413  
414 0 $self->_bind_queue($cv, $queue, $exchange, %opts);
415 }
416 }
417 0 $cv->end;
418 }
419  
420 0 ▶sub _bind_queue {
Subroutine _bind_queue: not called CC=1 SCAR=6.9
421 0 my ($self, $cv, $queue, $exchange, %options) = @_;
422  
423 0 $self->_guard->{flow}->begin;
424 0 $cv->begin;
425 $self->_guard->{channel}->bind_queue(
426 %options,
427 queue => $queue,
428 exchange => $exchange,
429 0 ▶ on_success => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
430 0 $self->_guard->{flow}->end;
431 0 $cv->end;
432 },
433 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 0 $self->_handle_error( 'BindQueueOnFailure', "queue:$queue, exchange:$exchange", @_ );
435 0 $cv->send;
436 },
437 0 );
438 }
439  
440  
441  
442 1 1;
443  
444 __END__
↑ Summary