Beanstalk/Send.pm

filter: Statementstmt 35.0% 27 / 77 Branchbran 7.1% 2 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 6 / 18 Podpod 0.0% 0 / 3 totaltotal 21.8% 35 / 160 CC 21 SCAR 35.9
CC
21
Cov
23%
put
110.0
_send
20.0
__ANON__
20.0
CRAP 36.3
↑ Summary Beanstalk/Receive.pm »
1 package Net::Async::Beanstalk::Send;
2  
3 3 our $VERSION = '0.001';
4 3 $VERSION = eval $VERSION;
5  
6 =head1 NAME
7  
8 Net::Async::Beanstalk::Send - Methods to send commands to beanstalk
9  
10 =head1 DOCUMENTED ELSEWHERE
11  
12 This module's external API is documented in L<Net::Async::Beanstalk>
13  
14 =cut
15  
16 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Net::Async::Beanstalk::Constant qw(:send :state);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # TODO: Document internal API
24  
25 0 ▶sub _next {
Subroutine _next: not called CC=2 SCAR=17.9
26 0 my $self = shift;
27 0 ▶ return unless $self->count_commands >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_commands >= 1 0 0 0
28 0 my $current = $self->current_command;
29 0 $current->[STATE_SEND] = join(' ', @$current[STATE_COMMAND..$#$current]) . $NL;
30 0 $self->adopt_future($self->write($current->[STATE_SEND]));
31 }
32  
33 # TODO: Warn if reserves are pending
34 # TODO: Don't send if not connected
35 0 ▶sub _send {
Subroutine _send: not called CC=4 SCAR=30.0
36 0 my $self = shift;
37 0 my ($cmd) = @_;
38 0 ▶ croak "Invalid command" unless exists $COMMAND{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Net::Async::Beanstalk::Send::COMMAND{$cmd} 0 0 0
39 0 my $future = $self->loop->new_future;
40 0 ▶ $future->on_ready(sub { $self->_next });
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 $self->_push_command([ $future, (undef) x (STATE_COMMAND - 1), @_ ]);
42 0 ▶ $self->_next if $self->count_commands == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count_commands == 1 0 0 0
43 0 ▶ defined wantarray ? $future : $future->get;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
44 }
45  
46 0 ▶around decoder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 my $orig = shift;
48 0 my $self = shift;
49 0 my ($no_decode) = @_;
50 0 my $decoder = $self->$orig();
51 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 my $utf8 = ''.$_[0];
53 0 utf8::decode($utf8);
54 0 ▶ $no_decode ? $utf8 : $decoder->($utf8);
Branch: 1 branch
BranchTrueFalseTotal
$no_decode ? : 0 0 0
55 0 };
56 3 };
57  
58 0 ▶around encoder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my $orig = shift;
60 0 my $self = shift;
61 0 my $encoder = $self->$orig();
62 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
63 0 ▶ my $result = '' . ((not ref $_[0] or overload::Method($_[0], '""'))
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) || overload::Method($_[0], '""') ? : 0 0 0
Condition0%0/3
not ref $_[0] or overload::Method($_[0], "\"\"")
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $_[0] or overload::Method($_[0], "\"\"")
not ref $_[0] overload::Method($_[0], "\"\"")
Truth table0%0/3
not(A) or B
A= not ref $_[0]B= overload::Method($_[0], "\"\"")
ABresult
1X1
011
000
64 ? $_[0]
65 : $encoder->(@_));
66 0 ▶ utf8::encode($result) if utf8::is_utf8($result);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($result) 0 0 0
67 0 $result;
68 0 };
69 3 };
70  
71 0 ▶sub put {
Subroutine put: not called CC=10 SCAR=47.0
Pod undocumented
72 0 my $self = shift;
73 0 ▶ my $job; $job = $self->encoder->(shift) if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
74 0 my %opt = @_;
75  
76 my @args = (
77 delete $opt{priority} || $self->default_priority,
78 delete $opt{delay} || $self->default_delay,
79 0 ▶ delete $opt{ttr} || $self->default_ttr,
Condition0%0/9
delete $opt{'priority'} || $self->default_priority
l!l&&r!l&&!r
000
delete $opt{'delay'} || $self->default_delay
l!l&&r!l&&!r
000
delete $opt{'ttr'} || $self->default_ttr
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opt{'priority'} || $self->default_priority
delete $opt{'priority'} $self->default_priority
MC/DC0%0/2
delete $opt{'delay'} || $self->default_delay
delete $opt{'delay'} $self->default_delay
MC/DC0%0/2
delete $opt{'ttr'} || $self->default_ttr
delete $opt{'ttr'} $self->default_ttr
Truth table0%0/3
A || B
A= delete $opt{'priority'}B= $self->default_priority
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= delete $opt{'delay'}B= $self->default_delay
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= delete $opt{'ttr'}B= $self->default_ttr
ABresult
1X1
011
000
80 );
81 0 ▶ $job //= delete $opt{raw_data} // $self->encoder->(delete $opt{data});
Condition0%0/6
$job //= delete $opt{"raw_data"} // $self->encoder->(delete $opt{"data"})
l!l&&r!l&&!r
000
delete $opt{"raw_data"} // $self->encoder->(delete $opt{"data"})
l!l&&r!l&&!r
000
MC/DC0%0/3
$job //= delete $opt{"raw_data"} // $self->encoder->(delete $opt{"data"})
$job delete $opt{"raw_data"} $self->encoder->(delete $opt{"data"})
Truth table0%0/4
A //= B // C
A= $jobB= delete $opt{"raw_data"}C= $self->encoder->(delete $opt{"data"})
ABCresult
1XX1
01X1
0011
0000
82 0 ▶ croak 'Too much job data' if exists $opt{raw_data} or exists $opt{data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"raw_data"} or exists $opt{"data"} 0 0 0
Condition0%0/3
exists $opt{"raw_data"} or exists $opt{"data"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $opt{"raw_data"} or exists $opt{"data"}
exists $opt{"raw_data"} exists $opt{"data"}
Truth table0%0/3
A or B
A= exists $opt{"raw_data"}B= exists $opt{"data"}
ABresult
1X1
011
000
83 0 ▶ croak 'Unknown options to "put": ' . join ', ', sort keys %opt if scalar keys %opt;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opt 0 0 0
84 0 $self->_send(put => (map int, @args), length($job) . $NL . $job);
85 }
86  
87 # TODO: raw/decode on other job-fetching commans
88 0 ▶sub reserve {
Subroutine reserve: not called CC=4 SCAR=30.0
Pod undocumented
89 0 my $self = shift;
90 0 my %opt = @_;
91 0 my $no_decode = delete $opt{asis};
92 0 my $as_raw = delete $opt{raw};
93 # TODO: Warn about illogical combinations?
94 0 my $timeout = delete $opt{timeout};
95 0 ▶ croak 'Unknown options to "reserve": ' . join ', ', sort keys %opt if scalar keys %opt;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opt 0 0 0
96  
97 0 ▶ my @command = defined $timeout ? ('reserve-with-timeout', $timeout) : ('reserve');
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 0 0 0
98 0 ▶ return $self->_send(@command) if $as_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $as_raw 0 0 0
99 0 ▶ $self->_send(@command)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 Future->done($_[0], $self->decoder($no_decode)->($_[1]), @_[2..$#_]);
101 0 });
102 }
103  
104 0 ▶sub reserve_with_timeout { $_[0]->reserve(timeout => $_[1], @_[2..$#_]) }
Subroutine reserve_with_timeout: not called CC=1 SCAR=6.9
Pod undocumented
105  
106 # TODO: Make void context useful
107 3 for my $command (keys %COMMAND) {
108 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 72 my $sub = $command =~ s/-/_/gr;
110 72 ▶ next if __PACKAGE__->can($sub);
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__->can($sub) 9 63 72
111 0 ▶ *$sub = sub { $_[0]->_send($command => @_[1..$#_]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
112 }
113  
114 3 1;
↑ Summary Beanstalk/Receive.pm »