blib/lib/DR/Tarantool/AEConnection.pm

filter: Statementstmt 81.3% 109 / 134 Branchbran 50.0% 25 / 50 Conditioncond 31.2% 10 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 77.7% 28 / 36 Podpod 0.0% 0 / 15 totaltotal 59.7% 172 / 288 CC 37 SCAR 18.7
CC
37
Cov
67%
push_write
20.0
__ANON__
20.0
new
14.2
CRAP 6.5
« blib/lib/DR/Tarantool/Tuple.pm ↑ Summary blib/lib/DR/Tarantool/MsgPack/SyncClient.pm »
1 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DR::Tarantool::AEConnection;
6 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub _errno() {
13 12 ▶ while (my ($k, $v) = each(%!)) {
Subroutine _errno: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %!) 804 6 810
14 804 ▶ return $k if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 6 798 804
15 }
16 6 return $!;
17 }
18  
19 sub new {
20 6 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=9 SCAR=26.5
Pod undocumented
21  
22 6 $opts{state} = 'init';
23 6 ▶ $opts{host} ||= '127.0.0.1';
Condition50%1/2
$opts{"host"} ||= "127.0.0.1"
l!l
06
MC/DC0%0/1
$opts{"host"} ||= "127.0.0.1"
$opts{"host"}
Truth table50%1/2
A
A= $opts{"host"}
Aresult
11
00
24 6 ▶ croak 'port is undefined' unless $opts{port};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"port"} 0 6 6
25  
26  
27 6 ▶ $opts{on}{connected} ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$opts{"on"}{"connected"} ||= sub { }
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"on"}{"connected"} ||= sub { }
$opts{"on"}{"connected"} sub { }
Truth table33%1/3
A ||= B
A= $opts{"on"}{"connected"}B= sub { }
ABresult
1X1
011
000
28 6 ▶ $opts{on}{connfail} ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$opts{"on"}{"connfail"} ||= sub { }
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"on"}{"connfail"} ||= sub { }
$opts{"on"}{"connfail"} sub { }
Truth table33%1/3
A ||= B
A= $opts{"on"}{"connfail"}B= sub { }
ABresult
1X1
011
000
29 6 ▶ $opts{on}{disconnect} ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$opts{"on"}{"disconnect"} ||= sub { }
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"on"}{"disconnect"} ||= sub { }
$opts{"on"}{"disconnect"} sub { }
Truth table33%1/3
A ||= B
A= $opts{"on"}{"disconnect"}B= sub { }
ABresult
1X1
011
000
30 6 ▶ $opts{on}{error} ||= sub { };
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
$opts{"on"}{"error"} ||= sub { }
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"on"}{"error"} ||= sub { }
$opts{"on"}{"error"} sub { }
Truth table33%1/3
A ||= B
A= $opts{"on"}{"error"}B= sub { }
ABresult
1X1
011
000
31 6 ▶ $opts{on}{reconnecting} ||= sub { };
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
$opts{"on"}{"reconnecting"} ||= sub { }
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"on"}{"reconnecting"} ||= sub { }
$opts{"on"}{"reconnecting"} sub { }
Truth table33%1/3
A ||= B
A= $opts{"on"}{"reconnecting"}B= sub { }
ABresult
1X1
011
000
32  
33 6 $opts{success_connects} = 0;
34 6 $opts{wbuf} = '';
35  
36 6 $opts{read} = { any => [] };
37  
38 6 ▶ bless \%opts => ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39 }
40  
41  
42 sub on {
43 8 ▶ my ($self, $name, $cb) = @_;
Subroutine on: called CC=3 SCAR=12.0
Pod undocumented
44 8 ▶ croak "wrong event name: $name" unless exists $self->{on}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"on"}{$name} 0 8 8
45 8 ▶ $self->{on}{$name} = $cb || sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb || sub { }
l!l&&r!l&&!r
800
MC/DC0%0/2
$cb || sub { }
$cb sub { }
Truth table33%1/3
A || B
A= $cbB= sub { }
ABresult
1X1
011
000
46 8 $self;
47 }
48  
49 6 ▶sub fh { $_[0]->{fh} }
Subroutine fh: called CC=1 SCAR=0.0
Pod undocumented
50 85 ▶sub state { $_[0]->{state} }
Subroutine state: called CC=1 SCAR=0.0
Pod undocumented
51 16 ▶sub host { $_[0]->{host} }
Subroutine host: called CC=1 SCAR=0.0
Pod undocumented
52 16 ▶sub port { $_[0]->{port} }
Subroutine port: called CC=1 SCAR=0.0
Pod undocumented
53 0 ▶sub error { $_[0]->{error} }
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
54 2 ▶sub errno { $_[0]->{errno} }
Subroutine errno: called CC=1 SCAR=0.0
Pod undocumented
55 13 ▶sub reconnect_always { $_[0]->{reconnect_always} }
Subroutine reconnect_always: called CC=1 SCAR=0.0
Pod undocumented
56 27 ▶sub reconnect_period { $_[0]->{reconnect_period} }
Subroutine reconnect_period: called CC=1 SCAR=0.0
Pod undocumented
57 sub timeout {
58 17 ▶ my ($self) = @_;
Subroutine timeout: called CC=2 SCAR=8.1
Pod undocumented
59 17 ▶ return $self->{timeout} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 17 0 17
60 0 return $self->{timeout} = $_[1];
61 }
62  
63  
64 sub set_error {
65 2 ▶ my ($self, $error, $errno) = @_;
Subroutine set_error: called CC=2 SCAR=7.0
Pod undocumented
66 2 ▶ $errno ||= $error;
Condition33%1/3
$errno ||= $error
l!l&&r!l&&!r
020
MC/DC0%0/2
$errno ||= $error
$errno $error
Truth table33%1/3
A ||= B
A= $errnoB= $error
ABresult
1X1
011
000
67 2 $self->{state} = 'error';
68 2 $self->{error} = $error;
69 2 $self->{errno} = $errno;
70 2 $self->{on}{error}($self);
71 2 $self->{guard} = {};
72 2 $self->{wbuf} = '';
73  
74 2 $self->_check_reconnect;
75
76 }
77  
78 sub _check_reconnect {
79 15 ▶ Scalar::Util::weaken(my $self = shift);
Subroutine _check_reconnect: called CC=7 SCAR=19.7
80 15 ▶ return if $self->state eq 'connected';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "connected" 0 15 15
81 15 ▶ return if $self->state eq 'connecting';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "connecting" 0 15 15
82 15 ▶ return if $self->{guard}{rc};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"guard"}{"rc"} 0 15 15
83  
84 15 ▶ return unless $self->reconnect_period;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->reconnect_period 2 13 15
85 13 ▶ unless ($self->reconnect_always) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->reconnect_always) 3 10 13
86 3 ▶ return unless $self->{success_connects};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"success_connects"} 1 2 3
87 }
88  
89 $self->{guard}{rc} = AE::timer $self->reconnect_period, 0, sub {
90 10 ▶ return unless $self;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 10 10
91 10 delete $self->{guard}{rc};
92 10 $self->{on}{reconnecting}($self);
93 10 $self->connect;
94 12 };
95 }
96  
97 sub connect {
98 16 ▶ Scalar::Util::weaken(my $self = shift);
Subroutine connect: called CC=4 SCAR=13.9
Pod undocumented
99  
100 16 ▶ return if $self->state eq 'connected' or $self->state eq 'connecting';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "connected" or $self->state eq "connecting" 0 16 16
Condition33%1/3
$self->state eq "connected" or $self->state eq "connecting"
l!l&&r!l&&!r
0016
MC/DC0%0/2
$self->state eq "connected" or $self->state eq "connecting"
$self->state eq "connected" $self->state eq "connecting"
Truth table33%1/3
A or B
A= $self->state eq "connected"B= $self->state eq "connecting"
ABresult
1X1
011
000
101  
102 16 $self->{state} = 'connecting';
103 16 $self->{error} = undef;
104 16 $self->{errno} = undef;
105 16 $self->{guard} = {};
106  
107 $self->{guard}{c} = AnyEvent::Socket::tcp_connect
108 $self->host,
109 $self->port,
110 sub {
111 15 ▶ $self->{guard} = {};
Subroutine __ANON__: called CC=4 SCAR=13.9
112 15 my ($fh) = @_;
113 15 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 3 12 15
114 3 $self->{fh} = $fh;
115 3 $self->{state} = 'connected';
116 3 $self->{success_connects}++;
117 3 ▶ $self->push_write('') if length $self->{wbuf};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"wbuf"} 0 3 3
118 3 $self->{on}{connected}($self);
119 3 return;
120 }
121
122 12 $self->{error} = $!;
123 12 $self->{errno} = _errno;
124 12 $self->{state} = 'connfail';
125 12 $self->{guard} = {};
126 12 $self->{on}{connfail}($self);
127 12 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 12 12
128 12 $self->_check_reconnect;
129 },
130 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
131  
132 }
133 16 ;
134  
135 16 ▶ if (defined $self->timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->timeout) 1 15 16
136 $self->{guard}{t} = AE::timer $self->timeout, 0, sub {
137 1 ▶ delete $self->{guard}{t};
Subroutine __ANON__: called CC=2 SCAR=7.0
138 1 ▶ return unless $self->state eq 'connecting';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->state eq "connecting" 0 1 1
139  
140 1 $self->{error} = 'Connection timeout';
141 1 $self->{errno} = 'ETIMEOUT';
142 1 $self->{state} = 'connfail';
143 1 $self->{guard} = {};
144 1 $self->{on}{connfail}($self);
145 1 $self->_check_reconnect;
146 1 };
147 }
148
149 16 $self;
150 }
151  
152 sub disconnect {
153 0 ▶ Scalar::Util::weaken(my $self = shift);
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod undocumented
154 0 ▶ return if $self->state eq 'disconnect' or $self->state eq 'init';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "disconnect" or $self->state eq "init" 0 0 0
Condition0%0/3
$self->state eq "disconnect" or $self->state eq "init"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->state eq "disconnect" or $self->state eq "init"
$self->state eq "disconnect" $self->state eq "init"
Truth table0%0/3
A or B
A= $self->state eq "disconnect"B= $self->state eq "init"
ABresult
1X1
011
000
155  
156 0 $self->{guard} = {};
157 0 $self->{error} = 'Disconnected';
158 0 $self->{errno} = 'SUCCESS';
159 0 $self->{state} = 'disconnect';
160 0 $self->{wbuf} = '';
161 0 $self->{on}{disconnect}($self);
162 }
163  
164  
165 sub push_write {
166 0 ▶ Scalar::Util::weaken(my $self = shift);
Subroutine push_write: not called CC=4 SCAR=30.0
Pod undocumented
167 0 my ($str) = @_;
168  
169 0 $self->{wbuf} .= $str;
170  
171 0 ▶ return unless $self->state eq 'connected';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->state eq "connected" 0 0 0
172 0 ▶ return unless length $self->{wbuf};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"wbuf"} 0 0 0
173 0 ▶ return if $self->{guard}{write};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"guard"}{"write"} 0 0 0
174  
175 $self->{guard}{write} = AE::io $self->fh, 1, sub {
176 0 ▶ my $l = syswrite $self->fh, $self->{wbuf};
Subroutine __ANON__: not called CC=4 SCAR=30.0
177 0 ▶ unless(defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 0 0 0
178 0 ▶ return if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
179 0 $self->set_error($!, _errno);
180 0 return;
181 }
182 0 substr $self->{wbuf}, 0, $l, '';
183 0 ▶ return if length $self->{wbuf};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"wbuf"} 0 0 0
184 0 delete $self->{guard}{write};
185 0 };
186 }
187  
188  
189  
190  
191 6 1;
« blib/lib/DR/Tarantool/Tuple.pm ↑ Summary blib/lib/DR/Tarantool/MsgPack/SyncClient.pm »