blib/lib/Test/Mock/Time.pm

filter: Statementstmt 86.1% 337 / 391 Branchbran 63.0% 87 / 138 Conditioncond 48.2% 42 / 87 MC/DCmcdc 31.5% 18 / 57 Subroutinesub 87.3% 62 / 71 Podpod 100% 1 / 1 totaltotal 73.4% 547 / 745 CC 99 SCAR 22.9
CC
99
Cov
76%
__ANON__
72.3
__ANON__
42.0
__ANON__
14.6
CRAP 9.9
↑ Summary
1 package Test::Mock::Time;
2 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our $VERSION = 'v0.2.1';
9  
10 8 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Test::MockModule;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use constant TIME_HIRES_CLOCK_NOT_SUPPORTED => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use constant MICROSECONDS => 1_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use constant NANOSECONDS => 1_000_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use constant DEFAULT_WAIT_ONE_TICK => 0.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 our $WAIT_ONE_TICK = DEFAULT_WAIT_ONE_TICK;
21  
22 8 my $Absolute = time; # usual time starts at current actual time
23 8 my $Monotonic = 0; # monotonic time starts at 0 if not available
24 8 my $Relative = 0; # how many deterministic time passed since start
25 8 my @Timers; # active timers
26 my @Timers_ns; # inactive timers
27 8 my %Module; # keep module mocks
28  
29  
30 8 _mock_core_global();
31 ## no critic (RequireCheckingReturnValueOfEval)
32 8 eval {
33 8 require Time::HiRes;
34 8 Time::HiRes->import(qw( CLOCK_REALTIME CLOCK_MONOTONIC ));
35 8 _mock_time_hires();
36 };
37 8 eval {
38 8 require EV;
39 8 _mock_ev();
40 };
41 8 eval {
42 8 require Mojolicious;
43 8 Mojolicious->VERSION('6'); # may be compatible with older ones, needs testing
44 8 require Mojo::Reactor::Poll;
45 8 _mock_mojolicious();
46 };
47  
48  
49 # FIXME make ff() reentrant
50 51 ▶sub ff :Export(:DEFAULT) {
Subroutine ff: called CC=11 SCAR=24.3
Pod documented
51 51 my ($dur) = @_;
52  
53 @Timers = sort {
54 51 $a->{start}+$a->{after} <=> $b->{start}+$b->{after} or
55 $a->{id} cmp $b->{id} # preserve order to simplify tests
56 0 ▶ } @Timers;
Condition0%0/3
$Test::Mock::Time::a->{'start'} + $Test::Mock::Time::a->{'after'} <=> $Test::Mock::Time::b->{'start'} + $Test::Mock::Time::b->{'after'} || $Test::Mock::Time::a->{'id'} cmp $Test::Mock::Time::b->{'id'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Test::Mock::Time::a->{'start'} + $Test::Mock::Time::a->{'after'} <=> $Test::Mock::Time::b->{'start'} + $Test::Mock::Time::b->{'after'} || $Test::Mock::Time::a->{'id'} cmp $Test::Mock::Time::b->{'id'}
$Test::Mock::Time::a->{'start'} + $Test::Mock::Time::a->{'after'} <=> $Test::Mock::Time::b->{'start'} + $Test::Mock::Time::b->{'after'} $Test::Mock::Time::a->{'id'} cmp $Test::Mock::Time::b->{'id'}
Truth table0%0/3
A || B
A= $Test::Mock::Time::a->{'start'} + $Test::Mock::Time::a->{'after'} <=> $Test::Mock::Time::b->{'start'} + $Test::Mock::Time::b->{'after'}B= $Test::Mock::Time::a->{'id'} cmp $Test::Mock::Time::b->{'id'}
ABresult
1X1
011
000
57 51 ▶ my $next_at = @Timers ? $Timers[0]{start}+$Timers[0]{after} : 0;
Branch: 1 branch
BranchTrueFalseTotal
@Timers ? : 35 16 51
58 51 $next_at = sprintf '%.6f', $next_at;
59  
60 51 ▶ if (!defined $dur) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dur) 23 28 51
61 23 ▶ $dur = $next_at > $Relative ? $next_at - $Relative : 0;
Branch: 1 branch
BranchTrueFalseTotal
$next_at > $Relative ? : 23 0 23
62 23 $dur = sprintf '%.6f', $dur;
63 }
64 51 ▶ croak "ff($dur): negative time not invented yet" if $dur < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dur < 0 0 51 51
65  
66 51 ▶ if ($next_at == 0 || $next_at > $Relative+$dur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_at == 0 or $next_at > $Relative + $dur) 28 23 51
Condition100%3/3
$next_at == 0 or $next_at > $Relative + $dur
l!l&&r!l&&!r
161223
MC/DC100%2/2
$next_at == 0 or $next_at > $Relative + $dur
$next_at == 0 $next_at > $Relative + $dur
Truth table100%3/3
A or B
A= $next_at == 0B= $next_at > $Relative + $dur
ABresult
1X1
011
000
67 28 $Relative += $dur;
68 28 $Relative = sprintf '%.6f', $Relative;
69 28 return;
70 }
71  
72 23 ▶ if ($next_at > $Relative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_at > $Relative) 23 0 23
73 23 $dur -= $next_at - $Relative;
74 23 $dur = sprintf '%.6f', $dur;
75 23 $Relative = $next_at;
76 }
77 23 my $cb = $Timers[0]{cb};
78 23 ▶ if ($Timers[0]{repeat} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Timers[0]{'repeat'} == 0) { } 7 16 23
79 7 ▶ if ($Timers[0]{watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Timers[0]{'watcher'}) { } 3 4 7
80 3 _stop_timer($Timers[0]{watcher});
81 }
82 else {
83 4 shift @Timers;
84 }
85 }
86 else {
87 16 $Timers[0]{after} = $Timers[0]{repeat};
88 16 $Timers[0]{start} = $Relative;
89 }
90 23 $cb->();
91 23 @_ = ($dur);
92 23 goto &ff;
93 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 {
96 8 my $next_id = 0;
97 20 ▶sub _add_timer {
Subroutine _add_timer: called CC=4 SCAR=14.0
98 20 my ($loop, $after, $repeat, $cb, $watcher) = @_;
99 20 my $id = sprintf 'fake_%05d', $next_id++;
100 20 ▶ push @Timers, {
Branch: 2 branches
BranchTrueFalseTotal
$after < 0 ? : 0 20 20
$repeat < 0 ? : 0 20 20
101 id => $id,
102 start => $Relative,
103 loop => $loop,
104 after => sprintf('%.6f', $after < 0 ? 0 : $after),
105 repeat => sprintf('%.6f', $repeat < 0 ? 0 : $repeat),
106 cb => $cb,
107 watcher => $watcher,
108 };
109 20 ▶ if ($watcher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watcher) 13 7 20
110 13 weaken($Timers[-1]{watcher});
111 }
112 20 return $id;
113 }
114 }
115  
116 8 ▶sub _start_timer {
Subroutine _start_timer: called CC=4 SCAR=14.1
117 2 my ($watcher) = @_;
118 2 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $watcher } @Timers_ns;
Condition66%2/3
$_->{'watcher'} && $_->{'watcher'} eq $watcher
!ll&&!rl&&r
022
MC/DC50%1/2
$_->{'watcher'} && $_->{'watcher'} eq $watcher
$_->{'watcher'} $_->{'watcher'} eq $watcher
Truth table66%2/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $watcher
ABresult
0X0
100
111
119 2 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 2 0 2
120 2 ▶ @Timers_ns = grep { !$_->{watcher} || $_->{watcher} ne $watcher } @Timers_ns;
Condition66%2/3
!$_->{'watcher'} || $_->{'watcher'} ne $watcher
l!l&&r!l&&!r
022
MC/DC50%1/2
!$_->{'watcher'} || $_->{'watcher'} ne $watcher
!$_->{'watcher'} $_->{'watcher'} ne $watcher
Truth table66%2/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $watcher
ABresult
1X1
011
000
121 2 push @Timers, $timer;
122 }
123 2 return;
124 }
125  
126 13 ▶sub _stop_timer {
Subroutine _stop_timer: called CC=4 SCAR=14.5
127 13 my ($watcher) = @_;
128 13 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $watcher } @Timers;
Condition33%1/3
$_->{'watcher'} && $_->{'watcher'} eq $watcher
!ll&&!rl&&r
006
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $watcher
$_->{'watcher'} $_->{'watcher'} eq $watcher
Truth table33%1/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $watcher
ABresult
0X0
100
111
129 13 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 6 7 13
130 6 ▶ @Timers = grep { !$_->{watcher} || $_->{watcher} ne $watcher } @Timers;
Condition33%1/3
!$_->{'watcher'} || $_->{'watcher'} ne $watcher
l!l&&r!l&&!r
006
MC/DC0%0/2
!$_->{'watcher'} || $_->{'watcher'} ne $watcher
!$_->{'watcher'} $_->{'watcher'} ne $watcher
Truth table33%1/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $watcher
ABresult
1X1
011
000
131 6 push @Timers_ns, $timer;
132 }
133 13 return;
134 }
135  
136 8 ▶sub _mock_core_global {
Subroutine _mock_core_global: called CC=1 SCAR=0.0
137 8 $Module{'CORE::GLOBAL'} = Test::MockModule->new('CORE::GLOBAL', no_auto=>1);
138 46 ▶ $Module{'CORE::GLOBAL'}->mock(time => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 46 return int($Absolute + $Relative);
140 8 });
141 12 ▶ $Module{'CORE::GLOBAL'}->mock(localtime => sub (;$) {
Subroutine __ANON__: called CC=2 SCAR=7.2
142 12 ▶ my $time = defined $_[0] ? $_[0] : int($Absolute + $Relative);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 12 12
143 12 return CORE::localtime($time);
144 8 });
145 13 ▶ $Module{'CORE::GLOBAL'}->mock(gmtime => sub (;$) {
Subroutine __ANON__: called CC=2 SCAR=6.9
146 13 ▶ my $time = defined $_[0] ? $_[0] : int($Absolute + $Relative);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 9 4 13
147 13 return CORE::gmtime($time);
148 8 });
149 16 ▶ $Module{'CORE::GLOBAL'}->mock(sleep => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 16 my $dur = int $_[0];
151 16 ▶ croak 'sleep with negative value is not supported' if $dur < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dur < 0 2 14 16
152 14 $Relative += $dur;
153 14 $Relative = sprintf '%.6f', $Relative;
154 14 return $dur;
155 8 });
156 8 return;
157 }
158  
159 8 ▶sub _mock_time_hires {
Subroutine _mock_time_hires: called CC=1 SCAR=0.0
160 # Do not improve precision of current actual time to simplify tests.
161 #$Absolute = Time::HiRes::time();
162 # Use current actual monotonic time.
163 8 $Monotonic = Time::HiRes::clock_gettime(CLOCK_MONOTONIC());
164  
165 8 $Module{'Time::HiRes'} = Test::MockModule->new('Time::HiRes');
166 8 ▶ $Module{'Time::HiRes'}->mock(time => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 8 return 0+sprintf '%.6f', $Absolute + $Relative;
168 8 });
169 4 ▶ $Module{'Time::HiRes'}->mock(gettimeofday => sub () {
Subroutine __ANON__: called CC=2 SCAR=6.9
170 4 my $t = sprintf '%.6f', $Absolute + $Relative;
171 4 ▶ return wantarray ? (map {0+$_} split qr/[.]/ms, $t) : 0+$t;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
172 8 });
173 51 ▶ $Module{'Time::HiRes'}->mock(clock_gettime => sub (;$) {
Subroutine __ANON__: called CC=3 SCAR=11.0
174 51 my ($which) = @_;
175 51 ▶ if ($which == CLOCK_REALTIME()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($which == CLOCK_REALTIME()) { } 1 50 51
elsif ($which == CLOCK_MONOTONIC()) { } 49 1 50
176 1 return 0+sprintf '%.6f', $Absolute + $Relative;
177 }
178 elsif ($which == CLOCK_MONOTONIC()) {
179 49 return 0+sprintf '%.6f', $Monotonic + $Relative;
180 }
181 1 return TIME_HIRES_CLOCK_NOT_SUPPORTED;
182 8 });
183 3 ▶ $Module{'Time::HiRes'}->mock(clock_getres => sub (;$) {
Subroutine __ANON__: called CC=3 SCAR=11.0
184 3 my ($which) = @_;
185 3 ▶ if ($which == CLOCK_REALTIME() || $which == CLOCK_MONOTONIC()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which == CLOCK_REALTIME() or $which == CLOCK_MONOTONIC()) 2 1 3
Condition100%3/3
$which == CLOCK_REALTIME() or $which == CLOCK_MONOTONIC()
l!l&&r!l&&!r
111
MC/DC100%2/2
$which == CLOCK_REALTIME() or $which == CLOCK_MONOTONIC()
$which == CLOCK_REALTIME() $which == CLOCK_MONOTONIC()
Truth table100%3/3
A or B
A= $which == CLOCK_REALTIME()B= $which == CLOCK_MONOTONIC()
ABresult
1X1
011
000
186 2 return $Module{'Time::HiRes'}->original('clock_getres')->(@_);
187 }
188 1 return TIME_HIRES_CLOCK_NOT_SUPPORTED;
189 8 });
190 3 ▶ $Module{'Time::HiRes'}->mock(sleep => sub (;@) {
Subroutine __ANON__: called CC=3 SCAR=11.0
191 3 my ($seconds) = @_;
192 3 ▶ croak 'sleep without arg is not supported' if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
193 2 ▶ croak "Time::HiRes::sleep($seconds): negative time not invented yet" if $seconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds < 0 1 1 2
194 1 $Relative += $seconds;
195 1 $Relative = sprintf '%.6f', $Relative;
196 1 return $seconds;
197 8 });
198 10 ▶ $Module{'Time::HiRes'}->mock(usleep => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=6.9
199 10 my ($useconds) = @_;
200 10 ▶ croak "Time::HiRes::usleep($useconds): negative time not invented yet" if $useconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $useconds < 0 1 9 10
201 9 $Relative += $useconds / MICROSECONDS;
202 9 $Relative = sprintf '%.6f', $Relative;
203 9 return $useconds;
204 8 });
205 2 ▶ $Module{'Time::HiRes'}->mock(nanosleep => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=6.9
206 2 my ($nanoseconds) = @_;
207 2 ▶ croak "Time::HiRes::nanosleep($nanoseconds): negative time not invented yet" if $nanoseconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nanoseconds < 0 1 1 2
208 1 $Relative += $nanoseconds / NANOSECONDS;
209 1 $Relative = sprintf '%.6f', $Relative;
210 1 return $nanoseconds;
211 8 });
212 5 ▶ $Module{'Time::HiRes'}->mock(clock_nanosleep => sub ($$;$) {
Subroutine __ANON__: called CC=5 SCAR=16.1
213 5 my ($which, $nanoseconds, $flags) = @_;
214 5 ▶ croak "Time::HiRes::clock_nanosleep(..., $nanoseconds): negative time not invented yet" if $nanoseconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nanoseconds < 0 1 4 5
215 4 ▶ croak 'only CLOCK_REALTIME and CLOCK_MONOTONIC are supported' if $which != CLOCK_REALTIME() && $which != CLOCK_MONOTONIC();
Branch: 1 branch
BranchTrueFalseTotal
if $which != CLOCK_REALTIME() and $which != CLOCK_MONOTONIC() 1 3 4
Condition100%3/3
$which != CLOCK_REALTIME() and $which != CLOCK_MONOTONIC()
!ll&&!rl&&r
121
MC/DC100%2/2
$which != CLOCK_REALTIME() and $which != CLOCK_MONOTONIC()
$which != CLOCK_REALTIME() $which != CLOCK_MONOTONIC()
Truth table100%3/3
A and B
A= $which != CLOCK_REALTIME()B= $which != CLOCK_MONOTONIC()
ABresult
0X0
100
111
216 3 ▶ croak 'only flags=0 is supported' if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 1 2 3
217 2 $Relative += $nanoseconds / NANOSECONDS;
218 2 $Relative = sprintf '%.6f', $Relative;
219 2 return $nanoseconds;
220 8 });
221 8 return;
222 }
223  
224 # TODO Distinguish timers set on different event loops / Mojo reactor
225 # objects while one_tick?
226  
227 8 ▶sub _mock_ev { ## no critic (ProhibitExcessComplexity)
Subroutine _mock_ev: called CC=1 SCAR=0.7
228 8 $Module{'EV'} = Test::MockModule->new('EV');
229 8 $Module{'EV::Watcher'} = Test::MockModule->new('EV::Watcher', no_auto=>1);
230 8 $Module{'EV::Timer'} = Test::MockModule->new('EV::Timer', no_auto=>1);
231 8 $Module{'EV::Periodic'} = Test::MockModule->new('EV::Periodic', no_auto=>1);
232 7 ▶ $Module{'EV'}->mock(time => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 7 return 0+sprintf '%.6f', $Absolute + $Relative;
234 8 });
235 21 ▶ $Module{'EV'}->mock(now => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
236 21 return 0+sprintf '%.6f', $Absolute + $Relative;
237 8 });
238 4 ▶ $Module{'EV'}->mock(sleep => sub ($) {
Subroutine __ANON__: called CC=2 SCAR=6.9
239 4 my ($seconds) = @_;
240 4 ▶ if ($seconds < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seconds < 0) 1 3 4
241 1 $seconds = 0;
242 }
243 4 $Relative += $seconds;
244 4 $Relative = sprintf '%.6f', $Relative;
245 4 return;
246 8 });
247 23 ▶ $Module{'EV'}->mock(run => sub (;$) {
Subroutine __ANON__: called CC=6 SCAR=18.7
248 23 my ($flags) = @_;
249 23 my $tick = 0;
250 23 my $w;
251 23 ▶ if (@Timers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Timers) 15 8 23
252 $w = $Module{'EV'}->original('timer')->(
253 23 ▶ $WAIT_ONE_TICK, $WAIT_ONE_TICK, sub {
Subroutine __ANON__: called CC=7 SCAR=20.7
254 23 my $me = shift;
255 23 my $k;
256 23 ▶ if (!$tick++ || !$flags) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tick++ and $flags) 15 8 23
Condition66%2/3
$tick++ and $flags
!ll&&!rl&&r
1508
MC/DC50%1/2
$tick++ and $flags
$tick++ $flags
Truth table66%2/3
A and B
A= $tick++B= $flags
ABresult
0X0
100
111
257 15 $k = $me->keepalive(0);
258 15 ff();
259 }
260 23 ▶ if (!@Timers) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @Timers) { } 7 16 23
elsif ($k and $flags || &any(sub { $_->{'watcher'} and $_->{'watcher'}->keepalive; } , @Timers)) { } 8 16 24
261 7 $me->stop;
262 }
263 0 ▶ elsif ($k && ($flags || any {$_->{watcher} && $_->{watcher}->keepalive} @Timers)) {
Subroutine __ANON__: not called CC=2 SCAR=11.6
Branch: 1 branch
BranchTrueFalseTotal
$_->{"watcher"} and $_->{"watcher"}->keepalive 0 0 0
Condition50%3/6
$k and $flags || &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
!ll&&!rl&&r
808
$flags or &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
l!l&&r!l&&!r
800
MC/DC33%1/3
$k and $flags || &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
$k $flags &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
Truth table50%2/4
A and B or C
A= $kB= $flagsC= &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
ABCresult
0XX0
1000
11X1
1011
264 8 $me->keepalive(1);
265 }
266 }
267 15 );
268 15 ▶ if (!($flags || any {$_->{watcher} && $_->{watcher}->keepalive} @Timers)) {
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 2 branches
BranchTrueFalseTotal
unless ($flags or &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)) 0 15 15
$_->{"watcher"} and $_->{"watcher"}->keepalive 2 0 2
Condition66%2/3
$flags or &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
l!l&&r!l&&!r
1320
MC/DC0%0/2
$flags or &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
$flags &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
Truth table66%2/3
A or B
A= $flagsB= &any(sub { $_->{"watcher"} and $_->{"watcher"}->keepalive; } , @Timers)
ABresult
1X1
011
000
269 0 $w->keepalive(0);
270 }
271 }
272 # $tick above and this second RUN_ONCE is work around bug in EV-4.10+
273 # http://lists.schmorp.de/pipermail/libev/2016q1/002656.html
274 # FIXME I believe this workaround isn't correct with EV-4.03 - calling
275 # RUN_ONCE twice must have side effect in processing two events
276 # (at least one of them must be a non-timer event) instead of one.
277 # To make it correct we probably need to mock all watcher types
278 # to intercept invoking their callbacks and thus make it possible
279 # to find out is first RUN_ONCE has actually called any callbacks.
280 23 ▶ if ($flags && $flags == EV::RUN_ONCE()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags and $flags == &EV::RUN_ONCE()) 18 5 23
Condition66%2/3
$flags and $flags == &EV::RUN_ONCE()
!ll&&!rl&&r
5018
MC/DC50%1/2
$flags and $flags == &EV::RUN_ONCE()
$flags $flags == &EV::RUN_ONCE()
Truth table66%2/3
A and B
A= $flagsB= $flags == &EV::RUN_ONCE()
ABresult
0X0
100
111
281 18 $Module{'EV'}->original('run')->(@_);
282 }
283 23 return $Module{'EV'}->original('run')->(@_);
284 8 });
285 11 ▶ $Module{'EV'}->mock(timer => sub ($$$) {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 11 my ($after, $repeat, $cb) = @_;
287 11 my $w = $Module{'EV'}->original('timer_ns')->(@_);
288 11 weaken(my $weakw = $w);
289 11 ▶ _add_timer('EV', $after, $repeat, sub { $weakw && $weakw->invoke(EV::TIMER()) }, $w);
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$weakw and $weakw->invoke(&EV::TIMER()) 13 0 13
290 11 return $w;
291 8 });
292 1 ▶ $Module{'EV'}->mock(timer_ns => sub ($$$) {
Subroutine __ANON__: called CC=1 SCAR=0.0
293 1 my ($after, $repeat, $cb) = @_;
294 1 my $w = EV::timer($after, $repeat, $cb);
295 1 _stop_timer($w);
296 1 return $w;
297 8 });
298 2 ▶ $Module{'EV'}->mock(periodic => sub ($$$$) {
Subroutine __ANON__: called CC=7 SCAR=19.9
299 2 my ($at, $repeat, $reschedule_cb, $cb) = @_;
300 2 ▶ croak 'reschedule_cb is not supported yet' if $reschedule_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $reschedule_cb 0 2 2
301 2 ▶ $at = sprintf '%.6f', $at < 0 ? 0 : $at;
Branch: 1 branch
BranchTrueFalseTotal
$at < 0 ? : 0 2 2
302 2 ▶ $repeat = sprintf '%.6f', $repeat < 0 ? 0 : $repeat;
Branch: 1 branch
BranchTrueFalseTotal
$repeat < 0 ? : 0 2 2
303 2 my $now = sprintf '%.6f', $Absolute + $Relative;
304 2 ▶ if ($repeat > 0 && $at < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repeat > 0 and $at < $now) 1 1 2
Condition66%2/3
$repeat > 0 and $at < $now
!ll&&!rl&&r
101
MC/DC50%1/2
$repeat > 0 and $at < $now
$repeat > 0 $at < $now
Truth table66%2/3
A and B
A= $repeat > 0B= $at < $now
ABresult
0X0
100
111
305 8 ▶ use bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 1 $at += $repeat * int(($now - $at) / $repeat + 1);
307 1 $at = sprintf '%.6f', $at;
308 }
309 2 ▶ my $after = $at > $now ? $at - $now : 0;
Branch: 1 branch
BranchTrueFalseTotal
$at > $now ? : 2 0 2
310 2 $after = sprintf '%.6f', $after;
311 2 my $w = $Module{'EV'}->original('periodic_ns')->(@_);
312 2 weaken(my $weakw = $w);
313 2 ▶ _add_timer('EV', $after, $repeat, sub { $weakw && $weakw->invoke(EV::TIMER()) }, $w);
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$weakw and $weakw->invoke(&EV::TIMER()) 2 0 2
314 2 return $w;
315 8 });
316 0 ▶ $Module{'EV'}->mock(periodic_ns => sub ($$$$) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
317 0 my ($at, $repeat, $reschedule_cb, $cb) = @_;
318 0 my $w = EV::periodic($at, $repeat, $reschedule_cb, $cb);
319 0 _stop_timer($w);
320 0 return $w;
321 8 });
322 2 ▶ $Module{'EV::Watcher'}->mock(is_active => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
323 2 my ($w) = @_;
324 2 ▶ my ($active) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers;
Condition66%2/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table66%2/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
325 2 ▶ my ($inactive) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers_ns;
Condition66%2/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table66%2/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
326 2 ▶ if ($active) {
Branch: 2 branches
BranchTrueFalseTotal
if ($active) { } 1 1 2
elsif ($inactive) { } 1 0 1
327 1 return 1;
328 }
329 elsif ($inactive) {
330 1 return;
331 }
332 0 return $Module{'EV::Watcher'}->original('is_active')->(@_);
333 8 });
334 26 ▶ $Module{'EV::Timer'}->mock(DESTROY => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
335 26 my ($w) = @_;
336 26 ▶ @Timers = grep { !$_->{watcher} || $_->{watcher} ne $w } @Timers;
Condition66%2/3
!$_->{'watcher'} || $_->{'watcher'} ne $w
l!l&&r!l&&!r
0108
MC/DC50%1/2
!$_->{'watcher'} || $_->{'watcher'} ne $w
!$_->{'watcher'} $_->{'watcher'} ne $w
Truth table66%2/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $w
ABresult
1X1
011
000
337 26 ▶ @Timers_ns = grep { !$_->{watcher} || $_->{watcher} ne $w } @Timers_ns;
Condition66%2/3
!$_->{'watcher'} || $_->{'watcher'} ne $w
l!l&&r!l&&!r
083
MC/DC50%1/2
!$_->{'watcher'} || $_->{'watcher'} ne $w
!$_->{'watcher'} $_->{'watcher'} ne $w
Truth table66%2/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $w
ABresult
1X1
011
000
338 26 return $Module{'EV::Timer'}->original('DESTROY')->(@_);
339 8 });
340 1 ▶ $Module{'EV::Timer'}->mock(start => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
341 1 return _start_timer(@_);
342 8 });
343 9 ▶ $Module{'EV::Timer'}->mock(stop => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
344 9 return _stop_timer(@_);
345 8 });
346 0 ▶ $Module{'EV::Timer'}->mock(set => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
347 0 my ($w, $after, $repeat) = @_;
348 0 ▶ if (!defined $repeat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $repeat) 0 0 0
349 0 $repeat = 0;
350 }
351 0 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers, @Timers_ns;
Condition0%0/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table0%0/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
352 0 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 0 0 0
353 0 $timer->{start} = $Relative;
354 0 ▶ $timer->{after} = sprintf '%.6f', $after < 0 ? 0 : $after;
Branch: 1 branch
BranchTrueFalseTotal
$after < 0 ? : 0 0 0
355 0 ▶ $timer->{repeat}= sprintf '%.6f', $repeat < 0 ? 0 : $repeat;
Branch: 1 branch
BranchTrueFalseTotal
$repeat < 0 ? : 0 0 0
356 }
357 0 return;
358 8 });
359 0 ▶ $Module{'EV::Timer'}->mock(remaining => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
360 0 my ($w) = @_;
361 0 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers, @Timers_ns;
Condition0%0/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table0%0/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
362 0 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 0 0 0
363 0 return 0+sprintf '%.6f', $timer->{start} + $timer->{after} - $Relative;
364 }
365 0 return;
366 8 });
367 3 ▶ $Module{'EV::Timer'}->mock(again => sub {
Subroutine __ANON__: called CC=11 SCAR=26.8
368 3 my ($w, $repeat) = @_;
369 3 ▶ if (defined $repeat && $repeat < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $repeat and $repeat < 0) 0 3 3
Condition33%1/3
defined $repeat and $repeat < 0
!ll&&!rl&&r
300
MC/DC0%0/2
defined $repeat and $repeat < 0
defined $repeat $repeat < 0
Truth table33%1/3
A and B
A= defined $repeatB= $repeat < 0
ABresult
0X0
100
111
370 0 $repeat = 0;
371 }
372 3 ▶ my ($active) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers;
Condition33%1/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
002
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table33%1/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
373 3 ▶ my ($inactive) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers_ns;
Condition66%2/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table66%2/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
374 3 ▶ if ($active) {
Branch: 2 branches
BranchTrueFalseTotal
if ($active) { } 2 1 3
elsif ($inactive) { } 1 0 1
375 2 ▶ $active->{repeat} = sprintf '%.6f', defined $repeat ? $repeat : $active->{repeat};
Branch: 1 branch
BranchTrueFalseTotal
defined $repeat ? : 0 2 2
376 2 ▶ if ($active->{repeat} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active->{'repeat'} > 0) { } 2 0 2
377 2 $active->{after} = $active->{repeat};
378 2 $active->{start} = $Relative;
379 }
380 else {
381 0 _stop_timer($active->{watcher});
382 }
383 }
384 elsif ($inactive) {
385 1 ▶ $inactive->{repeat} = sprintf '%.6f', defined $repeat ? $repeat : $inactive->{repeat};
Branch: 1 branch
BranchTrueFalseTotal
defined $repeat ? : 0 1 1
386 1 ▶ if ($inactive->{repeat} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inactive->{"repeat"} > 0) 1 0 1
387 1 $inactive->{after} = $inactive->{repeat};
388 1 $inactive->{start} = $Relative;
389 1 _start_timer($inactive->{watcher});
390 }
391 }
392 3 return;
393 8 });
394 2 ▶ $Module{'EV::Periodic'}->mock(DESTROY => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
395 2 my ($w) = @_;
396 2 ▶ @Timers = grep { !$_->{watcher} || $_->{watcher} ne $w } @Timers;
Condition33%1/3
!$_->{'watcher'} || $_->{'watcher'} ne $w
l!l&&r!l&&!r
001
MC/DC0%0/2
!$_->{'watcher'} || $_->{'watcher'} ne $w
!$_->{'watcher'} $_->{'watcher'} ne $w
Truth table33%1/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $w
ABresult
1X1
011
000
397 2 ▶ @Timers_ns = grep { !$_->{watcher} || $_->{watcher} ne $w } @Timers_ns;
Condition66%2/3
!$_->{'watcher'} || $_->{'watcher'} ne $w
l!l&&r!l&&!r
011
MC/DC50%1/2
!$_->{'watcher'} || $_->{'watcher'} ne $w
!$_->{'watcher'} $_->{'watcher'} ne $w
Truth table66%2/3
not(A) || B
A= !$_->{'watcher'}B= $_->{'watcher'} ne $w
ABresult
1X1
011
000
398 2 return $Module{'EV::Periodic'}->original('DESTROY')->(@_);
399 8 });
400 0 ▶ $Module{'EV::Periodic'}->mock(start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 return _start_timer(@_);
402 8 });
403 0 ▶ $Module{'EV::Periodic'}->mock(stop => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
404 0 return _stop_timer(@_);
405 8 });
406 0 ▶ $Module{'EV::Periodic'}->mock(set => sub {
Subroutine __ANON__: not called CC=9 SCAR=42.8
407 0 my ($w, $at, $repeat, $reschedule_cb, $cb) = @_;
408 0 ▶ croak 'reschedule_cb is not supported yet' if $reschedule_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $reschedule_cb 0 0 0
409 0 ▶ $at = sprintf '%.6f', $at < 0 ? 0 : $at;
Branch: 1 branch
BranchTrueFalseTotal
$at < 0 ? : 0 0 0
410 0 ▶ $repeat = sprintf '%.6f', $repeat < 0 ? 0 : $repeat;
Branch: 1 branch
BranchTrueFalseTotal
$repeat < 0 ? : 0 0 0
411 0 my $now = sprintf '%.6f', $Absolute + $Relative;
412 0 ▶ if ($repeat > 0 && $at < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repeat > 0 and $at < $now) 0 0 0
Condition0%0/3
$repeat > 0 and $at < $now
!ll&&!rl&&r
000
MC/DC0%0/2
$repeat > 0 and $at < $now
$repeat > 0 $at < $now
Truth table0%0/3
A and B
A= $repeat > 0B= $at < $now
ABresult
0X0
100
111
413 8 ▶ use bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 0 $at += $repeat * int(($now - $at) / $repeat + 1);
415 0 $at = sprintf '%.6f', $at;
416 }
417 0 ▶ my $after = $at > $now ? $at - $now : 0;
Branch: 1 branch
BranchTrueFalseTotal
$at > $now ? : 0 0 0
418 0 $after = sprintf '%.6f', $after;
419 0 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers, @Timers_ns;
Condition0%0/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table0%0/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
420 0 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 0 0 0
421 0 $timer->{start} = $Relative;
422 0 $timer->{after} = $after;
423 0 $timer->{repeat}= $repeat;
424 }
425 0 return;
426 8 });
427 0 ▶ $Module{'EV::Periodic'}->mock(again => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
428 0 return _start_timer(@_);
429 8 });
430 0 ▶ $Module{'EV::Periodic'}->mock(at => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
431 0 my ($w) = @_;
432 0 ▶ my ($timer) = grep { $_->{watcher} && $_->{watcher} eq $w } @Timers, @Timers_ns;
Condition0%0/3
$_->{'watcher'} && $_->{'watcher'} eq $w
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'watcher'} && $_->{'watcher'} eq $w
$_->{'watcher'} $_->{'watcher'} eq $w
Truth table0%0/3
A && B
A= $_->{'watcher'}B= $_->{'watcher'} eq $w
ABresult
0X0
100
111
433 0 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 0 0 0
434 0 return 0+sprintf '%.6f', $timer->{start} + $timer->{after};
435 }
436 0 return;
437 8 });
438 8 return;
439 }
440  
441 8 ▶sub _mock_mojolicious {
Subroutine _mock_mojolicious: called CC=1 SCAR=0.0
442 8 $Module{'Mojo::Reactor::Poll'} = Test::MockModule->new('Mojo::Reactor::Poll');
443 10 ▶ $Module{'Mojo::Reactor::Poll'}->mock(one_tick => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
444 10 my ($self) = @_;
445 10 ▶ if (!@Timers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Timers) 2 8 10
446 2 return $Module{'Mojo::Reactor::Poll'}->original('one_tick')->(@_);
447 }
448 my $id = $Module{'Mojo::Reactor::Poll'}->original('timer')->(
449 8 ▶ $self, $WAIT_ONE_TICK, sub { ff() }
Subroutine __ANON__: called CC=1 SCAR=0.0
450 8 );
451 8 $Module{'Mojo::Reactor::Poll'}->original('one_tick')->(@_);
452 8 $Module{'Mojo::Reactor::Poll'}->original('remove')->($self, $id);
453 8 return;
454 8 });
455 5 ▶ $Module{'Mojo::Reactor::Poll'}->mock(timer => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
456 5 my ($self, $delay, $cb) = @_;
457 5 ▶ if ($delay == 0) { # do not fake timer for 0 seconds to avoid hang
Branch: 1 branch
BranchTrueFalseTotal
if ($delay == 0) 0 5 5
458 0 return $Module{'Mojo::Reactor::Poll'}->original('timer')->(@_);
459 }
460 5 ▶ return _add_timer($self, $delay, 0, sub { $cb->($self) });
Subroutine __ANON__: called CC=1 SCAR=0.0
461 8 });
462 2 ▶ $Module{'Mojo::Reactor::Poll'}->mock(recurring => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
463 2 my ($self, $delay, $cb) = @_;
464 2 ▶ return _add_timer($self, $delay, $delay, sub { $cb->($self) });
Subroutine __ANON__: called CC=1 SCAR=0.0
465 8 });
466 2 ▶ $Module{'Mojo::Reactor::Poll'}->mock(again => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
467 2 my ($self, $id) = @_;
468 2 ▶ if ($id !~ /\Afake_\d+\z/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $id =~ /\Afake_\d+\z/ms) { } 0 2 2
469 0 $Module{'Mojo::Reactor::Poll'}->original('again')->(@_);
470 }
471 else {
472 2 my ($timer) = grep { $_->{id} eq $id } @Timers;
473 2 ▶ if ($timer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer) 2 0 2
474 2 $timer->{start} = $Relative;
475 }
476 }
477 2 return;
478 8 });
479 12 ▶ $Module{'Mojo::Reactor::Poll'}->mock(remove => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
480 12 my ($self, $id) = @_;
481 12 ▶ if ($id !~ /\Afake_\d+\z/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $id =~ /\Afake_\d+\z/ms) { } 10 2 12
482 10 $Module{'Mojo::Reactor::Poll'}->original('remove')->(@_);
483 }
484 else {
485 2 ▶ @Timers = grep { $_->{loop} ne $self || $_->{id} ne $id } @Timers;
Condition33%1/3
$_->{'loop'} ne $self || $_->{'id'} ne $id
l!l&&r!l&&!r
002
MC/DC0%0/2
$_->{'loop'} ne $self || $_->{'id'} ne $id
$_->{'loop'} ne $self $_->{'id'} ne $id
Truth table33%1/3
A || B
A= $_->{'loop'} ne $selfB= $_->{'id'} ne $id
ABresult
1X1
011
000
486 }
487 12 return;
488 8 });
489 2 ▶ $Module{'Mojo::Reactor::Poll'}->mock(reset => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
490 2 my ($self) = @_;
491 2 @Timers = grep { $_->{loop} ne $self } @Timers;
492 2 return $Module{'Mojo::Reactor::Poll'}->original('reset')->(@_);
493 8 });
494 8 return;
495 }
496  
497  
498 8 1;
499 __END__
↑ Summary