Backbone/SendPolicy/MinimumInterval.pm

filter: Statementstmt 100% 33 / 33 Branchbran 91.6% 11 / 12 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 87.8% 58 / 66 CC 10 SCAR 16.0
CC
10
Cov
93%
allow_send
7.2
_too_soon
4.0
BEGIN
1.0
CRAP 4.9
« Backbone/Dispatcher/Predicate.pm ↑ Summary Backbone/Service/DirectChat.pm »
1 package Bot::Backbone::SendPolicy::MinimumInterval;
2 1 $Bot::Backbone::SendPolicy::MinimumInterval::VERSION = '0.161950';
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 with 'Bot::Backbone::SendPolicy';
7  
8 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Prevent any message from being delivered too soon
11  
12  
13 1 has interval => (
14 is => 'ro',
15 isa => 'Num',
16 required => 1,
17 );
18  
19  
20 1 has queue_length => (
21 is => 'ro',
22 isa => 'Int',
23 predicate => 'has_queue',
24 );
25  
26  
27 1 has discard => (
28 is => 'ro',
29 isa => 'Bool',
30 required => 1,
31 default => 0,
32 );
33  
34  
35 1 has last_send_time => (
36 is => 'rw',
37 isa => 'Num',
38 predicate => 'has_last_send_time',
39 );
40  
41  
42 598 ▶sub _too_soon {
Subroutine _too_soon: called CC=4 SCAR=13.9
43 598 my $self = shift;
44 598 my $now = AnyEvent->now;
45  
46 598 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_last_send_time 2 596 598
47 unless $self->has_last_send_time;
48  
49 596 ▶ return $self->last_send_time + $self->interval
Branch: 1 branch
BranchTrueFalseTotal
if $self->last_send_time > $now or $now - $self->last_send_time < $self->interval 592 4 596
Condition100%3/3
$self->last_send_time > $now or $now - $self->last_send_time < $self->interval
l!l&&r!l&&!r
2972954
MC/DC100%2/2
$self->last_send_time > $now or $now - $self->last_send_time < $self->interval
$self->last_send_time > $now $now - $self->last_send_time < $self->interval
Truth table100%3/3
A or B
A= $self->last_send_time > $nowB= $now - $self->last_send_time < $self->interval
ABresult
1X1
011
000
50 if ($self->last_send_time > $now)
51 or ($now - $self->last_send_time < $self->interval);
52  
53 4 return 0;
54 }
55  
56 598 ▶sub allow_send {
Subroutine allow_send: called CC=7 SCAR=19.7
Pod documented
57 598 my ($self, $options) = @_;
58  
59 598 my %send = ( allow => 1 );
60 598 my $now = AnyEvent->now;
61 598 my $too_soon = $self->_too_soon;
62  
63 598 my $save = 1;
64 598 ▶ if ($too_soon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($too_soon) 592 6 598
65  
66 # Messages coming too fast should be thrown away
67 592 ▶ if ($self->discard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->discard) { } 294 298 592
68 294 $save = 0;
69 294 $send{allow} = 0;
70 }
71  
72 # Messages coming too fast should be postponed
73 else {
74 298 $send{after} = $too_soon - $now;
75  
76 # If the number of messages queued is too long, nevermind...
77 $send{allow} = 0
78 if $self->has_queue
79 298 ▶ and $send{after} / $self->interval > $self->queue_length;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_queue and $send{"after"} / $self->interval > $self->queue_length 0 298 298
Condition33%1/3
$self->has_queue and $send{"after"} / $self->interval > $self->queue_length
!ll&&!rl&&r
29800
MC/DC0%0/2
$self->has_queue and $send{"after"} / $self->interval > $self->queue_length
$self->has_queue $send{"after"} / $self->interval > $self->queue_length
Truth table33%1/3
A and B
A= $self->has_queueB= $send{"after"} / $self->interval > $self->queue_length
ABresult
0X0
100
111
80 }
81 }
82  
83 598 ▶ $self->last_send_time($too_soon || $now) if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 304 294 598
Condition66%2/3
$too_soon || $now
l!l&&r!l&&!r
29860
MC/DC0%0/2
$too_soon || $now
$too_soon $now
Truth table66%2/3
A || B
A= $too_soonB= $now
ABresult
1X1
011
000
84 598 return \%send;
85 }
86  
87 1 __PACKAGE__->meta->make_immutable;
88  
89 __END__
« Backbone/Dispatcher/Predicate.pm ↑ Summary Backbone/Service/DirectChat.pm »