Multi/Base.pm

filter: Statementstmt 100% 80 / 80 Branchbran 100% 32 / 32 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 18 / 18 Podpod 20.0% 1 / 5 totaltotal 95.8% 140 / 146 CC 20 SCAR 14.2
CC
20
Cov
99%
lock
8.0
new
7.0
_lock_non_block
4.0
CRAP 4.1
« Multi/MySQL.pm ↑ Summary Multi/FlockFiles.pm »
1 package File::Lock::Multi::Base;
2  
3 8 ▶use 5.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use base q(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Params::Classify qw(is_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 __PACKAGE__->mk_accessors(qw(max name timeout polling_interval));
14  
15 8 return 1;
16  
17 # rename file -> name to make more sense with virtual resources
18 367 ▶sub file { &name }
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
19  
20 sub __Validators {
21 26 ▶ my $class = shift;
Subroutine __Validators: called CC=1 SCAR=0.0
22  
23 my $float_spec = { optional => 1, callbacks => { number => sub {
24 20 ▶ is_number(shift)
Subroutine __ANON__: called CC=1 SCAR=0.0
25 } }
26 26 };;
27 26 my $integer_spec = { optional => 1, regex => qr/^\d+$/ };
28  
29 return(
30 26 name => 1,
31 polling_interval => $float_spec,
32 timeout => $float_spec,
33 max => $integer_spec,
34 @_
35 );
36 }
37  
38  
39 sub new {
40 28 ▶ my($class, %args_in) = @_;
Subroutine new: called CC=7 SCAR=19.5
Pod documented
41 28 (my $subclass = __PACKAGE__) =~ s{::Base$}{};
42  
43 28 ▶ $args_in{name} = delete $args_in{file} if exists $args_in{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args_in{"file"} 24 4 28
44 # silliness to accomodate Params::Validate
45 28 my @args_in = %args_in;
46  
47 28 ▶ croak "$class is a base class; please find a suitable subclass to use"
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ or $class eq $subclass 2 26 28
Condition66%2/3
$class eq __PACKAGE__ or $class eq $subclass
l!l&&r!l&&!r
0226
MC/DC50%1/2
$class eq __PACKAGE__ or $class eq $subclass
$class eq __PACKAGE__ $class eq $subclass
Truth table66%2/3
A or B
A= $class eq __PACKAGE__B= $class eq $subclass
ABresult
1X1
011
000
48 if $class eq __PACKAGE__ || $class eq $subclass;
49  
50 26 my %validate_spec = $class->__Validators;
51 26 my %args = validate(@args_in, \%validate_spec);
52  
53 24 ▶ $args{polling_interval} ||= 0.2;
Condition100%2/2
$args{"polling_interval"} ||= "0.2"
l!l
222
MC/DC100%1/1
$args{"polling_interval"} ||= "0.2"
$args{"polling_interval"}
Truth table100%2/2
A
A= $args{"polling_interval"}
Aresult
11
00
54 24 ▶ $args{timeout} = -1 unless defined $args{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeout"} 6 18 24
55 24 ▶ $args{max} ||= 1;
Condition100%2/2
$args{"max"} ||= 1
l!l
168
MC/DC100%1/1
$args{"max"} ||= 1
$args{"max"}
Truth table100%2/2
A
A= $args{"max"}
Aresult
11
00
56  
57 24 return $class->SUPER::new(\%args);
58 }
59  
60 sub lockable {
61 10 ▶ my $self = shift;
Subroutine lockable: called CC=2 SCAR=6.9
Pod undocumented
62 10 ▶ if($self->lock(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lock(0)) { } 4 6 10
63 4 return $self->release;
64 } else {
65 6 return;
66 }
67 }
68  
69 sub _lock_non_block {
70 114 ▶ my $self = shift;
Subroutine _lock_non_block: called CC=4 SCAR=13.9
71  
72 114 ▶ croak("i already have a lock on ", $self->file) if $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 2 112 114
73  
74 112 ▶ if(my $id = $self->_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_lock) { } 71 41 112
75 71 ▶ if($self->lockers > $self->max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lockers > $self->max) { } 39 32 71
76 39 $self->release;
77 39 return;
78 } else {
79 32 return $id;
80 }
81 } else {
82 41 return;
83 }
84 }
85  
86 sub lock {
87 50 ▶ my $self = shift;
Subroutine lock: called CC=8 SCAR=20.8
Pod undocumented
88  
89 50 ▶ my $timeout = scalar(@_) ? shift : $self->timeout;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 16 34 50
90 50 ▶ return $self->_lock_non_block unless $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout 30 20 50
91  
92 20 my $polling_interval = $self->polling_interval;
93  
94 20 ▶ if($timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout < 0) { } 8 12 20
95 8 while(1) {
96 38 ▶ if(my $id = $self->_lock_non_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_lock_non_block) { } 8 30 38
97 8 return $id;
98 } else {
99 30 sleep($polling_interval);
100 }
101 }
102 } else {
103 12 my $cycles = $timeout / $polling_interval;
104 12 ▶ if($cycles < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cycles < 1) 2 10 12
105 2 $cycles = 1;
106 2 $polling_interval = $timeout;
107 }
108  
109 12 ▶ while($cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cycles) 46 8 54
110 46 ▶ if(my $id = $self->_lock_non_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_lock_non_block) { } 4 42 46
111 4 return $id;
112 } else {
113 42 sleep($polling_interval);
114 42 $cycles --;
115 }
116 }
117 }
118 }
119  
120 sub release {
121 75 ▶ my $self = shift;
Subroutine release: called CC=2 SCAR=6.9
Pod undocumented
122 75 ▶ croak("i do not have a lock on ", $self->file) unless $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->locked 4 71 75
123 71 return $self->_release;
124 }
125  
126 sub DESTROY {
127 24 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
128 24 ▶ $self->release if $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 8 16 24
129 }
130  
131  
« Multi/MySQL.pm ↑ Summary Multi/FlockFiles.pm »