Redis/Multi.pm

filter: Statementstmt 34.0% 30 / 88 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 56.2% 9 / 16 Podpod 33.3% 1 / 3 totaltotal 28.9% 40 / 138 CC 15 SCAR 31.9
CC
15
Cov
26%
exec
72.0
DESTROY
20.0
__ANON__
12.0
CRAP 24.2
« Redis.pm ↑ Summary Redis/Protocol.pm »
1 package Net::Async::Redis::Multi;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '2.004_001'; # TRIAL VERSION
7  
8 =head1 NAME
9  
10 Net::Async::Redis::Multi - represents multiple operations in a single Redis transaction
11  
12 =head1 DESCRIPTION
13  
14 Instances are generated by L<Net::Async::Redis/multi>.
15  
16 =cut
17  
18 11 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use Syntax::Keyword::Dynamically;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new {
25 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
26 0 ▶ weaken($args{redis} // die 'Must be provided a Net::Async::Redis instance');
Condition0%0/2
$args{"redis"} // die("Must be provided a Net::Async::Redis instance")
l!l
00
MC/DC0%0/1
$args{"redis"} // die("Must be provided a Net::Async::Redis instance")
$args{"redis"}
Truth table0%0/2
A
A= $args{"redis"}
Aresult
11
00
27 0 bless \%args, $class;
28 }
29  
30 0 ▶async sub exec {
Subroutine exec: not called CC=8 SCAR=42.8
Pod undocumented
31 0 my ($self, $code) = @_;
32 0 my $f = $self->$code;
33 0 ▶ $f->retain if blessed($f) and $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($f) and $f->isa("Future") 0 0 0
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
34  
35 try {
36 0 my ($exec_result) = await $self->redis->exec;
37 0 my @reply = $exec_result->@*;
38 0 my $success = 0;
39 0 my $failure = 0;
40 0 ▶ while(@reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reply) 0 0 0
41 try {
42 0 my $reply = shift @reply;
43 0 ▶ my $queued = shift @{$self->{queued_requests}} or die 'invalid queued request';
Branch: 1 branch
BranchTrueFalseTotal
my $queued = shift @{$$self{"queued_requests"};} or die "invalid queued request" 0 0 0
44 0 ▶ $queued->done($reply) unless $queued->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $queued->is_ready 0 0 0
45 0 ++$success
46 0 } catch {
47 0 $log->warnf("Failure during transaction: %s", $@);
48 0 ++$failure
49 }
50 }
51 0 return $success, $failure;
52 0 } catch {
53 0 my $err = $@;
54 0 for my $queued (splice @{$self->{queued_requests}}) {
55 try {
56 0 ▶ $queued->fail("Transaction failed", redis => 'transaction_failure') unless $queued->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $queued->is_ready 0 0 0
57 0 } catch {
58 0 $log->warnf("Failure during transaction: %s", $@);
59 }
60 }
61 0 die $@;
62 }
63 11 }
64  
65 =head2 redis
66  
67 Accessor for the L<Net::Async::Redis> instance.
68  
69 =cut
70  
71 0 ▶sub redis { shift->{redis} }
Subroutine redis: not called CC=1 SCAR=6.9
Pod documented
72  
73 11 ▶use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 sub AUTOLOAD {
76 0 ▶ my ($method) = our $AUTOLOAD =~ m{::([^:]+)$};
Subroutine AUTOLOAD: not called CC=2 SCAR=15.9
77 0 ▶ die "Unknown method $method" unless Net::Async::Redis::Commands->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless "Net::Async::Redis::Commands"->can($method) 0 0 0
78  
79 0 ▶ my $code = async sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
80 0 my ($self, @args) = @_;
81 0 my $f = $self->redis->future->set_label($method);
82 0 push @{$self->{queued_requests}}, $f;
83 0 my $ff = do {
84 # $self->redis->{_is_multi} //= 0;
85 0 dynamically $self->redis->{_is_multi} = 1;
86 0 $self->redis->$method(@args);
87 };
88 0 my ($resp) = await $ff;
89 0 ▶ if($resp eq 'QUEUED') {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp eq 'QUEUED') { } 0 0 0
90 0 return await $f;
91 } else {
92 # my $addr = refaddr($f);
93 # extract_by { $addr == refaddr($_) } @{$self->{queued_requests}};
94 0 $f->fail($resp);
95 0 ▶ die $resp unless $resp eq 'QUEUED';
Branch: 1 branch
BranchTrueFalseTotal
unless $resp eq "QUEUED" 0 0 0
96 }
97 0 };
98 0 set_subname $method => $code;
99 11 ▶ { no strict 'refs'; *$method = sub { $code->(@_)->retain } }
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
100 0 $code->(@_)->retain;
101 }
102  
103 sub DESTROY {
104 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=4 SCAR=30.0
105 0 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT' or not $self->{queued_requests};
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" or not $self->{"queued_requests"} 0 0 0
Condition0%0/3
${^GLOBAL_PHASE} eq "DESTRUCT" or not $self->{"queued_requests"}
l!l&&r!l&&!r
000
MC/DC0%0/2
${^GLOBAL_PHASE} eq "DESTRUCT" or not $self->{"queued_requests"}
${^GLOBAL_PHASE} eq "DESTRUCT" not $self->{"queued_requests"}
Truth table0%0/3
A or not(B)
A= ${^GLOBAL_PHASE} eq "DESTRUCT"B= not $self->{"queued_requests"}
ABresult
1X1
011
000
106  
107 0 for my $queued (splice @{$self->{queued_requests}}) {
108 try {
109 0 $queued->cancel;
110 0 } catch {
111 0 $log->warnf("Failure during cleanup: %s", $@);
112 }
113 }
114 }
115  
116 11 1;
117  
118 __END__
« Redis.pm ↑ Summary Redis/Protocol.pm »