blib/lib/Net/AMQP/ConnectionMgr.pm

filter: Statementstmt 84.2% 59 / 70 Branchbran 58.3% 14 / 24 Conditioncond 50.0% 10 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 69.0% 98 / 142 CC 23 SCAR 15.5
CC
23
Cov
73%
with_connection_do
7.2
new
5.9
declare_resource
5.8
CRAP 4.7
↑ Summary
1 package Net::AMQP::ConnectionMgr;
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  
5 1 our $VERSION = '0.0.1';
6  
7 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.8
Pod documented
8 1 my $self = shift;
9 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
10 1 ▶ my $hostname = shift || 'localhost';
Condition50%1/2
shift() || 'localhost'
l!l
01
MC/DC0%0/1
shift() || 'localhost'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
11 1 ▶ my $options = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
12 1 ▶ my $conn_class = shift || 'Net::AMQP::RabbitMQ';
Condition50%1/2
shift() || 'Net::AMQP::RabbitMQ'
l!l
10
MC/DC0%0/1
shift() || 'Net::AMQP::RabbitMQ'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
13 1 return bless
14 { hostname => $hostname,
15 options => $options,
16 conn_class => $conn_class,
17 }, $class;
18 }
19  
20 2 ▶sub _rmq_connect {
Subroutine _rmq_connect: called CC=3 SCAR=14.2
21 2 my $self = shift;
22 2 ▶ if (!$self->{conn_class}->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"conn_class"}->can("new")) 0 2 2
23 0 eval "require $self->{conn_class}";
24 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
25 0 die "Failed to load $self->{conn_class}: $@"
26 }
27 }
28 2 $self->{rmq} = $self->{conn_class}->new();
29 2 $self->{rmq}->connect($self->{hostname}, $self->{options});
30 # we only need to run channel declarations, because resource
31 # declarations get a specific channel which runs them
32 2 $self->_run_channel_declarations();
33 }
34  
35 2 ▶sub _run_channel_declarations {
Subroutine _run_channel_declarations: called CC=3 SCAR=11.2
36 2 my $self = shift;
37  
38 2 ▶ if (!$self->{declare_channel_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"declare_channel_data"}) 0 2 2
39 0 $self->{declare_channel_data} = [];
40 }
41 2 my $data = $self->{declare_channel_data};
42 2 my $channel_count = 0;
43 2 for my $code (@$data) {
44 4 $channel_count++;
45 4 $self->{rmq}->channel_open($channel_count);
46 4 $code->($self->{rmq}, $channel_count);
47 }
48 }
49  
50 2 ▶sub with_connection_do {
Subroutine with_connection_do: called CC=7 SCAR=19.7
Pod documented
51 2 my ($self, $code, $retry) = @_;
52 2 ▶ $retry ||= 1;
Condition50%1/2
$retry ||= 1
l!l
02
MC/DC0%0/1
$retry ||= 1
$retry
Truth table50%1/2
A
A= $retry
Aresult
11
00
53 2 ▶ while ($retry >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry >= 0) 3 0 3
54 3 eval {
55 3 ▶ if (!$self->{rmq} || !$self->{rmq}->is_connected()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"rmq"} and $self->{"rmq"}->is_connected) 2 1 3
Condition100%3/3
$self->{"rmq"} and $self->{"rmq"}->is_connected
!ll&&!rl&&r
111
MC/DC100%2/2
$self->{"rmq"} and $self->{"rmq"}->is_connected
$self->{"rmq"} $self->{"rmq"}->is_connected
Truth table100%3/3
A and B
A= $self->{"rmq"}B= $self->{"rmq"}->is_connected
ABresult
0X0
100
111
56 2 $self->_rmq_connect();
57 }
58 3 $code->($self->{rmq});
59 };
60 3 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 2 1 3
61 2 last;
62 } else {
63 1 $retry--;
64 }
65 }
66 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
67 0 die $@;
68 }
69 }
70  
71 2 ▶sub declare_channel {
Subroutine declare_channel: called CC=4 SCAR=15.2
Pod documented
72 2 my ($self, $code) = @_;
73 2 ▶ if (!$self->{declare_channel_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"declare_channel_data"}) 1 1 2
74 1 $self->{declare_channel_data} = [];
75 }
76 2 my $data = $self->{declare_channel_data};
77 2 my $next_channel = scalar(@$data) + 1;
78  
79 # we put a placeholder first, such that we don't get ourselves
80 # into a bad position in case this code fails.
81 0 ▶ $data->[$next_channel - 1] = sub { 1; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
82  
83 2 my $rmq = $self->{rmq};
84 2 ▶ if ($rmq && $rmq->is_connected()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rmq and $rmq->is_connected) 0 2 2
Condition33%1/3
$rmq and $rmq->is_connected
!ll&&!rl&&r
200
MC/DC0%0/2
$rmq and $rmq->is_connected
$rmq $rmq->is_connected
Truth table33%1/3
A and B
A= $rmqB= $rmq->is_connected
ABresult
0X0
100
111
85 0 ▶ $self->with_connection_do(sub { $rmq->channel_open($next_channel);
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 $code->($rmq, $next_channel) });
87 }
88  
89 # if we got here, then we assign
90 2 $data->[$next_channel - 1] = $code;
91 2 return $next_channel;
92 }
93  
94 1 ▶sub declare_resource {
Subroutine declare_resource: called CC=5 SCAR=17.6
Pod documented
95 1 my ($self, $code) = @_;
96 1 ▶ if (!$self->{declare_resouce_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"declare_resouce_data"}) 1 0 1
97 1 $self->{declare_resouce_data} = [];
98 }
99 1 my $data = $self->{declare_resource_data};
100 1 ▶ if (!$self->{declare_resource_channel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"declare_resource_channel"}) 1 0 1
101 $self->{declare_resource_channel} =
102 1 ▶ $self->declare_channel(sub { $_->(@_) for @$data } );
Subroutine __ANON__: called CC=2 SCAR=6.9
103 }
104 1 my $decl_ch = $self->{declare_resource_channel};
105 1 ▶ if ($self->{rmq} && $self->{rmq}->is_connected()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rmq"} and $self->{"rmq"}->is_connected) 0 1 1
Condition33%1/3
$self->{"rmq"} and $self->{"rmq"}->is_connected
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"rmq"} and $self->{"rmq"}->is_connected
$self->{"rmq"} $self->{"rmq"}->is_connected
Truth table33%1/3
A and B
A= $self->{"rmq"}B= $self->{"rmq"}->is_connected
ABresult
0X0
100
111
106 0 ▶ $self->with_connection_do(sub { $code->($_[0], $decl_ch) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 }
108 1 push @$data, $code;
109 1 return 1;
110 }
111  
112 1 1;
113  
114 __END__
↑ Summary