blib/lib/Try/ALRM.pm

filter: Statementstmt 96.0% 120 / 125 Branchbran 86.9% 40 / 46 Conditioncond 77.4% 24 / 31 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 100% 20 / 20 Podpod 100% 6 / 6 totaltotal 89.3% 219 / 245 CC 36 SCAR 19.4
CC
36
Cov
91%
retry
14.1
_parse_retry_args
11.0
_dispatch_alarm
3.2
CRAP 6.9
↑ Summary
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Try::ALRM;
5  
6 # ABSTRACT: Structured retry and timeout handling using CORE::alarm
7  
8 7 our $VERSION = q{1.06};
9  
10 7 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our @EXPORT = qw(try_once retry ALRM finally timeout tries);
15 7 our @EXPORT_OK = qw(try_once retry ALRM finally timeout tries);
16  
17 7 our $TIMEOUT = 60;
18 7 our $TRIES = 3;
19  
20 7 my @ALARM_STACK;
21  
22 59 ▶sub timeout (;$) {
Subroutine timeout: called CC=2 SCAR=6.9
Pod documented
23 59 my $timeout = shift;
24 59 ▶ if ( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 5 54 59
25 5 _assert_timeout($timeout);
26 5 $TIMEOUT = $timeout;
27 }
28 59 return $TIMEOUT;
29 }
30  
31 51 ▶sub tries (;$) {
Subroutine tries: called CC=2 SCAR=6.9
Pod documented
32 51 my $tries = shift;
33 51 ▶ if ( defined $tries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tries) 1 50 51
34 1 _assert_tries($tries);
35 1 $TRIES = $tries;
36 }
37 51 return $TRIES;
38 }
39  
40 8 ▶sub try_once (&;@) {
Subroutine try_once: called CC=1 SCAR=0.0
Pod documented
41 8 my $block = shift;
42 8 &retry( $block, @_, tries => 1 ); # bypass prototype intentionally
43 }
44  
45 34 ▶sub retry (&;@) {
Subroutine retry: called CC=14 SCAR=26.4
Pod documented
46 34 my $block = shift;
47  
48 34 my $spec = _parse_retry_args(@_);
49  
50 32 my $retry_block = $block;
51 32 my $alarm_block = $spec->{ALRM};
52 32 ▶ my $finally_block = $spec->{finally} || sub { };
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$spec->{'finally'} || sub { }
l!l&&r!l&&!r
20120
MC/DC0%0/2
$spec->{'finally'} || sub { }
$spec->{'finally'} sub { }
Truth table66%2/3
A || B
A= $spec->{'finally'}B= sub { }
ABresult
1X1
011
000
53  
54 32 ▶ my $timeout = exists $spec->{timeout} ? $spec->{timeout} : $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'timeout'} ? : 27 5 32
55 32 ▶ my $tries = exists $spec->{tries} ? $spec->{tries} : $TRIES;
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'tries'} ? : 30 2 32
56  
57 32 _assert_timeout($timeout);
58 30 _assert_tries($tries);
59  
60 28 local $TIMEOUT = $timeout;
61 28 local $TRIES = $tries;
62  
63 28 my $attempts = 0;
64 28 my $succeeded = 0;
65 28 my $error;
66  
67 ATTEMPT:
68 28 for my $attempt ( 1 .. $tries ) {
69 34 $attempts = $attempt;
70  
71 34 my $alarm_token = bless \( my $token = "Try::ALRM timeout" ),
72 'Try::ALRM::_Timeout';
73  
74 34 local $SIG{ALRM} = \&_dispatch_alarm;
75  
76 34 my $frame = _push_alarm_frame(
77 timeout => $timeout,
78 attempt => $attempt,
79 handler => $alarm_block,
80 token => $alarm_token,
81 );
82  
83 34 my $ok = eval {
84 34 $retry_block->($attempt);
85 10 _pop_alarm_frame($frame);
86 10 1;
87 };
88  
89 34 my $eval_error = $@;
90  
91 34 _pop_alarm_frame($frame);
92  
93 34 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 10 24 34
94 10 $succeeded = 1;
95 10 last ATTEMPT;
96 }
97  
98 24 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $eval_error and ref $eval_error eq "Try::ALRM::_Timeout" and builtin::refaddr($eval_error) == builtin::refaddr($alarm_token)) 20 4 24
Condition66%4/6
ref $eval_error and ref $eval_error eq "Try::ALRM::_Timeout" and builtin::refaddr($eval_error) == builtin::refaddr($alarm_token)
!ll&&!rl&&r
4020
ref $eval_error and ref $eval_error eq "Try::ALRM::_Timeout"
!ll&&!rl&&r
4020
MC/DC33%1/3
ref $eval_error and ref $eval_error eq "Try::ALRM::_Timeout" and builtin::refaddr($eval_error) == builtin::refaddr($alarm_token)
ref $eval_error ref $eval_error eq "Try::ALRM::_Timeout" builtin::refaddr($eval_error) == builtin::refaddr($alarm_token)
Truth table50%2/4
A and B and C
A= ref $eval_errorB= ref $eval_error eq "Try::ALRM::_Timeout"C= builtin::refaddr($eval_error) == builtin::refaddr($alarm_token)
ABCresult
0XX0
10X0
1100
1111
99 ref($eval_error)
100 && ref($eval_error) eq 'Try::ALRM::_Timeout'
101 && refaddr($eval_error) == refaddr($alarm_token)
102 ) {
103 20 next ATTEMPT;
104 }
105  
106 4 ▶ $error = $eval_error || 'Unknown error';
Condition50%1/2
$eval_error || 'Unknown error'
l!l
40
MC/DC0%0/1
$eval_error || 'Unknown error'
$eval_error
Truth table50%1/2
A
A= $eval_error
Aresult
11
00
107 4 last ATTEMPT;
108 }
109  
110 28 my $finally_error;
111 eval {
112 28 $finally_block->( $attempts, $succeeded );
113 26 1;
114 28 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$finally_block($attempts, $succeeded); 1 } } or do { $finally_error = $@ || "Unknown error" } 26 2 28
115 2 ▶ $finally_error = $@ || 'Unknown error';
Condition50%1/2
$@ || 'Unknown error'
l!l
20
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
116 };
117  
118 28 ▶ die $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 4 24 28
119 24 ▶ die $finally_error if defined $finally_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $finally_error 1 23 24
120  
121 23 return;
122 }
123  
124 11 ▶sub ALRM (&;@) {
Subroutine ALRM: called CC=1 SCAR=0.0
Pod documented
125 11 return ALRM => @_;
126 }
127  
128 20 ▶sub finally (&;@) {
Subroutine finally: called CC=1 SCAR=0.0
Pod documented
129 20 return finally => @_;
130 }
131  
132 34 ▶sub _push_alarm_frame {
Subroutine _push_alarm_frame: called CC=1 SCAR=0.0
133 34 my %frame = @_;
134  
135 34 $frame{deadline} = time() + $frame{timeout};
136  
137 34 push @ALARM_STACK, \%frame;
138 34 _reset_alarm();
139  
140 34 return \%frame;
141 }
142  
143 44 ▶sub _pop_alarm_frame {
Subroutine _pop_alarm_frame: called CC=2 SCAR=7.0
144 44 my $frame = shift;
145  
146 44 ▶ return unless $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame 0 44 44
147  
148 @ALARM_STACK = grep {
149 44 refaddr($_) != refaddr($frame)
150 } @ALARM_STACK;
151  
152 44 _reset_alarm();
153  
154 44 return;
155 }
156  
157 20 ▶sub _dispatch_alarm {
Subroutine _dispatch_alarm: called CC=3 SCAR=11.5
158 20 my $now = time();
159  
160 my @expired = sort {
161 $a->{deadline} <=> $b->{deadline}
162 0 } grep {
163 20 $_->{deadline} <= $now
164 } @ALARM_STACK;
165  
166 20 ▶ unless (@expired) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@expired) 0 20 20
167 0 _reset_alarm();
168 0 return;
169 }
170  
171 20 my $frame = $expired[0];
172  
173 20 ▶ if ( ref( $frame->{handler} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $frame->{"handler"} eq "CODE") 14 6 20
174 14 $frame->{handler}->( $frame->{attempt} );
175 }
176  
177 20 die $frame->{token};
178 }
179  
180 78 ▶sub _reset_alarm {
Subroutine _reset_alarm: called CC=3 SCAR=11.3
181 78 CORE::alarm(0);
182  
183 78 ▶ return unless @ALARM_STACK;
Branch: 1 branch
BranchTrueFalseTotal
unless @ALARM_STACK 41 37 78
184  
185 my ($next) = sort {
186 37 $a->{deadline} <=> $b->{deadline}
187 2 } @ALARM_STACK;
188  
189 37 my $remaining = $next->{deadline} - time();
190  
191 37 ▶ if ( $remaining <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= 0) 0 37 37
192 0 CORE::alarm(1);
193 0 return;
194 }
195  
196 37 CORE::alarm( _ceil($remaining) );
197  
198 37 return;
199 }
200  
201 37 ▶sub _ceil {
Subroutine _ceil: called CC=2 SCAR=7.2
202 37 my $value = shift;
203  
204 37 ▶ return int($value) == $value ? int($value) : int($value) + 1;
Branch: 1 branch
BranchTrueFalseTotal
int $value == $value ? : 0 37 37
205 }
206  
207 34 ▶sub _parse_retry_args {
Subroutine _parse_retry_args: called CC=11 SCAR=24.0
208 34 my @args = @_;
209  
210 34 ▶ die "Odd number of arguments to retry\n" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 34 34
211  
212 34 my %spec;
213  
214 34 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 91 32 123
215 91 my ( $key, $value ) = splice @args, 0, 2;
216  
217 91 ▶ die "Unknown retry argument '$key'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq "ALRM" or $key eq "finally" or $key eq "timeout" or $key eq "tries" 1 90 91
Condition100%9/9
$key eq "ALRM" or $key eq "finally" or $key eq "timeout" or $key eq "tries"
l!l&&r!l&&!r
60301
$key eq "ALRM" or $key eq "finally" or $key eq "timeout"
l!l&&r!l&&!r
312931
$key eq "ALRM" or $key eq "finally"
l!l&&r!l&&!r
112060
MC/DC100%4/4
$key eq "ALRM" or $key eq "finally" or $key eq "timeout" or $key eq "tries"
$key eq "ALRM" $key eq "finally" $key eq "timeout" $key eq "tries"
Truth table100%5/5
A or B or C or D
A= $key eq "ALRM"B= $key eq "finally"C= $key eq "timeout"D= $key eq "tries"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
218 unless $key eq 'ALRM'
219 || $key eq 'finally'
220 || $key eq 'timeout'
221 || $key eq 'tries';
222  
223 die "Duplicate retry argument '$key'\n"
224 90 ▶ if exists $spec{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec{$key} 1 89 90
225  
226 89 ▶ if ( $key eq 'ALRM' || $key eq 'finally' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ALRM" or $key eq "finally") 31 58 89
Condition100%3/3
$key eq "ALRM" or $key eq "finally"
l!l&&r!l&&!r
112058
MC/DC100%2/2
$key eq "ALRM" or $key eq "finally"
$key eq "ALRM" $key eq "finally"
Truth table100%3/3
A or B
A= $key eq "ALRM"B= $key eq "finally"
ABresult
1X1
011
000
227 31 ▶ die "$key must be a CODE reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 31 31
228 unless ref($value) eq 'CODE';
229 }
230  
231 89 $spec{$key} = $value;
232 }
233  
234 32 return \%spec;
235 }
236  
237 37 ▶sub _assert_timeout {
Subroutine _assert_timeout: called CC=3 SCAR=11.1
238 37 my $timeout = shift;
239  
240 37 ▶ die qq{timeout must be an integer >= 1!\n}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout and $timeout =~ /\A[1-9][0-9]*\z/ 2 35 37
Condition66%2/3
defined $timeout and $timeout =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
0235
MC/DC50%1/2
defined $timeout and $timeout =~ /\A[1-9][0-9]*\z/
defined $timeout $timeout =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $timeoutB= $timeout =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
241 unless defined $timeout
242 && $timeout =~ /\A[1-9][0-9]*\z/;
243 }
244  
245 31 ▶sub _assert_tries {
Subroutine _assert_tries: called CC=3 SCAR=11.1
246 31 my $tries = shift;
247  
248 31 ▶ die qq{tries must be an integer >= 1!\n}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tries and $tries =~ /\A[1-9][0-9]*\z/ 2 29 31
Condition66%2/3
defined $tries and $tries =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
0229
MC/DC50%1/2
defined $tries and $tries =~ /\A[1-9][0-9]*\z/
defined $tries $tries =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $triesB= $tries =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
249 unless defined $tries
250 && $tries =~ /\A[1-9][0-9]*\z/;
251 }
252  
253 7 __PACKAGE__;
254  
255 __END__
↑ Summary