lib/DR/Tnt/LowLevel/Connector.pm

filter: Statementstmt 27.8% 61 / 219 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 45.1% 14 / 31 Podpod 0.0% 0 / 7 totaltotal 24.0% 75 / 312 SCAR 61.5
CC
31
Cov
23%
check_rbuf
182.0
__ANON__
30.0
send_request
20.0
CRAP 470.9
« blib/lib/DR/Tnt/Proto.pm ↑ Summary lib/DR/Tnt/Test/TntInstance.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::Tnt::LowLevel::Connector;
6 6 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use DR::Tnt::Proto;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use List::MoreUtils 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 $Carp::Internal{ (__PACKAGE__) }++;
12 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use DR::Tnt::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 enum LLConnectorState => [
18 'init',
19 'connecting',
20 'connected',
21 'ready',
22 'error'
23 ];
24 6 ▶no Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 has host => is => 'ro', isa => 'Str', required => 1;
27 6 has port => is => 'ro', isa => 'Str', required => 1;
28 6 has user => is => 'ro', isa => 'Maybe[Str]';
29 6 has password => is => 'ro', isa => 'Maybe[Str]';
30 6 has utf8 => is => 'ro', isa => 'Bool', default => 1;
31  
32 has state =>
33 is => 'rw',
34 isa => 'LLConnectorState',
35 default => 'init',
36 trigger => sub {
37 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
38 0 goto $self->state;
39  
40 0 init:
41 $self->last_error(undef);
42 0 die 1;
43  
44 connecting: {
45 0 $self->last_error(undef);
46 0 $self->_clean_fh;
47 0 $self->_active_sync({});
48 0 $self->_watcher({});
49 0 return;
50 }
51  
52 connected:
53 0 $self->last_error(undef);
54 0 return;
55  
56 0 ready:
57 $self->last_error(undef);
58 0 return;
59  
60 error: {
61 0 $self->_clean_fh;
62  
63 0 ▶ confess "Can't set state 'error' without last_error"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->last_error 0 0 0
64 unless $self->last_error;
65
66 # on_handshake erorrs
67 0 my $list = $self->_on_handshake;
68 0 $self->_on_handshake([]);
69 0 $_->(@{ $self->last_error }) for @$list;
70
71 # waiter errors
72 0 $list = $self->_watcher;
73 0 $self->_watcher({});
74 0 $_->(@{ $self->last_error }) for map { @$_ } values %$list;
75  
76 # unsent errors
77 0 $list = $self->_unsent;
78 0 $self->_unsent([]);
79 0 $_->[-1](@{ $self->last_error }) for @$list;
80 0 return;
81 }
82 6 };
83  
84 6 has fh =>
85 is => 'ro',
86 isa => 'Maybe[Any]',
87 clearer => '_clean_fh',
88 writer => '_set_fh';
89  
90 6 has last_error_time => is => 'rw', isa => 'Num', default => 0;
91 has last_error =>
92 is => 'rw',
93 isa => 'Maybe[ArrayRef]',
94 0 ▶ trigger => sub { $_[0]->last_error_time(Time::HiRes::time) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 6 ;
96  
97 6 has greeting => is => 'rw', isa => 'Maybe[HashRef]';
98 6 has rbuf => is => 'rw', isa => 'Str', default => '';
99  
100 0 ▶has _unsent => is => 'rw', isa => 'ArrayRef', default => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
101 6 has _last_sync => is => 'rw', isa => 'Int', default => 0;
102 0 ▶has _active_sync => is => 'rw', isa => 'HashRef', default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
103 0 ▶has _watcher => is => 'rw', isa => 'HashRef', default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
104 0 ▶has _on_handshake => is => 'rw', isa => 'ArrayRef', default => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
105  
106 sub next_sync {
107 0 ▶ my ($self) = @_;
Subroutine next_sync: not called CC=3 SCAR=24.8
Pod undocumented
108  
109 0 for (my $sync = $self->_last_sync + 1;; $sync++) {
110 0 ▶ $sync = 1 if $sync > 0x7FFF_FFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $sync > 2147483647 0 0 0
111 0 ▶ next if exists $self->_active_sync->{ $sync };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_active_sync->{$sync} 0 0 0
112 0 $self->_last_sync($sync);
113 0 $self->_active_sync->{ $sync } = 1;
114 0 return $sync;
115 }
116 }
117  
118 sub connect {
119 0 ▶ my ($self, $cb) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod undocumented
120  
121 0 ▶ if (any { $_ eq $self->state } 'init', 'error', 'ready') {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $self->state; } , "init", "error", "ready")) 0 0 0
122 0 $self->_clean_fh;
123 0 $self->state('connecting');
124 $self->_connect(sub {
125 0 ▶ my ($state, $message) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
126 0 ▶ if ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'OK') { } 0 0 0
127 0 $self->state('connected');
128 } else {
129 # TODO connection error
130 0 ▶ $self->last_error([$state, $message // "Can't connect to remote host"]);
Condition0%0/2
$message // "Can't connect to remote host"
l!l
00
MC/DC0%0/1
$message // "Can't connect to remote host"
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
131 0 $self->state('error');
132 }
133 0 goto &$cb;
134 0 });
135 0 return;
136 }
137 0 $cb->(fatal => 'can not connect in state: ' . $self->state);
138 0 return;
139 }
140  
141 sub socket_error {
142 0 ▶ my ($self, $message) = @_;
Subroutine socket_error: not called CC=2 SCAR=17.9
Pod undocumented
143 0 ▶ $self->last_error([ER_SOCKET => $message // 'Socket error']);
Condition0%0/2
$message // "Socket error"
l!l
00
MC/DC0%0/1
$message // "Socket error"
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
144 0 $self->state('error');
145 }
146  
147 sub handshake {
148 0 ▶ my ($self, $cb) = @_;
Subroutine handshake: not called CC=1 SCAR=6.9
Pod undocumented
149  
150 0 goto $self->state;
151  
152 0 ready:
153 $cb->(OK => 'Handshake was received', $self->greeting);
154 0 return;
155  
156 init:
157 connecting:
158 connected:
159 0 push @{ $self->_on_handshake } => $cb;
160 0 return;
161  
162 error:
163 0 $cb->(@{ $self->last_error });
164 0 return;
165 }
166  
167  
168 sub send_request {
169 0 ▶ my $cb = pop;
Subroutine send_request: not called CC=4 SCAR=30.0
Pod undocumented
170 0 my ($self, $name, @args) = @_;
171  
172  
173 0 goto $self->state;
174  
175  
176 error: {
177 0 $cb->(@{ $self->last_error });
178 0 return;
179 }
180  
181  
182 ready: {
183 0 state $r = {
184 select => \&DR::Tnt::Proto::select,
185 update => \&DR::Tnt::Proto::update,
186 insert => \&DR::Tnt::Proto::insert,
187 replace => \&DR::Tnt::Proto::replace,
188 delete => \&DR::Tnt::Proto::del,
189 call_lua => \&DR::Tnt::Proto::call_lua,
190 eval_lua => \&DR::Tnt::Proto::eval_lua,
191 ping => \&DR::Tnt::Proto::ping,
192 auth => \&DR::Tnt::Proto::auth,
193 };
194  
195 0 ▶ croak "unknown method $name" unless exists $r->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $r->{$name} 0 0 0
196  
197 state $ra = {
198 auth => sub {
199 0 ▶ my ($self, $schema_id, $user, $password) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
200 return (
201 $schema_id,
202 $user // $self->user,
203 $password // $self->password,
204 $self->greeting->{salt},
205 0 ▶ );
Condition0%0/6
$user // $self->user
l!l&&r!l&&!r
000
$password // $self->password
l!l&&r!l&&!r
000
MC/DC0%0/2
$user // $self->user
$user $self->user
MC/DC0%0/2
$password // $self->password
$password $self->password
Truth table0%0/3
A // B
A= $userB= $self->user
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $passwordB= $self->password
ABresult
1X1
011
000
206 }
207 0 };
208
209 0 ▶ @args = $ra->{$name}->($self, @args) if exists $ra->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ra->{$name} 0 0 0
210
211 0 my $sync = $self->next_sync;
212 0 my $pkt = $r->{$name}->($sync, @args);
213  
214 0 ▶ if ($ENV{DR_SEND_DUMP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DR_SEND_DUMP"}) 0 0 0
215 0 warn pkt_dump($name, $pkt);
216 }
217  
218  
219 $self->send_pkt($pkt, sub {
220 0 ▶ my ($state, $message) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
221 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
222 0 $self->last_error([$state, $message]);
223 0 $self->state('error');
224 0 $self->fh(undef);
225 0 goto &$cb;
226 }
227 0 $cb->(OK => sprintf("packet '%s' sent", $name), $sync);
228 0 });
229  
230 0 return;
231 }
232  
233 init:
234 connected:
235 connecting:
236 {
237 0 push @{ $self->_unsent } => [ $name, @args, $cb ];
238 0 return;
239 }
240 0 }
241  
242 sub wait_response {
243 0 ▶ my ($self, $sync, $cb) = @_;
Subroutine wait_response: not called CC=3 SCAR=24.8
Pod undocumented
244 0 ▶ unless (exists $self->_active_sync->{$sync}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_active_sync->{$sync}) 0 0 0
245 0 $cb->(ER_FATAL => "Request $sync was not sent");
246 0 return;
247 }
248 0 ▶ if (ref $self->_active_sync->{$sync}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->_active_sync->{$sync}) 0 0 0
249 0 my $resp = delete $self->_active_sync->{$sync};
250 0 $cb->(OK => 'Request was read', $resp);
251 0 return;
252 }
253 0 push @{ $self->_watcher->{$sync} } => $cb;
254 0 return;
255 }
256  
257 sub check_rbuf {
258 0 ▶ my ($self) = @_;
Subroutine check_rbuf: not called CC=13 SCAR=52.0
Pod undocumented
259
260 0 my $found = 0;
261  
262 # handshake
263 0 goto $self->state;
264  
265 ready: {
266 0 ▶ while (length $self->rbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->rbuf) 0 0 0
267 0 my ($res, $tail) = DR::Tnt::Proto::response($self->rbuf, $self->utf8);
268 0 ▶ return $found unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 0 0
269
270 0 $found++;
271  
272 0 $self->rbuf($tail);
273  
274 0 my $sync = $res->{SYNC};
275 0 ▶ if (exists $self->_watcher->{$sync}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_watcher->{$sync}) 0 0 0
276 0 my $list = delete $self->_watcher->{$sync};
277 0 delete $self->_active_sync->{$sync};
278 0 for my $cb (@$list) {
279 0 $cb->(OK => 'Response received', $res);
280 }
281 0 next;
282 }
283  
284 0 ▶ unless (exists $self->_active_sync->{$sync}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_active_sync->{$sync}) 0 0 0
285 0 warn "Unexpected tarantool reply $sync";
286 0 next;
287 }
288  
289 0 $self->_active_sync->{$sync} = $res;
290 };
291 0 return $found;
292 }
293  
294 connected: {
295 0 ▶ return $found if 128 > length $self->rbuf;
Branch: 1 branch
BranchTrueFalseTotal
if 128 > length $self->rbuf 0 0 0
296 0 my $handshake = substr $self->rbuf, 0, 128;
297 0 $self->rbuf(substr $self->rbuf, 128);
298  
299 0 my $greeting = DR::Tnt::Proto::parse_greeting($handshake);
300 0 ▶ unless ($greeting and $greeting->{salt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($greeting and $greeting->{"salt"}) 0 0 0
Condition0%0/3
$greeting and $greeting->{"salt"}
!ll&&!rl&&r
000
MC/DC0%0/2
$greeting and $greeting->{"salt"}
$greeting $greeting->{"salt"}
Truth table0%0/3
A and B
A= $greetingB= $greeting->{"salt"}
ABresult
0X0
100
111
301 0 $self->fh(undef);
302 0 $self->last_error([ER_HANDSHAKE => 'Broken handshake']);
303 0 $self->state('error');
304 0 return $found;
305 }
306
307 0 $self->greeting($greeting);
308 0 $self->state('ready');
309
310 0 for (my $list = $self->_on_handshake) {
311 0 $self->_on_handshake([]);
312 0 $_->(OK => 'Handshake received', $self->greeting) for @$list;
313 0 $found++;
314 }
315
316 0 for (my $list = $self->_unsent) {
317 0 $self->_unsent([]);
318 0 $self->send_request(@$_) for @$list;
319 }
320 0 goto ready;
321 }
322 }
323 6 __PACKAGE__->meta->make_immutable;
« blib/lib/DR/Tnt/Proto.pm ↑ Summary lib/DR/Tnt/Test/TntInstance.pm »