BackOff/Random.pm

filter: Statementstmt 80.0% 24 / 30 Branchbran 50.0% 5 / 10 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 57.7% 41 / 71 CC 11 SCAR 26.9
CC
11
Cov
62%
new
23.1
calculate_back_off
3.1
BEGIN
1.0
CRAP 14.7
« BackOff/Linear.pm ↑ Summary BackOff/Exponential.pm »
1 package Proc::BackOff::Random;
2  
3 # Inheritance
4 2 ▶use base qw( Proc::BackOff );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Set up get/set fields
7 # 2 ^ 5
8 # 2 is the base
9 # 5 is the exponent
10  
11 2 __PACKAGE__->mk_accessors( 'min',
12 'max',
13 );
14  
15 # standard pragmas
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # standard perl modules
20  
21 # CPAN & others
22  
23 2 our $VERSION = '0.02';
24  
25 =head1 NAME
26  
27 Proc::BackOff::Random
28  
29 =head1 SYNOPSIS
30  
31 Usage:
32  
33 use Proc::BackOff::Random;
34  
35 my $obj = Proc::BackOff::Random->new( { min => 5 , max => 100 } );
36 # On N'th failure delay would be set to:
37 # 1st failure : a random number between 5 and 100 inclusive.
38 # (5 is a possible value)
39 # 2nd failure : a random number between 5 and 100 inclusive.
40 # 3rd failure : a random number between 5 and 100 inclusive.
41  
42 See L<Proc::BackOff> for further documentation.
43  
44 =head1 Overloaded Methods
45  
46 =head2 new()
47  
48 Check for variables being set: min & max. If they are not set, you will get a
49 warning and undef will be returned.
50  
51 =cut
52  
53 1 ▶sub new {
Subroutine new: called CC=9 SCAR=31.4
Pod documented
54 1 my $proto = shift;
55 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
56 1 my $obj = $class->SUPER::new(@_);
57  
58 1 ▶ if ( ! defined $obj->min() || ! $obj->valid_number_check($obj->min())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->min and $obj->valid_number_check($obj->min)) 0 1 1
Condition33%1/3
defined $obj->min and $obj->valid_number_check($obj->min)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $obj->min and $obj->valid_number_check($obj->min)
defined $obj->min $obj->valid_number_check($obj->min)
Truth table33%1/3
A and B
A= defined $obj->minB= $obj->valid_number_check($obj->min)
ABresult
0X0
100
111
59 0 warn "$proto: Minimum value not set";
60 0 return undef;
61 }
62  
63 1 ▶ if ( ! defined $obj->max() || ! $obj->valid_number_check($obj->max())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->max and $obj->valid_number_check($obj->max)) 0 1 1
Condition33%1/3
defined $obj->max and $obj->valid_number_check($obj->max)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $obj->max and $obj->valid_number_check($obj->max)
defined $obj->max $obj->valid_number_check($obj->max)
Truth table33%1/3
A and B
A= defined $obj->maxB= $obj->valid_number_check($obj->max)
ABresult
0X0
100
111
64 0 warn "$proto: Maximum value not set";
65 0 return undef;
66 }
67  
68 1 ▶ if ( $obj->min() ne 'count' && $obj->max() ne 'count' && $obj->min() > $obj->max()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->min ne "count" and $obj->max ne "count" and $obj->min > $obj->max) 0 1 1
Condition33%2/6
$obj->min ne "count" and $obj->max ne "count" and $obj->min > $obj->max
!ll&&!rl&&r
010
$obj->min ne "count" and $obj->max ne "count"
!ll&&!rl&&r
001
MC/DC0%0/3
$obj->min ne "count" and $obj->max ne "count" and $obj->min > $obj->max
$obj->min ne "count" $obj->max ne "count" $obj->min > $obj->max
Truth table25%1/4
A and B and C
A= $obj->min ne "count"B= $obj->max ne "count"C= $obj->min > $obj->max
ABCresult
0XX0
10X0
1100
1111
69 0 warn "$proto: Minimum is greater than Maximum";
70 0 return undef;
71 }
72  
73 1 return $obj;
74 }
75  
76 =head2 calculate_back_off()
77  
78 Returns the new back off value.
79  
80 =cut
81  
82 10 ▶sub calculate_back_off {
Subroutine calculate_back_off: called CC=3 SCAR=11.2
Pod documented
83 10 my $self = shift;
84  
85 # this is an Random back off
86 10 my $min = $self->min();
87 10 my $max = $self->max();
88  
89 10 ▶ $min = $self->failure_count() if $min eq 'count';
Branch: 1 branch
BranchTrueFalseTotal
if $min eq "count" 0 10 10
90 10 ▶ $max = $self->failure_count() if $max eq 'count';
Branch: 1 branch
BranchTrueFalseTotal
if $max eq "count" 0 10 10
91  
92 10 return int (rand($max-$min) + $min);
93 }
94  
95 =cut
96  
97 1;
98  
99 =head1 Changes
100  
101 0.02 2007-08-12 -- Daniel Lo
102 - Documentation fixes. No code changes.
103  
104 0.01 2007-04-17 -- Daniel Lo
105 - Initial Version
106  
107 =head1 AUTHOR
108  
109 Daniel Lo <daniel_lo@picturetrail.com>
110  
111 =head1 LICENSE
112  
113 Copyright (C) PictureTrail Inc. 1999-2007
114 Santa Clara, California, United States of America.
115  
116 This code is released to the public for public use under Perl's Artisitic
117 licence.
118  
119 =cut
« BackOff/Linear.pm ↑ Summary BackOff/Exponential.pm »