blib/lib/AnyEvent/FileLock.pm

filter: Statementstmt 90.7% 59 / 65 Branchbran 62.5% 15 / 24 Conditioncond 50.0% 12 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 71.3% 97 / 136 CC 25 SCAR 30.2
CC
25
Cov
76%
flock
32.4
_acquire_lock
7.5
BEGIN
1.0
CRAP 20.4
↑ Summary
1 package AnyEvent::FileLock;
2  
3 1 our $VERSION = '0.06';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use AE;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Method::WeakCallback qw(weak_method_callback);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶sub flock {
Subroutine flock: called CC=19 SCAR=34.8
Pod undocumented
15 3 my ($class, %opts) = @_;
16  
17 3 ▶ my $type = delete $opts{type} // 'flock';
Condition50%1/2
delete $opts{"type"} // "flock"
l!l
03
MC/DC0%0/1
delete $opts{"type"} // "flock"
delete $opts{"type"}
Truth table50%1/2
A
A= delete $opts{"type"}
Aresult
11
00
18 3 ▶ $type =~ /^(?:flock|fcntl)$/ or croak "invalid lock type '$type'";
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^(?:flock|fcntl)$/ or croak("invalid lock type '${type}'") 3 0 3
19  
20 3 ▶ my $mode = delete $opts{mode} // '+<';
Condition50%1/2
delete $opts{"mode"} // "+<"
l!l
03
MC/DC0%0/1
delete $opts{"mode"} // "+<"
delete $opts{"mode"}
Truth table50%1/2
A
A= delete $opts{"mode"}
Aresult
11
00
21 3 ▶ my $lock_mode = delete $opts{lock_mode} // $mode;
Condition33%1/3
delete $opts{"lock_mode"} // $mode
l!l&&r!l&&!r
030
MC/DC0%0/2
delete $opts{"lock_mode"} // $mode
delete $opts{"lock_mode"} $mode
Truth table33%1/3
A // B
A= delete $opts{"lock_mode"}B= $mode
ABresult
1X1
011
000
22 3 ▶ my $delay = delete $opts{delay} || 0.1;
Condition50%1/2
delete $opts{'delay'} || 0.1
l!l
03
MC/DC0%0/1
delete $opts{'delay'} || 0.1
delete $opts{'delay'}
Truth table50%1/2
A
A= delete $opts{'delay'}
Aresult
11
00
23  
24 3 ▶ my $user_cb = delete $opts{cb} // croak "cb argument is missing";
Condition50%1/2
delete $opts{"cb"} // croak("cb argument is missing")
l!l
30
MC/DC0%0/1
delete $opts{"cb"} // croak("cb argument is missing")
delete $opts{"cb"}
Truth table50%1/2
A
A= delete $opts{"cb"}
Aresult
11
00
25  
26 3 my $max_time;
27 3 ▶ if (defined(my $timeout = delete $opts{timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $timeout = delete $opts{"timeout"})) 1 2 3
28 1 $max_time = AE::now() + $timeout;
29 }
30  
31 3 my $fh;
32 3 my $file = delete $opts{file};
33 3 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 2 1 3
34 2 ▶ my $open_mode = delete $opts{open_mode} // $mode;
Condition33%1/3
delete $opts{"open_mode"} // $mode
l!l&&r!l&&!r
020
MC/DC0%0/2
delete $opts{"open_mode"} // $mode
delete $opts{"open_mode"} $mode
Truth table33%1/3
A // B
A= delete $opts{"open_mode"}B= $mode
ABresult
1X1
011
000
35 2 ▶ $open_mode =~ /^\+?(?:<|>>?)/ or croak "bad mode specification";
Branch: 1 branch
BranchTrueFalseTotal
$open_mode =~ /^\+?(?:<|>>?)/ or croak("bad mode specification") 2 0 2
36 2 ▶ open $fh, $mode, $file or return
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $file or return 2 0 2
37 }
38 else {
39 1 ▶ $fh = delete $opts{fh} // croak "file or fh argument is required";
Condition50%1/2
delete $opts{"fh"} // croak("file or fh argument is required")
l!l
10
MC/DC0%0/1
delete $opts{"fh"} // croak("file or fh argument is required")
delete $opts{"fh"}
Truth table50%1/2
A
A= delete $opts{"fh"}
Aresult
11
00
40 }
41  
42 3 my $self = { file => $file,
43 fh => $fh,
44 type => $type,
45 max_time => $max_time,
46 user_cb => $user_cb,
47 delay => $delay };
48  
49 3 bless $self, $class;
50  
51 3 ▶ if ($type eq 'fcntl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'fcntl') { } 0 3 3
52 $self->{$_} = delete($opts{lock_start}) // 0
53 0 ▶ for qw(lock_start lock_whence lock_len);
Condition0%0/2
delete $opts{"lock_start"} // 0
l!l
00
MC/DC0%0/1
delete $opts{"lock_start"} // 0
delete $opts{"lock_start"}
Truth table0%0/2
A
A= delete $opts{"lock_start"}
Aresult
11
00
54 0 ▶ $self->{operation} = ($lock_mode eq '<' ? Fcntl::F_RDLCK() : Fcntl::F_WRLCK());
Branch: 1 branch
BranchTrueFalseTotal
$lock_mode eq '<' ? : 0 0 0
55 }
56 else {
57 3 ▶ $self->{operation} = ($lock_mode eq '<' ? Fcntl::LOCK_SH() : Fcntl::LOCK_EX());
Branch: 1 branch
BranchTrueFalseTotal
$lock_mode eq '<' ? : 0 3 3
58 }
59  
60  
61 3 ▶ %opts and croak "unkwnown arguments found (".join(', ', sort keys %opts).")";
Branch: 1 branch
BranchTrueFalseTotal
%opts and croak('unkwnown arguments found (' . join(", ", sort(keys %opts)) . ')') 0 3 3
62  
63 3 my $alcb = $self->{acquire_lock_cb} = weak_method_callback($self, '_acquire_lock');
64 3 &AE::postpone($alcb);
65  
66 3 $self;
67 }
68  
69 16 ▶sub _acquire_lock {
Subroutine _acquire_lock: called CC=7 SCAR=20.1
70 16 my $self = shift;
71 16 my $operation = $self->{opertation};
72 16 my $now = AE::now;
73  
74 16 my $ok;
75 16 ▶ if ($self->{type} eq 'flock') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'flock') { } 16 0 16
76 16 $ok = CORE::flock($self->{fh}, $self->{operation}|Fcntl::LOCK_NB());
77 }
78 else {
79 0 require Fcntl::Packer;
80 0 my %flock = (type => $self->{operation});
81 0 $flock{$_} = $self->{"lock_$_"} for qw(whence start len);
82 0 $ok = fcntl($self->{fh}, Fcntl::F_SETLK, Fcntl::Packer::pack_fcntl_flock(\%flock));
83 }
84 16 ▶ if ($ok) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok) { } 2 14 16
elsif ($! == &Errno::EAGAIN() and !defined($self->{'max_time'}) || $self->{'max_time'} >= $now) { } 13 1 14
85 2 $self->{user_cb}->($self->{fh});
86 }
87 ▶ elsif ($! == Errno::EAGAIN() and
Condition83%5/6
$! == &Errno::EAGAIN() and !defined($self->{"max_time"}) || $self->{"max_time"} >= $now
!ll&&!rl&&r
0113
not defined $self->{"max_time"} or $self->{"max_time"} >= $now
l!l&&r!l&&!r
3101
MC/DC66%2/3
$! == &Errno::EAGAIN() and !defined($self->{"max_time"}) || $self->{"max_time"} >= $now
$! == &Errno::EAGAIN() not defined $self->{"max_time"} $self->{"max_time"} >= $now
Truth table75%3/4
A and not(B) or C
A= $! == &Errno::EAGAIN()B= not defined $self->{"max_time"}C= $self->{"max_time"} >= $now
ABCresult
0XX0
1000
11X1
1011
88 (!defined($self->{max_time}) or $self->{max_time} >= $now)) {
89 # we add some randomness into the delay to avoid the case
90 # where all the contenders follow exactly the same pattern so
91 # that they end looking for the pattern all at once every time
92 # (and obviosly all but one failing).
93 13 $self->{timer} = &AE::timer($self->{delay} * (0.8 + rand 0.40), 0, $self->{acquire_lock_cb});
94 13 return;
95 }
96 else {
97 1 $self->{user_cb}->();
98 }
99 # release all the references, the object is useless from this
100 # point on time.
101 3 %$self = ();
102 }
103  
104 1 1;
105 __END__
↑ Summary