Out.pm

filter: Statementstmt 100% 59 / 59 Branchbran 95.4% 21 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 99.0% 104 / 105 CC 15 SCAR 14.2
CC
15
Cov
99%
timeout
8.0
__ANON__
5.0
BEGIN
3.1
CRAP 4.1
« Out/ParamConstraints.pm ↑ Summary Out/Exception.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Time::Out;
5  
6 # keeping the following $VERSION declaration on a single line is important
7 #<<<
8 8 ▶use version 0.9915; our $VERSION = version->declare( '1.0.0' );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #>>>
10  
11 8 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Time::Out::Exception qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Time::Out::ParamConstraints qw( assert_NonNegativeNumber assert_CodeRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Try::Tiny qw( finally try );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.4
18 # if possible use Time::HiRes drop-in replacements
19 8 for ( qw( alarm time ) ) {
20 16 ▶ Time::HiRes->import( $_ ) if Time::HiRes->can( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if "Time::HiRes"->can($_) 16 0 16
21 }
22 }
23  
24 8 our @EXPORT_OK = qw( timeout );
25  
26 20 ▶sub timeout( $@ ) {
Subroutine timeout: called CC=8 SCAR=20.8
Pod documented
27 20 my $context = wantarray;
28 # wallclock seconds
29 20 my $timeout = assert_NonNegativeNumber shift;
30 20 my $code = assert_CodeRef pop;
31 20 my @code_args = @_;
32  
33 20 my $exception;
34 # in scalar context store the result in the first array element
35 my @result;
36  
37 # disable previous timer and save the amount of time remaining on it
38 20 my $remaining_time_on_previous_timer = alarm 0;
39 20 my $start_time = time;
40  
41 {
42 # https://stackoverflow.com/questions/1194113/whats-the-difference-between-ignoring-a-signal-and-telling-it-to-do-nothing-in
43 # disable ALRM handling to prevent possible race condition between the end of the
44 # try block and the execution of alarm(0) in the finally block
45 20 local $SIG{ ALRM } = 'IGNORE';
46 20 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.1
47 20 ▶ local $SIG{ ALRM } = sub { die Time::Out::Exception->new( previous_exception => $@, timeout => $timeout ) }; ## no critic (RequireCarping)
Subroutine __ANON__: called CC=1 SCAR=0.0
48 20 ▶ if ( $remaining_time_on_previous_timer and $remaining_time_on_previous_timer < $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_time_on_previous_timer and $remaining_time_on_previous_timer < $timeout) { } 1 19 20
Condition100%3/3
$remaining_time_on_previous_timer and $remaining_time_on_previous_timer < $timeout
!ll&&!rl&&r
1811
MC/DC100%2/2
$remaining_time_on_previous_timer and $remaining_time_on_previous_timer < $timeout
$remaining_time_on_previous_timer $remaining_time_on_previous_timer < $timeout
Truth table100%3/3
A and B
A= $remaining_time_on_previous_timerB= $remaining_time_on_previous_timer < $timeout
ABresult
0X0
100
111
49 # a shorter timer was pending, let's use it instead
50 1 alarm $remaining_time_on_previous_timer;
51 } else {
52 19 alarm $timeout;
53 }
54 20 ▶ defined $context
Branch: 2 branches
BranchTrueFalseTotal
defined $context ? : 5 15 20
$context ? : 1 4 5
55 ? $context
56 ? @result = $code->( @code_args ) # list context
57 : $result[ 0 ] = $code->( @code_args ) # scalar context
58 : $code->( @code_args ); # void context
59 6 alarm 0;
60 20 ▶ } finally {
Subroutine __ANON__: called CC=2 SCAR=6.9
61 20 alarm 0;
62 20 ▶ $exception = $_[ 0 ] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 14 6 20
63 }
64 20 }
65  
66 20 my $elapsed_time = time - $start_time;
67 20 my $new_timeout = $remaining_time_on_previous_timer - $elapsed_time;
68  
69 20 ▶ if ( $new_timeout > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_timeout > 0) { } 1 19 20
elsif ($remaining_time_on_previous_timer) { } 1 18 19
70 # rearm previous timer with new timeout
71 1 alarm $new_timeout;
72 } elsif ( $remaining_time_on_previous_timer ) {
73 # previous timer has already expired; send ALRM
74 1 kill 'ALRM', $$;
75 }
76  
77 # handle exceptions
78 19 ▶ if ( defined $exception ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exception) 13 6 19
79 13 ▶ if ( defined blessed( $exception ) and $exception->isa( 'Time::Out::Exception' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($exception) and $exception->isa("Time::Out::Exception")) 8 5 13
Condition100%3/3
defined builtin::blessed($exception) and $exception->isa("Time::Out::Exception")
!ll&&!rl&&r
418
MC/DC100%2/2
defined builtin::blessed($exception) and $exception->isa("Time::Out::Exception")
defined builtin::blessed($exception) $exception->isa("Time::Out::Exception")
Truth table100%3/3
A and B
A= defined builtin::blessed($exception)B= $exception->isa("Time::Out::Exception")
ABresult
0X0
100
111
80 8 $@ = $exception; ## no critic (RequireLocalizedPunctuationVars)
81 8 return;
82 }
83 5 die $exception; ## no critic (RequireCarping)
84 }
85  
86 return
87 6 ▶ defined $context
Branch: 2 branches
BranchTrueFalseTotal
defined $context ? : 5 1 6
$context ? : 1 4 5
88 ? $context
89 ? return @result # list context
90 : $result[ 0 ] # scalar context
91 : (); # void context
92 }
93  
94 8 1;
« Out/ParamConstraints.pm ↑ Summary Out/Exception.pm »