blib/lib/WWW/Mechanize/Plugin/Retry.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 100% 8 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 5 / 5 totaltotal 93.8% 91 / 97 CC 8 SCAR 6.3
CC
8
Cov
96%
posthook
4.0
init
2.0
retry_if
2.0
CRAP 1.9
↑ Summary
1 package WWW::Mechanize::Plugin::Retry;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 __PACKAGE__->mk_accessors(qw(retry_failed _retry_check_sub
7 _method_to_retry
8 _delays _delay_index));
9  
10 5 our $VERSION = '0.04';
11  
12 3 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod documented
13 3 my($class, $pluggable) = @_;
14 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 local $_;
16 eval "*WWW::Mechanize::Pluggable::$_ = \\&$_"
17 3 for qw(retry retry_if _method_to_retry _retry_fib
18 _retry_check_sub _delays _delays_max _delay_index retry_failed);
19  
20 3 ▶ $pluggable->pre_hook('get', sub { prehook(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 0 ▶ $pluggable->pre_hook('submit_form', sub { prehook(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 3 ▶ $pluggable->post_hook('get', sub { posthook(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23 0 ▶ $pluggable->post_hook('submit_form', sub { posthook(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 }
25  
26 7 ▶sub retry_if {
Subroutine retry_if: called CC=2 SCAR=6.9
Pod documented
27 7 my($self, $sub, $times) = @_;
28  
29 7 ▶ if (defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub) { } 6 1 7
30 6 $self->_retry_check_sub($sub);
31 6 $self->_delays($times);
32 6 $self->_delay_index(0);
33 6 $self->retry_failed(0);
34 }
35 else {
36 1 $sub;
37 }
38 }
39  
40 1 ▶sub retry {
Subroutine retry: called CC=1 SCAR=0.0
Pod documented
41 1 my($self, $times) = @_;
42 1 ▶ $self->retry_if(sub {$self->success}, $times);
Subroutine __ANON__: called CC=1 SCAR=0.0
43 }
44  
45 9 ▶sub prehook {
Subroutine prehook: called CC=1 SCAR=0.0
Pod documented
46 9 my($pluggable, $mech, @args) = @_;
47 9 $pluggable->_method_to_retry($pluggable->last_method);
48
49 # Don't skip the actual method call.
50 9 0;
51 }
52  
53 9 ▶sub posthook {
Subroutine posthook: called CC=4 SCAR=13.9
Pod documented
54 9 my($pluggable, $mech, @args) = @_;
55  
56 # just leave if we have no retry check, or the check passes.
57 9 my $sub = $pluggable->_retry_check_sub;
58 9 ▶ if (!defined($sub) or $sub->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sub or &$sub()) 3 6 9
Condition66%2/3
not defined $sub or &$sub()
l!l&&r!l&&!r
036
MC/DC50%1/2
not defined $sub or &$sub()
not defined $sub &$sub()
Truth table66%2/3
not(A) or B
A= not defined $subB= &$sub()
ABresult
1X1
011
000
59 # Ensure that the delay works next time round, and
60 # note that we did not fail retry.
61 3 $pluggable->_delay_index(-1);
62 3 $pluggable->retry_failed(0);
63 3 return;
64 }
65  
66 # Retry needed (check failed). Are we out of delays?
67 6 my $delay_index = $pluggable->_delay_index;
68 6 ▶ if ($delay_index == $pluggable->_delays) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay_index == $pluggable->_delays) { } 1 5 6
69 # Ran out this time.
70 1 $pluggable->_delay_index(-1);
71 1 $pluggable->retry_failed(1);
72 }
73 else {
74 5 my $current_delay = _retry_fib($delay_index);
75 5 $pluggable->_delay_index($pluggable->_delay_index+1);
76 5 sleep $current_delay;
77 5 my $method = $pluggable->_method_to_retry();
78 5 eval "\$pluggable->$method->(\@args)";
79 }
80 }
81  
82 # initial values in Fibonacci sequence
83 5 my @fib_for = (1,1);
84  
85 # Extend and cache as needed
86 1 ▶sub _retry_fib_for {
Subroutine _retry_fib_for: called CC=2 SCAR=6.9
87 1 my($n) = @_;
88 # walk up cache from last known value, applying F(n) = F(n-1)+F(n-2)
89 1 for my $i (@fib_for..$n) {
90 1 $fib_for[$i] = $fib_for[$i-1]+$fib_for[$i-2];
91 }
92 1 return;
93 }
94  
95 # Fibonacci # N
96 5 ▶sub _retry_fib {
Subroutine _retry_fib: called CC=2 SCAR=6.9
97 5 my($n) = @_;
98 5 ▶ if (!defined $fib_for[$n]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fib_for[$n]) 1 4 5
99 1 _retry_fib_for($n);
100 }
101 5 return $fib_for[$n];
102 }
103  
104  
105 5 1; # End of WWW::Mechanize::Plugin::Retry
106 __END__
↑ Summary