blib/lib/Cassandra/Client/AsyncEV.pm

filter: Statementstmt 16.5% 17 / 103 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 21.7% 5 / 23 Podpod 0.0% 0 / 12 totaltotal 11.6% 22 / 189 CC 20 SCAR 28.6
CC
20
Cov
12%
handle_timeouts
56.0
unregister
30.0
deadline
20.0
CRAP 17.4
« blib/lib/Cassandra/Client/Policy/Throttle/Adaptive.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Retry/Default.pm »
1 package Cassandra::Client::AsyncEV;
2 13 our $AUTHORITY = 'cpan:TVDW';
3 13 $Cassandra::Client::AsyncEV::VERSION = '0.21';
4 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Time::HiRes qw(CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use vars qw/@TIMEOUTS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
12 0 my ($class, %args)= @_;
13  
14 0 my $options= $args{options};
15  
16 0 require EV;
17  
18 return bless {
19 0 ▶ timer_granularity => ($options->{timer_granularity} || 0.1),
Condition0%0/2
$options->{'timer_granularity'} || 0.1
l!l
00
MC/DC0%0/1
$options->{'timer_granularity'} || 0.1
$options->{'timer_granularity'}
Truth table0%0/2
A
A= $options->{'timer_granularity'}
Aresult
11
00
20 ev_read => {},
21 ev_write => {},
22 ev_timeout => undef,
23 fh_to_obj => {},
24 timeouts => [],
25 ev => EV::Loop->new(),
26 }, $class;
27 }
28  
29 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my ($self, $fh, $connection)= @_;
31 0 $self->{fh_to_obj}{$fh}= $connection;
32 0 return;
33 }
34  
35 0 ▶sub unregister {
Subroutine unregister: not called CC=5 SCAR=34.0
Pod undocumented
36 0 my ($self, $fh)= @_;
37 0 delete $self->{fh_to_obj}{$fh};
38 0 ▶ if ($self->{timeouts} && grep { $_->[1] == $fh && !$_->[3] } @{$self->{timeouts}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"timeouts"} and grep {$_->[1] == $fh and not $_->[3];} @{$$self{"timeouts"};}) 0 0 0
Condition0%0/6
$self->{"timeouts"} and grep {$_->[1] == $fh and not $_->[3];} @{$$self{"timeouts"};}
!ll&&!rl&&r
000
$_->[1] == $fh && !$_->[3]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"timeouts"} and grep {$_->[1] == $fh and not $_->[3];} @{$$self{"timeouts"};}
$self->{"timeouts"} grep {$_->[1] == $fh and not $_->[3];} @{$$self{"timeouts"};}
MC/DC0%0/2
$_->[1] == $fh && !$_->[3]
$_->[1] == $fh !$_->[3]
Truth table0%0/3
A and B
A= $self->{"timeouts"}B= grep {$_->[1] == $fh and not $_->[3];} @{$$self{"timeouts"};}
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $_->[1] == $fhB= !$_->[3]
ABresult
0X0
100
111
39 0 warn 'In unregister(): not all timeouts were dismissed!';
40 }
41 0 @{$self->{timeouts}}= grep { $_->[1] != $fh } @{$self->{timeouts}};
42 0 ▶ undef $self->{ev_timeout} unless @{$self->{timeouts}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"timeouts"};} 0 0 0
43 0 return;
44 }
45  
46 0 ▶sub register_read {
Subroutine register_read: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my ($self, $fh)= @_;
48 0 ▶ my $connection= $self->{fh_to_obj}{$fh} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $self->{"fh_to_obj"}{$fh} or die 0 0 0
49  
50 0 ▶ $self->{ev_read}{$fh}= $self->{ev}->io( $fh, &EV::READ, sub { $connection->can_read } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 return;
52 }
53  
54 0 ▶sub register_write {
Subroutine register_write: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my ($self, $fh)= @_;
56 0 ▶ my $connection= $self->{fh_to_obj}{$fh} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $self->{"fh_to_obj"}{$fh} or die 0 0 0
57  
58 0 ▶ $self->{ev_write}{$fh}= $self->{ev}->io( $fh, &EV::WRITE, sub { $connection->can_write } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 return;
60 }
61  
62 0 ▶sub unregister_read {
Subroutine unregister_read: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my ($self, $fh)= @_;
64 0 undef $self->{ev_read}{$fh};
65  
66 0 return;
67 }
68  
69 0 ▶sub unregister_write {
Subroutine unregister_write: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my ($self, $fh)= @_;
71 0 undef $self->{ev_write}{$fh};
72  
73 0 return;
74 }
75  
76 0 ▶sub deadline {
Subroutine deadline: not called CC=4 SCAR=30.0
Pod undocumented
77 0 my ($self, $fh, $id, $timeout)= @_;
78 0 local *TIMEOUTS= $self->{timeouts};
79  
80 0 ▶ if (!$self->{ev_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ev_timeout"}) 0 0 0
81 0 ▶ $self->{ev_timeout}= $self->{ev}->timer( $self->{timer_granularity}, $self->{timer_granularity}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 $self->handle_timeouts(Time::HiRes::clock_gettime(CLOCK_MONOTONIC));
83 0 } );
84 }
85  
86 0 my $curtime= Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
87 0 my $deadline= $curtime + $timeout;
88 0 my $additem= [ $deadline, $fh, $id, 0 ];
89  
90 0 ▶ if (@TIMEOUTS && $TIMEOUTS[-1][0] > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Cassandra::Client::AsyncEV::TIMEOUTS and $Cassandra::Client::AsyncEV::TIMEOUTS[-1][0] > $deadline) { } 0 0 0
Condition0%0/3
@Cassandra::Client::AsyncEV::TIMEOUTS and $Cassandra::Client::AsyncEV::TIMEOUTS[-1][0] > $deadline
!ll&&!rl&&r
000
MC/DC0%0/2
@Cassandra::Client::AsyncEV::TIMEOUTS and $Cassandra::Client::AsyncEV::TIMEOUTS[-1][0] > $deadline
@Cassandra::Client::AsyncEV::TIMEOUTS $Cassandra::Client::AsyncEV::TIMEOUTS[-1][0] > $deadline
Truth table0%0/3
A and B
A= @Cassandra::Client::AsyncEV::TIMEOUTSB= $Cassandra::Client::AsyncEV::TIMEOUTS[-1][0] > $deadline
ABresult
0X0
100
111
91 # Grumble... that's slow
92 0 push @TIMEOUTS, $additem;
93 0 @TIMEOUTS= sort { $a->[0] <=> $b->[0] } @TIMEOUTS;
94 } else {
95 # Common case
96 0 push @TIMEOUTS, $additem;
97 }
98  
99 0 return \($additem->[3]);
100 }
101  
102 0 ▶sub handle_timeouts {
Subroutine handle_timeouts: not called CC=7 SCAR=40.3
Pod undocumented
103 0 my ($self, $curtime)= @_;
104  
105 0 local *TIMEOUTS= $self->{timeouts};
106  
107 0 my %triggered_read;
108 0 ▶ while (@TIMEOUTS && $curtime >= $TIMEOUTS[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Cassandra::Client::AsyncEV::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncEV::TIMEOUTS[0][0]) 0 0 0
Condition0%0/3
@Cassandra::Client::AsyncEV::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncEV::TIMEOUTS[0][0]
!ll&&!rl&&r
000
MC/DC0%0/2
@Cassandra::Client::AsyncEV::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncEV::TIMEOUTS[0][0]
@Cassandra::Client::AsyncEV::TIMEOUTS $curtime >= $Cassandra::Client::AsyncEV::TIMEOUTS[0][0]
Truth table0%0/3
A and B
A= @Cassandra::Client::AsyncEV::TIMEOUTSB= $curtime >= $Cassandra::Client::AsyncEV::TIMEOUTS[0][0]
ABresult
0X0
100
111
109 0 my $item= shift @TIMEOUTS;
110 0 ▶ if (!$item->[3]) { # If it timed out
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->[3]) 0 0 0
111 0 my ($deadline, $fh, $id, $timedout)= @$item;
112 0 my $obj= $self->{fh_to_obj}{$fh};
113 0 ▶ $obj->can_read unless $triggered_read{$fh}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $triggered_read{$fh}++ 0 0 0
114 0 ▶ $obj->can_timeout($id) unless $item->[3]; # We may have received an answer...
Branch: 1 branch
BranchTrueFalseTotal
unless $item->[3] 0 0 0
115 }
116 }
117  
118 0 ▶ if (!@TIMEOUTS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Cassandra::Client::AsyncEV::TIMEOUTS) 0 0 0
119 0 $self->{ev_timeout}= undef;
120 }
121  
122 0 return;
123 }
124  
125 0 ▶sub timer {
Subroutine timer: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ($self, $callback, $wait)= @_;
127 0 ▶ my $t; $t= $self->{ev}->timer($wait, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 undef $t;
129 0 $callback->();
130 0 });
131 }
132  
133 0 ▶sub later {
Subroutine later: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my ($self, $callback)= @_;
135 0 $self->timer($callback, 0);
136 }
137  
138 # $something->($async->wait(my $w)); my ($error, $result)= $w->();
139 0 ▶sub wait {
Subroutine wait: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my ($self)= @_;
141 0 my $output= \$_[1];
142  
143 0 my ($done, $in_run);
144 0 my @output;
145 0 ▶ my $callback= sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 $done= 1;
147 0 @output= @_;
148 0 ▶ $self->{ev}->break() if $in_run;
Branch: 1 branch
BranchTrueFalseTotal
if $in_run 0 0 0
149 0 };
150  
151 0 ▶ $$output= sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
152 0 ▶ if ($self->{in_wait}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_wait"}) 0 0 0
153 0 die "Unable to recursively wait for callbacks; are you doing synchronous Cassandra queries from asynchronous callbacks?";
154 }
155 0 local $self->{in_wait}= 1;
156  
157 0 $in_run= 1;
158 0 ▶ $self->{ev}->run unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 0 0
159 0 return @output;
160 0 };
161  
162 0 return $callback;
163 }
164  
165 13 1;
166  
167 __END__
« blib/lib/Cassandra/Client/Policy/Throttle/Adaptive.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Retry/Default.pm »