Connection.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 82 CC 7 SCAR 24.6
CC
7
Cov
0%
_do
30.0
channel
6.0
confirmed_connect
6.0
CRAP 11.8
1 package Net::RabbitMQ::Connection;
2  
3 use Class::Easy;
4  
5 use Net::RabbitMQ;
6  
7 use Net::RabbitMQ::Channel;
8 use Net::RabbitMQ::Exchange;
9 use Net::RabbitMQ::Queue;
10  
11 # has 'mq';
12  
13 has exchange_pack => 'Net::RabbitMQ::Exchange';
14 has queue_pack => 'Net::RabbitMQ::Queue';
15  
16 &init;
17  
18 sub init {
19 my $class = caller;
20
21 # singleton
22 # has instance => $class->new;
23 }
24  
25 sub new {
26 my $class = shift;
27 my $config = {@_};
28
29 $config->{mq} = Net::RabbitMQ->new;
30
31 my $self = bless $config, $class;
32  
33 $self->confirmed_connect;
34
35 return $self;
36 }
37  
38 sub channel {
39 my $self = shift;
40 my $num = shift;
41
42 $self->{_channels}->{$num} = Net::RabbitMQ::Channel->new ($num, $self)
43 unless $self->{_channels}->{$num};
44
45 return $self->{_channels}->{$num};
46 }
47  
48 # here we try to connect, if all servers fails, then die
49 sub confirmed_connect {
50 my $self = shift;
51
52 my $mq = $self->{mq};
53
54 local $@;
55
56 foreach my $host (keys %{$self->{hosts}}) {
57 eval {
58 $mq->connect ($host, $self->{hosts}->{$host});
59 };
60
61 return unless $@;
62 }
63
64 die "we can't connect to any server";
65 }
66  
67 sub _do {
68 my $self = shift;
69 my $cmd = shift;
70
71 my $verify = "_verify_$cmd";
72
73 # parameter verification
74 if ($self->can ($verify)) {
75 return unless $self->$verify (@_);
76 }
77
78 local $@;
79
80 my $result;
81 my $success = 0;
82
83 # real server work -> we must restart connection after failure
84 eval {
85 $result = $self->{mq}->$cmd (@_);
86 $success = 1;
87 };
88
89 # warn "command: $cmd, result: $result, success: $success, errors: $@";
90
91 # TODO: check for real connection error, we don't want to run erratical command another time
92 if ($@) {
93 $self->confirmed_connect ($_[0]); # send channel for reconnect
94
95 # if we have more than one failure after successful
96 # reconnect, then we must die
97 $result = $self->{mq}->$cmd (@_);
98 $success = 1;
99 }
100
101 return wantarray ? ($success, $result) : $success;
102 }
103  
104 1;