File Coverage

lib/IPC/SRLock/Base.pm
Criterion Covered Total %
statement 54 54 100.0
branch 5 6 100.0
condition 7 9 100.0
subroutine 13 13 100.0
pod 1 1 100.0
total 80 83 100.0


line stmt bran cond sub pod time code
1             package IPC::SRLock::Base;
2              
3 1     1   441 use namespace::autoclean;
  1         1  
  1         5  
4              
5 1     1   937 use Date::Format qw( time2str );
  1         5282  
  1         54  
6 1     1   6 use English qw( -no_match_vars );
  1         1  
  1         6  
7 1         7 use File::DataClass::Types qw( Bool LoadableClass NonEmptySimpleStr
8 1     1   309 Num Object PositiveInt );
  1         2  
9 1     1   812 use IPC::SRLock::Utils qw( Unspecified hash_from merge_attributes throw );
  1         1  
  1         5  
10 1     1   726 use Time::Elapsed qw( elapsed );
  1         2578  
  1         4  
11 1     1   705 use Time::HiRes qw( usleep );
  1         1002  
  1         4  
12 1     1   126 use Moo;
  1         1  
  1         8  
13              
14             # Public attributes
15             has 'debug' => is => 'ro', isa => Bool, default => 0;
16              
17             has 'log' => is => 'lazy', isa => Object,
18 5     5   554 builder => sub { $_[ 0 ]->_null_class->new };
19              
20             has 'name' => is => 'ro', isa => NonEmptySimpleStr, required => 1;
21              
22             has 'nap_time' => is => 'ro', isa => Num, default => 0.1;
23              
24             has 'patience' => is => 'ro', isa => PositiveInt, default => 0;
25              
26             has 'time_out' => is => 'ro', isa => PositiveInt, default => 300;
27              
28             # Private attributes
29             has '_null_class' => is => 'lazy', isa => LoadableClass,
30             default => 'Class::Null', init_arg => undef;
31              
32             # Construction
33             around 'BUILDARGS' => sub {
34             my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args );
35              
36             my $builder = $attr->{builder} or return $attr;
37              
38             merge_attributes $attr, $builder, [ 'debug', 'log' ];
39              
40             return $attr;
41             };
42              
43             # Private methods
44             sub _get_args {
45 15     15   20 my $self = shift; my $args = hash_from @_;
  15         40  
46              
47 15 100       37 $args->{k} or throw Unspecified, [ 'key' ]; $args->{k} .= q();
  14         23  
48 14   66     59 $args->{p} //= $PID; # uncoverable condition false
49 14   66     68 $args->{t} //= $self->time_out; # uncoverable condition false
50              
51 14         37 return $args;
52             }
53              
54             sub _sleep_or_timeout {
55 3     3   4836 my ($self, $start, $now, $key) = @_;
56              
57 3 100 100     34 $self->patience and $now > $start + $self->patience
58             and throw 'Lock [_1] timed out', [ $key ];
59 2         200271 usleep( 1_000_000 * $self->nap_time );
60 2         38 return 1;
61             }
62              
63             sub _timeout_error {
64 1     1   14 my ($self, $key, $pid, $when, $after) = @_;
65              
66 1         6 return "Timed out ${key} set by ${pid} on "
67             . time2str( '%Y-%m-%d at %H:%M', $when )." after ${after} seconds\n";
68             }
69              
70             # Public methods
71             sub get_table {
72 4     4 1 495 my $self = shift;
73 4         7 my $count = 0;
74 4         50 my $data = { align => { id => 'left',
75             pid => 'right',
76             stime => 'right',
77             tleft => 'right'},
78             count => $count,
79             fields => [ qw( id pid stime tleft ) ],
80             hclass => { id => 'most' },
81             labels => { id => 'Key',
82             pid => 'PID',
83             stime => 'Lock Time',
84             tleft => 'Time Left' },
85             values => [] };
86              
87 4         7 for my $lock (@{ $self->list }) {
  4         12  
88 2         4 my $fields = {};
89              
90 2         4 $fields->{id } = $lock->{key};
91 2         5 $fields->{pid } = $lock->{pid};
92 2         9 $fields->{stime} = time2str( '%Y-%m-%d %H:%M:%S', $lock->{stime} );
93              
94 2         213 my $tleft = $lock->{stime} + $lock->{timeout} - time;
95              
96             # uncoverable branch false
97 2 50       13 $fields->{tleft} = $tleft > 0 ? elapsed( $tleft ) : 'Expired';
98 2         1015 push @{ $data->{values} }, $fields; $count++;
  2         6  
  2         4  
99             }
100              
101 4         8 $data->{count} = $count;
102 4         18 return $data;
103             }
104              
105             1;
106              
107             __END__