blib/lib/Cassandra/Client/AsyncAnyEvent.pm

filter: Statementstmt 17.3% 17 / 98 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 21.7% 5 / 23 Podpod 0.0% 0 / 12 totaltotal 12.3% 22 / 178 CC 17 SCAR 28.7
CC
17
Cov
13%
handle_timeouts
56.0
unregister
30.0
deadline
20.0
CRAP 17.6
« blib/lib/Cassandra/Client.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Throttle/Adaptive.pm »
1 package Cassandra::Client::AsyncAnyEvent;
2 13 our $AUTHORITY = 'cpan:TVDW';
3 13 $Cassandra::Client::AsyncAnyEvent::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 AnyEvent;
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 ae_read => {},
21 ae_write => {},
22 ae_timeout => undef,
23 fh_to_obj => {},
24 timeouts => [],
25 }, $class;
26 }
27  
28 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my ($self, $fh, $connection)= @_;
30 0 $self->{fh_to_obj}{$fh}= $connection;
31 0 return;
32 }
33  
34 0 ▶sub unregister {
Subroutine unregister: not called CC=5 SCAR=34.0
Pod undocumented
35 0 my ($self, $fh)= @_;
36 0 delete $self->{fh_to_obj}{$fh};
37 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
38 0 warn 'In unregister(): not all timeouts were dismissed!';
39 }
40 0 @{$self->{timeouts}}= grep { $_->[1] != $fh } @{$self->{timeouts}};
41 0 ▶ undef $self->{ae_timeout} unless @{$self->{timeouts}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"timeouts"};} 0 0 0
42 0 return;
43 }
44  
45 0 ▶sub register_read {
Subroutine register_read: not called CC=2 SCAR=17.9
Pod undocumented
46 0 my ($self, $fh)= @_;
47 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
48  
49 $self->{ae_read}{$fh}= AnyEvent->io(
50 poll => 'r',
51 fh => $fh,
52 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 $connection->can_read;
54 },
55 0 );
56  
57 0 return;
58 }
59  
60 0 ▶sub register_write {
Subroutine register_write: not called CC=2 SCAR=17.9
Pod undocumented
61 0 my ($self, $fh)= @_;
62 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
63  
64 $self->{ae_write}{$fh}= AnyEvent->io(
65 poll => 'w',
66 fh => $fh,
67 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 $connection->can_write;
69 },
70 0 );
71  
72 0 return;
73 }
74  
75 0 ▶sub unregister_read {
Subroutine unregister_read: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my ($self, $fh)= @_;
77 0 undef $self->{ae_read}{$fh};
78  
79 0 return;
80 }
81  
82 0 ▶sub unregister_write {
Subroutine unregister_write: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my ($self, $fh)= @_;
84 0 undef $self->{ae_write}{$fh};
85  
86 0 return;
87 }
88  
89 0 ▶sub deadline {
Subroutine deadline: not called CC=4 SCAR=30.0
Pod undocumented
90 0 my ($self, $fh, $id, $timeout)= @_;
91 0 local *TIMEOUTS= $self->{timeouts};
92  
93 0 ▶ if (!$self->{ae_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ae_timeout"}) 0 0 0
94 $self->{ae_timeout}= AnyEvent->timer(
95 after => $self->{timer_granularity},
96 interval => $self->{timer_granularity},
97 0 ▶ cb => sub { $self->handle_timeouts(Time::HiRes::clock_gettime(CLOCK_MONOTONIC)) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 );
99 }
100  
101 0 my $curtime= Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
102 0 my $deadline= $curtime + $timeout;
103 0 my $additem= [ $deadline, $fh, $id, 0 ];
104  
105 0 ▶ if (@TIMEOUTS && $TIMEOUTS[-1][0] > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[-1][0] > $deadline) { } 0 0 0
Condition0%0/3
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[-1][0] > $deadline
!ll&&!rl&&r
000
MC/DC0%0/2
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[-1][0] > $deadline
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[-1][0] > $deadline
Truth table0%0/3
A and B
A= @Cassandra::Client::AsyncAnyEvent::TIMEOUTSB= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[-1][0] > $deadline
ABresult
0X0
100
111
106 # Grumble... that's slow
107 0 push @TIMEOUTS, $additem;
108 0 @TIMEOUTS= sort { $a->[0] <=> $b->[0] } @TIMEOUTS;
109 } else {
110 # Common case
111 0 push @TIMEOUTS, $additem;
112 }
113  
114 0 return \($additem->[3]);
115 }
116  
117 0 ▶sub handle_timeouts {
Subroutine handle_timeouts: not called CC=7 SCAR=40.3
Pod undocumented
118 0 my ($self, $curtime)= @_;
119  
120 0 local *TIMEOUTS= $self->{timeouts};
121  
122 0 my %triggered_read;
123 0 ▶ while (@TIMEOUTS && $curtime >= $TIMEOUTS[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[0][0]) 0 0 0
Condition0%0/3
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[0][0]
!ll&&!rl&&r
000
MC/DC0%0/2
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS and $curtime >= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[0][0]
@Cassandra::Client::AsyncAnyEvent::TIMEOUTS $curtime >= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[0][0]
Truth table0%0/3
A and B
A= @Cassandra::Client::AsyncAnyEvent::TIMEOUTSB= $curtime >= $Cassandra::Client::AsyncAnyEvent::TIMEOUTS[0][0]
ABresult
0X0
100
111
124 0 my $item= shift @TIMEOUTS;
125 0 ▶ if (!$item->[3]) { # If it timed out
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->[3]) 0 0 0
126 0 my ($deadline, $fh, $id, $timedout)= @$item;
127 0 my $obj= $self->{fh_to_obj}{$fh};
128 0 ▶ $obj->can_read unless $triggered_read{$fh}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $triggered_read{$fh}++ 0 0 0
129 0 ▶ $obj->can_timeout($id) unless $item->[3]; # We may have received an answer...
Branch: 1 branch
BranchTrueFalseTotal
unless $item->[3] 0 0 0
130 }
131 }
132  
133 0 ▶ if (!@TIMEOUTS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Cassandra::Client::AsyncAnyEvent::TIMEOUTS) 0 0 0
134 0 $self->{ae_timeout}= undef;
135 }
136  
137 0 return;
138 }
139  
140 0 ▶sub timer {
Subroutine timer: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my ($self, $callback, $wait)= @_;
142 0 ▶ my $t; $t= AE::timer($wait, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 undef $t;
144 0 $callback->();
145 0 });
146 }
147  
148 0 ▶sub later {
Subroutine later: not called CC=1 SCAR=6.9
Pod undocumented
149 0 my ($self, $callback)= @_;
150 0 &AE::postpone($callback);
151 }
152  
153 # $something->($async->wait(my $w)); my ($error, $result)= $w->();
154 0 ▶sub wait {
Subroutine wait: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my ($self)= @_;
156 0 my $output= \$_[1];
157  
158 0 my $cv= AnyEvent->condvar;
159 0 my @output;
160 0 ▶ my $callback= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 @output= @_;
162 0 $cv->send;
163 0 };
164  
165 0 ▶ $$output= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 $cv->recv;
167 0 return @output;
168 0 };
169  
170 0 return $callback;
171 }
172  
173 13 1;
174  
175 __END__
« blib/lib/Cassandra/Client.pm ↑ Summary blib/lib/Cassandra/Client/Policy/Throttle/Adaptive.pm »