Waiter/Client.pm

filter: Statementstmt 33.7% 30 / 89 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 44.4% 8 / 18 Podpod 0.0% 0 / 8 totaltotal 21.9% 38 / 173 CC 25 SCAR 32.4
CC
25
Cov
22%
wait
56.0
match_pathAll
42.0
match_pathAny
42.0
CRAP 25.6
↑ Summary Waiter.pm »
1 package PawsX::Waiter::Client;
2  
3 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use feature qw(postderef say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings qw(experimental::postderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Jmespath;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Structure::Util qw(unbless);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use PawsX::Waiter::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has client => (
16 is => 'ro',
17 required => 1,
18 weak_ref => 1,
19 );
20  
21 2 has delay => (
22 is => 'rw',
23 isa => 'Int',
24 required => 1,
25 );
26  
27 2 has maxAttempts => (
28 is => 'rw',
29 isa => 'Int',
30 required => 1,
31 );
32  
33 2 has operation => (
34 is => 'ro',
35 isa => 'Str',
36 required => 1,
37 );
38  
39 2 has acceptors => (
40 is => 'ro',
41 isa => 'ArrayRef[HashRef]',
42 required => 1,
43 );
44  
45 2 has 'beforeWait' => (
46 traits => ['Code'],
47 is => 'rw',
48 isa => 'CodeRef',
49 predicate => 'has_beforeWait',
50 handles => {
51 _beforeWait => 'execute_method',
52 },
53 );
54  
55 0 ▶sub operation_request {
Subroutine operation_request: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my ( $self, $options ) = @_;
57  
58 0 my $func = $self->operation;
59 0 my $response = {};
60  
61 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 $response = $self->client->$func( $options->%* );
63 }
64 0 ▶ catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
65 0 my $error = $_;
66 0 ▶ if ( blessed($error) and $error->isa('Paws::Exception') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Paws::Exception')) { } 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("Paws::Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Paws::Exception")
builtin::blessed($error) $error->isa("Paws::Exception")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("Paws::Exception")
ABresult
0X0
100
111
67 0 $response = {
68 code => $error->code,
69 request_id => $error->request_id,
70 http_status => $error->http_status,
71 message => $error->message,
72 };
73 }
74 else {
75 0 PawsX::Waiter::Exception->throw(
76 name => $self->operation,
77 reason => $error,
78 last_response => $response,
79 );
80 }
81 0 };
82 0 ▶ return blessed($response) ? unbless($response) : $response;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($response) ? : 0 0 0
83 }
84  
85 0 ▶sub wait {
Subroutine wait: not called CC=7 SCAR=40.3
Pod undocumented
86 0 my ( $self, $options ) = @_;
87  
88 0 my $current_state = 'waiting';
89 0 my $num_attempts = 0;
90  
91 0 while (1) {
92
93 0 my $response = $self->operation_request($options);
94 0 $num_attempts++;
95
96 ACCEPTOR:
97 0 foreach my $acceptor ( $self->acceptors->@* ) {
98 0 ▶ if ( my $status = $self->matcher( $acceptor, $response ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $self->matcher($acceptor, $response)) 0 0 0
99 0 $current_state = $acceptor->{'state'};
100 0 last ACCEPTOR;
101 }
102 }
103  
104 0 ▶ if ( $current_state eq 'success' ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($current_state eq "success") 0 0 0
105 0 ▶ if ( $current_state eq 'failure' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_state eq "failure") 0 0 0
106 0 PawsX::Waiter::Exception->throw(
107 message => 'Waiter encountered a terminal failure state',
108 last_response => $response,
109 );
110 }
111 0 ▶ if ( $num_attempts >= $self->maxAttempts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_attempts >= $self->maxAttempts) 0 0 0
112 0 PawsX::Waiter::Exception::TimeOut->throw(
113 message => 'Max attempts exceeded',
114 last_response => $response,
115 );
116 }
117  
118 0 ▶ $self->_beforeWait($num_attempts, $response) if $self->has_beforeWait;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_beforeWait 0 0 0
119  
120 0 sleep( $self->delay );
121 }
122 }
123  
124 0 ▶sub matcher {
Subroutine matcher: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my ( $self, $acceptor, $response ) = @_;
126  
127 0 my $func = "match_" . $acceptor->{'matcher'};
128  
129 0 return $self->$func( $acceptor, $response );
130 }
131  
132 0 ▶sub match_path {
Subroutine match_path: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my ( $self, $acceptor, $response ) = @_;
134  
135 0 ▶ if ( 'HASH' eq ref $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $response) { } 0 0 0
136 my $ret =
137 ( Jmespath->search( $acceptor->{'argument'}, $response ) eq
138 0 ▶ $acceptor->{'expected'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Jmespath'->search($acceptor->{'argument'}, $response) eq $acceptor->{'expected'} ? : 0 0 0
139 0 return $ret;
140 }
141 else {
142 0 return 0;
143 }
144 }
145  
146 0 ▶sub match_error {
Subroutine match_error: not called CC=4 SCAR=30.0
Pod undocumented
147 0 my ( $self, $acceptor, $response ) = @_;
148 0 ▶ if ( 'HASH' eq ref $response && exists $response->{'code'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $response and exists $response->{'code'}) { } 0 0 0
Condition0%0/3
"HASH" eq ref $response and exists $response->{"code"}
!ll&&!rl&&r
000
MC/DC0%0/2
"HASH" eq ref $response and exists $response->{"code"}
"HASH" eq ref $response exists $response->{"code"}
Truth table0%0/3
A and B
A= "HASH" eq ref $responseB= exists $response->{"code"}
ABresult
0X0
100
111
149 my $ret =
150 0 ▶ ( $response->{'code'} eq $acceptor->{'expected'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$response->{'code'} eq $acceptor->{'expected'} ? : 0 0 0
151 0 return $ret;
152 }
153 else {
154 0 return 0;
155 }
156 }
157  
158 0 ▶sub match_pathAll {
Subroutine match_pathAll: not called CC=6 SCAR=37.4
Pod undocumented
159 0 my ( $self, $acceptor, $response ) = @_;
160  
161 0 my $expression = Jmespath->compile( $acceptor->{'argument'} );
162 0 my $result = $expression->search($response);
163  
164 0 ▶ unless ( ref $result && 'ARRAY' eq ref $result
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result and "ARRAY" eq ref $result and length $result >= 1) 0 0 0
Condition0%0/6
ref $result and "ARRAY" eq ref $result and length $result >= 1
!ll&&!rl&&r
000
ref $result and "ARRAY" eq ref $result
!ll&&!rl&&r
000
MC/DC0%0/3
ref $result and "ARRAY" eq ref $result and length $result >= 1
ref $result "ARRAY" eq ref $result length $result >= 1
Truth table0%0/4
A and B and C
A= ref $resultB= "ARRAY" eq ref $resultC= length $result >= 1
ABCresult
0XX0
10X0
1100
1111
165 and length $result >= 1 )
166 {
167 0 return 0;
168 }
169  
170 0 for my $element ( $result->@* ) {
171 0 ▶ return 0 unless $element eq $acceptor->{'expected'};
Branch: 1 branch
BranchTrueFalseTotal
unless $element eq $acceptor->{"expected"} 0 0 0
172 }
173  
174 0 return 1;
175 }
176  
177 0 ▶sub match_pathAny {
Subroutine match_pathAny: not called CC=6 SCAR=37.4
Pod undocumented
178 0 my ( $self, $acceptor, $response ) = @_;
179  
180 0 my $expression = Jmespath->compile( $acceptor->{'argument'} );
181 0 my $result = $expression->search($response);
182  
183 0 ▶ unless ( ref $result && 'ARRAY' eq ref $result
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result and "ARRAY" eq ref $result and length $result >= 1) 0 0 0
Condition0%0/6
ref $result and "ARRAY" eq ref $result and length $result >= 1
!ll&&!rl&&r
000
ref $result and "ARRAY" eq ref $result
!ll&&!rl&&r
000
MC/DC0%0/3
ref $result and "ARRAY" eq ref $result and length $result >= 1
ref $result "ARRAY" eq ref $result length $result >= 1
Truth table0%0/4
A and B and C
A= ref $resultB= "ARRAY" eq ref $resultC= length $result >= 1
ABCresult
0XX0
10X0
1100
1111
184 and length $result >= 1 )
185 {
186 0 return 0;
187 }
188  
189 0 for my $element ( $result->@* ) {
190 0 ▶ return 1 if $element eq $acceptor->{'expected'};
Branch: 1 branch
BranchTrueFalseTotal
if $element eq $acceptor->{"expected"} 0 0 0
191 }
192  
193 0 return 0;
194 }
195  
196 0 ▶sub match_status {
Subroutine match_status: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my ( $self, $acceptor, $response ) = @_;
198 0 return $response->{'http_status'} == $acceptor->{'expected'};
199 }
200  
201 2 1;
↑ Summary Waiter.pm »