blib/lib/Thread/Queue/MaxSize.pm

filter: Statementstmt 82.4% 94 / 114 Branchbran 71.4% 40 / 56 Conditioncond 56.8% 25 / 44 MC/DCmcdc 34.6% 9 / 26 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 71.5% 181 / 253 CC 44 SCAR 26.6
CC
44
Cov
74%
insert
20.8
enqueue
16.2
_validate_maxsize
9.9
CRAP 14.2
↑ Summary
1 package Thread::Queue::MaxSize;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '1.03';
7 8 $VERSION = eval $VERSION;
8  
9 8 ▶use parent qw(Thread::Queue);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use threads::shared 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Scalar::Util 1.10 qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶sub new {
Subroutine new: called CC=7 SCAR=22.6
Pod documented
15 17 my ($class, $config) = @_;
16 17 my $self = $class->SUPER::new();
17  
18 17 ▶ if ($config && (!ref($config) || ref($config) ne "HASH")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config and !ref($config) || ref $config ne "HASH") 0 17 17
Condition33%2/6
$config and !ref($config) || ref $config ne "HASH"
!ll&&!rl&&r
0170
not ref $config or ref $config ne "HASH"
l!l&&r!l&&!r
0017
MC/DC0%0/3
$config and !ref($config) || ref $config ne "HASH"
$config not ref $config ref $config ne "HASH"
Truth table25%1/4
A and not(B) or C
A= $configB= not ref $configC= ref $config ne "HASH"
ABCresult
0XX0
1000
11X1
1011
19 0 require Carp;
20 0 Carp::croak("invalid first argument to constructor -- must be a hashref with any configuration options");
21 }
22  
23 # make sure that maxsize is actually a number
24 17 ▶ my $maxsize = ($config) ? $config->{'maxsize'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$config ? : 17 0 17
25 17 $self->{'MAXSIZE'} = $self->_validate_maxsize($maxsize);
26  
27 # determine what type of action we'll take on exceeding our max size
28 # 1. raise an exception (die)
29 # 2. warn and reject entire addition/insertion
30 # 3. silently reject entire addition/insertion
31 # 4. warn, process addition/insertion and then truncate to max size
32 # 5. silently process addition/insertion and then truncate to max size
33 17 ▶ my $on_maxsize = ($config) ? $config->{'on_maxsize'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$config ? : 17 0 17
34 17 ▶ $self->{'ON_MAXSIZE'} = $self->_validate_on_maxsize($on_maxsize || 'silent_truncate');
Condition100%2/2
$on_maxsize || 'silent_truncate'
l!l
152
MC/DC100%1/1
$on_maxsize || 'silent_truncate'
$on_maxsize
Truth table100%2/2
A
A= $on_maxsize
Aresult
11
00
35  
36 17 return $self;
37 }
38  
39 # add items to the tail of a queue
40 84 ▶sub enqueue {
Subroutine enqueue: called CC=15 SCAR=27.9
Pod documented
41 84 my $self = shift;
42 84 lock(%$self);
43  
44 84 ▶ if ($self->{'ENDED'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENDED"}) 0 84 84
45 0 require Carp;
46 0 Carp::croak("'enqueue' method called on queue that has been 'end'ed");
47 }
48  
49 84 my $queue = $self->{'queue'};
50  
51 # queue can't be too big so shift the oldest things off if necessary
52 84 ▶ if (defined($self->{'MAXSIZE'}) && $self->{'MAXSIZE'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0) 81 3 84
Condition66%2/3
defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0
!ll&&!rl&&r
3081
MC/DC50%1/2
defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0
defined $self->{"MAXSIZE"} $self->{"MAXSIZE"} > 0
Truth table66%2/3
A and B
A= defined $self->{"MAXSIZE"}B= $self->{"MAXSIZE"} > 0
ABresult
0X0
100
111
53 81 ▶ if ((scalar(@{$queue}) + scalar(@_)) > $self->{'MAXSIZE'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi) 35 46 81
Condition100%3/3
scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
!ll&&!rl&&r
351135
MC/DC100%2/2
scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"} $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
Truth table100%3/3
A and B
A= scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}B= $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
ABresult
0X0
100
111
54 $self->{'ON_MAXSIZE'} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/ix) {
55 35 ▶ if ($1 =~ /^warn_and_truncate$/ix) {
Branch: 4 branches
BranchTrueFalseTotal
if ($1 =~ /^warn_and_truncate$/xi) { } 11 24 35
elsif ($1 =~ /^silent_reject$/xi) { } 11 13 24
elsif ($1 =~ /^warn_and_reject$/xi) { } 11 2 13
elsif ($1 =~ /^die$/xi) { } 2 0 2
56 11 warn "queue exceeding its maximum size: truncating\n";
57 } elsif ($1 =~ /^silent_reject$/ix) {
58 11 return;
59 } elsif ($1 =~ /^warn_and_reject$/ix) {
60 11 warn "not enqueuing new items: queue would exceed its maximum size\n";
61 11 return;
62 } elsif ($1 =~ /^die$/ix) {
63 2 die "not enqueuing new items: queue would exceed its maximum size\n";
64 }
65 }
66  
67 # remove things already on the queue
68 57 ▶ while (scalar(@{$queue}) && (scalar(@{$queue}) + scalar(@_)) > $self->{'MAXSIZE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}) 22 57 79
Condition100%3/3
scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}
!ll&&!rl&&r
154222
MC/DC100%2/2
scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}
scalar @{$queue;} scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}
Truth table100%3/3
A and B
A= scalar @{$queue;}B= scalar(@{$queue;}) + scalar(@_) > $self->{"MAXSIZE"}
ABresult
0X0
100
111
69 22 shift(@{$queue});
70 }
71  
72 # if we've already removed everything off of the queue and we're still
73 # over maxsize then take things off of the list of new items
74 57 ▶ while (scalar(@_) && (scalar(@_)) > $self->{'MAXSIZE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and scalar @_ > $self->{"MAXSIZE"}) 0 57 57
Condition33%1/3
scalar @_ and scalar @_ > $self->{"MAXSIZE"}
!ll&&!rl&&r
0570
MC/DC0%0/2
scalar @_ and scalar @_ > $self->{"MAXSIZE"}
scalar @_ scalar @_ > $self->{"MAXSIZE"}
Truth table33%1/3
A and B
A= scalar @_B= scalar @_ > $self->{"MAXSIZE"}
ABresult
0X0
100
111
75 0 shift(@_);
76 }
77 }
78  
79 60 ▶ push(@{$queue}, map { shared_clone($_) } @_) and cond_signal(%$self);
Branch: 1 branch
BranchTrueFalseTotal
push @{$queue;}, map({shared_clone($_);} @_) and &cond_signal(\%$self) 60 0 60
80 }
81  
82 # insert items anywhere into a queue
83 9 ▶sub insert {
Subroutine insert: called CC=18 SCAR=30.3
Pod documented
84 9 my $self = shift;
85 9 lock(%$self);
86  
87 9 ▶ if ($self->{'ENDED'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENDED"}) 0 9 9
88 0 require Carp;
89 0 Carp::croak("'insert' method called on queue that has been 'end'ed");
90 }
91  
92 9 my $queue = $self->{'queue'};
93  
94 9 my $index = $self->_validate_index(shift);
95  
96 # make sure we have something to insert
97 5 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
98  
99 # support negative indices
100 5 ▶ if ($index < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 0 5 5
101 0 $index += @{$queue};
102 0 ▶ $index = 0 if ($index < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
103 }
104  
105 # dequeue items from $index onward
106 5 my @tmp = ();
107 5 ▶ while (@{$queue} > $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$queue;} > $index) 25 5 30
108 25 unshift(@tmp, pop(@{$queue}))
109 }
110  
111 # queue can't be too big so shift the oldest things off if necessary
112 5 ▶ if (defined($self->{'MAXSIZE'}) && $self->{'MAXSIZE'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0) 5 0 5
Condition33%1/3
defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0
!ll&&!rl&&r
005
MC/DC0%0/2
defined $self->{"MAXSIZE"} and $self->{"MAXSIZE"} > 0
defined $self->{"MAXSIZE"} $self->{"MAXSIZE"} > 0
Truth table33%1/3
A and B
A= defined $self->{"MAXSIZE"}B= $self->{"MAXSIZE"} > 0
ABresult
0X0
100
111
113 5 ▶ if ((scalar(@{$queue}) + scalar(@_) + scalar(@tmp)) > $self->{'MAXSIZE'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi) 4 1 5
Condition66%2/3
scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
!ll&&!rl&&r
014
MC/DC50%1/2
scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"} and $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"} $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
Truth table66%2/3
A and B
A= scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}B= $self->{"ON_MAXSIZE"} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/xi
ABresult
0X0
100
111
114 $self->{'ON_MAXSIZE'} =~ /^(die|warn_and_reject|silent_reject|warn_and_truncate)$/ix) {
115 4 ▶ if ($1 =~ /^warn_and_truncate$/ix) {
Branch: 4 branches
BranchTrueFalseTotal
if ($1 =~ /^warn_and_truncate$/xi) { } 1 3 4
elsif ($1 =~ /^silent_reject$/xi) { } 1 2 3
elsif ($1 =~ /^warn_and_reject$/xi) { } 1 1 2
elsif ($1 =~ /^die$/xi) { } 1 0 1
116 1 warn "queue exceeding its maximum size: truncating\n";
117 } elsif ($1 =~ /^silent_reject$/ix) {
118 # reset queue before dying
119 1 push(@{$queue}, @tmp);
120 1 return;
121 } elsif ($1 =~ /^warn_and_reject$/ix) {
122 # reset queue before dying
123 1 push(@{$queue}, @tmp);
124 1 warn "not inserting new items: queue would exceed its maximum size\n";
125 1 return;
126 } elsif ($1 =~ /^die$/ix) {
127 # reset queue before dying
128 1 push(@{$queue}, @tmp);
129 1 die "not inserting new items: queue would exceed its maximum size\n";
130 }
131 }
132  
133 # remove things already on the queue
134 2 ▶ while (scalar(@{$queue}) && (scalar(@{$queue}) + scalar(@_) + scalar(@tmp)) > $self->{'MAXSIZE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}) 6 2 8
Condition66%2/3
scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
!ll&&!rl&&r
206
MC/DC50%1/2
scalar @{$queue;} and scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
scalar @{$queue;} scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
Truth table66%2/3
A and B
A= scalar @{$queue;}B= scalar(@{$queue;}) + scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
ABresult
0X0
100
111
135 6 shift(@{$queue});
136 }
137  
138 # if we've already removed everything off of the queue and we're still
139 # over maxsize then take things off of the list of new items
140 2 ▶ while (scalar(@_) && (scalar(@_) + scalar(@tmp)) > $self->{'MAXSIZE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}) 2 2 4
Condition66%2/3
scalar @_ and scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
!ll&&!rl&&r
022
MC/DC50%1/2
scalar @_ and scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
scalar @_ scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
Truth table66%2/3
A and B
A= scalar @_B= scalar(@_) + scalar(@tmp) > $self->{"MAXSIZE"}
ABresult
0X0
100
111
141 2 shift(@_);
142 }
143 }
144  
145 # add new items to the queue
146 2 push(@{$queue}, map { shared_clone($_) } @_);
147  
148 # add previous items back onto the queue
149 2 push(@{$queue}, @tmp);
150  
151 # soup's up
152 2 cond_signal(%$self);
153 }
154  
155 17 ▶sub _validate_maxsize {
Subroutine _validate_maxsize: called CC=5 SCAR=22.9
156 17 my ($self, $maxsize) = @_;
157  
158 17 ▶ if (defined($maxsize) && (!looks_like_number($maxsize) || (int($maxsize) != $maxsize) || ($maxsize < 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxsize and !&looks_like_number($maxsize) || int $maxsize != $maxsize || $maxsize < 1) 0 17 17
Condition44%4/9
defined $maxsize and !&looks_like_number($maxsize) || int $maxsize != $maxsize || $maxsize < 1
!ll&&!rl&&r
2150
not &looks_like_number($maxsize) or int $maxsize != $maxsize or $maxsize < 1
l!l&&r!l&&!r
0015
not &looks_like_number($maxsize) or int $maxsize != $maxsize
l!l&&r!l&&!r
0015
MC/DC0%0/4
defined $maxsize and !&looks_like_number($maxsize) || int $maxsize != $maxsize || $maxsize < 1
defined $maxsize not &looks_like_number($maxsize) int $maxsize != $maxsize $maxsize < 1
Truth table40%2/5
A and not(B) or C or D
A= defined $maxsizeB= not &looks_like_number($maxsize)C= int $maxsize != $maxsizeD= $maxsize < 1
ABCDresult
0XXX0
10000
11XX1
101X1
10011
159 0 require Carp;
160 0 my ($method) = (caller(1))[3];
161 0 my $class_name = ref($self);
162 0 $method =~ s/${class_name}:://;
163 0 Carp::croak("Invalid 'maxsize' argument ($maxsize) to '$method' method");
164 }
165  
166 17 return $maxsize;
167 }
168  
169 17 ▶sub _validate_on_maxsize {
Subroutine _validate_on_maxsize: called CC=3 SCAR=16.3
170 17 my ($self, $on_maxsize) = @_;
171  
172 17 ▶ if (defined($on_maxsize) && ($on_maxsize !~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/ix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_maxsize and not $on_maxsize =~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/xi) 0 17 17
Condition33%1/3
defined $on_maxsize and not $on_maxsize =~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/xi
!ll&&!rl&&r
0170
MC/DC0%0/2
defined $on_maxsize and not $on_maxsize =~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/xi
defined $on_maxsize not $on_maxsize =~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/xi
Truth table33%1/3
A and not(B)
A= defined $on_maxsizeB= not $on_maxsize =~ /^(?:die|warn_and_reject|silent_reject|warn_and_truncate|silent_truncate)$/xi
ABresult
0X0
100
111
173 0 require Carp;
174 0 my ($method) = (caller(1))[3];
175 0 my $class_name = ref($self);
176 0 $method =~ s/${class_name}:://;
177 0 Carp::croak("Invalid 'on_maxsize' argument ($on_maxsize) to '$method' method");
178 }
179  
180 17 return $on_maxsize;
181 }
182  
183 8 1;
184  
185 =head1 NAME
186  
187 Thread::Queue::MaxSize - Thread-safe queues with an upper bound
188  
189 =head1 VERSION
190  
191 This document describes Thread::Queue::MaxSize version 1.02
192  
193 =head1 SYNOPSIS
194  
195 use strict;
196 use warnings;
197  
198 use threads;
199 use Thread::Queue::MaxSize;
200  
201 # create a new empty queue with no max limit
202 my $q = Thread::Queue::MaxSize->new();
203  
204 # create a new empty queue that will only ever store 1000 items
205 my $q = Thread::Queue::MaxSize->new({ maxsize => 1000 });
206  
207 # create a queue that will die when too many items are enqueued
208 my $q = Thread::Queue::MaxSize->new({ maxsize => 1000, on_maxsize => 'die' });
209  
210 =head1 DESCRIPTION
211  
212 This is a subclass to L<Thread::Queue> that will enforce an upper bound on the
213 number of items in a queue. This can be used to prevent memory use from
214 exploding on a queue that might never empty.
215  
216 =head1 QUEUE CREATION
217  
218 =over
219  
220 =item ->new()
221  
222 Creates a new empty queue. This queue will have no items to start.
223  
224 =item ->new(OPTIONS)
225  
226 Creates a new empty queue with some options. The two configurable options are:
227  
228 =over
229  
230 =item maxsize
231  
232 Defines the maximum size that the queue can ever be.
233  
234 =item on_maxsize
235  
236 Defines the action that will be taken when a queue reaches its maximum size.
237 There are five actions that can be taken when the list of items to enqueue or
238 insert would cause the queue to go over its maximum size. In all cases either
239 the all items are enqueued/inserted or none of the items are enqueued/inserted.
240  
241 =over
242  
243 =item die
244  
245 No items will be enqueued/inserted and the queue will throw an exception.
246  
247 =item warn_and_reject
248  
249 No items will be enqueued/inserted and the queue will issue a warning.
250  
251 =item silent_reject
252  
253 No items will be enqueued/inserted and no indication will be given as to why.
254  
255 =back warn_and_truncate
256  
257 All items will be enqueued/inserted, the oldest items on the list will be
258 truncated off the end, and the queue will issue a warning.
259  
260 =back silent_truncate
261  
262 All items will be enqueued/insertd, the oldest items on the list will be
263 truncated off the end, and no indication will be given as to why. This is the
264 default action.
265  
266 =back
267  
268 =head1 SEE ALSO
269  
270 L<Thread::Queue>, L<threads>, L<threads::shared>
271  
272 =head1 MAINTAINER
273  
274 Paul Lockaby S<E<lt>plockaby AT cpan DOT orgE<gt>>
275  
276 =head1 CREDIT
277  
278 Significant portions of this module are directly from L<Thread::Queue> which is
279 maintained by Jerry D. Hedden, <jdhedden AT cpan DOT org>.
280  
281 =head1 LICENSE
282  
283 This program is free software; you can redistribute it and/or modify it under
284 the same terms as Perl itself.
285  
286 =cut
↑ Summary