lib/ForgeOps/Tracker/DeliveryStatus.pm

filter: Statementstmt 41.8% 18 / 43 Branchbran 20.8% 5 / 24 Conditioncond 40.7% 11 / 27 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 4 totaltotal 35.5% 43 / 121 CC 22 SCAR 32.0
CC
22
Cov
36%
from
56.0
of_client
30.0
settle
20.0
CRAP 24.6
« blib/lib/ForgeOps/Tracker/PerformanceFlusher.pm ↑ Summary blib/lib/ForgeOps/Tracker/Integrations/Dancer2.pm »
1 package ForgeOps::Tracker::DeliveryStatus;
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 # What became of one delivery attempt. A bare true/false could not tell "the server will never take
7 # this payload" apart from "try again later", so a batch a plan or a validation rule refused was
8 # re-sent at every flush for as long as the process lived. Ported from
9 # gems/forge_ops_tracker/lib/forge_ops_tracker/delivery_status.rb.
10 #
11 # Client's own deliver* methods still answer 1 or 0 (1 only for DELIVERED); the status of the attempt
12 # behind that answer is Client->last_status.
13 11 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 DELIVERED => 'delivered', # any 2xx
15 REJECTED => 'rejected', # a 4xx other than 408 and 429: the same payload can only fail again
16 FAILED => 'failed', # a network error, a timeout, a 5xx, a 408 or a 429: worth another attempt
17 11 };
18  
19 # After this many flushes in a row that failed for now, a batch is dropped rather than kept for yet
20 # another attempt, so an outage can never pin the same data in memory for the life of the process.
21 11 ▶use constant MAX_FAILED_FLUSHES => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # HTTP::Tiny reports a network error or a timeout as its own 599, which lands in FAILED like any 5xx.
24 16 ▶sub for_code {
Subroutine for_code: called CC=9 SCAR=22.6
Pod undocumented
25 16 my ($code) = @_;
26 16 ▶ return FAILED unless defined $code && $code =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and $code =~ /\A\d+\z/ 0 16 16
Condition33%1/3
defined $code and $code =~ /\A\d+\z/
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $code and $code =~ /\A\d+\z/
defined $code $code =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\A\d+\z/
ABresult
0X0
100
111
27 16 ▶ return DELIVERED if $code >= 200 && $code < 300;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 200 and $code < 300 3 13 16
Condition66%2/3
$code >= 200 and $code < 300
!ll&&!rl&&r
0133
MC/DC50%1/2
$code >= 200 and $code < 300
$code >= 200 $code < 300
Truth table66%2/3
A and B
A= $code >= 200B= $code < 300
ABresult
0X0
100
111
28 13 ▶ return REJECTED if $code >= 400 && $code < 500 && $code != 408 && $code != 429;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 400 and $code < 500 and $code != 408 and $code != 429 7 6 13
Condition88%8/9
$code >= 400 and $code < 500 and $code != 408 and $code != 429
!ll&&!rl&&r
517
$code >= 400 and $code < 500 and $code != 408
!ll&&!rl&&r
418
$code >= 400 and $code < 500
!ll&&!rl&&r
049
MC/DC75%3/4
$code >= 400 and $code < 500 and $code != 408 and $code != 429
$code >= 400 $code < 500 $code != 408 $code != 429
Truth table80%4/5
A and B and C and D
A= $code >= 400B= $code < 500C= $code != 408D= $code != 429
ABCDresult
0XXX0
10XX0
110X0
11100
11111
29 6 return FAILED;
30 }
31  
32 # Reads whatever a deliver callback handed back. A status passes through; anything else is read the
33 # way the old boolean contract was (true delivered, false failed for now), so a callback that still
34 # answers 1 or 0 keeps working.
35 0 ▶sub from {
Subroutine from: not called CC=7 SCAR=40.3
Pod undocumented
36 0 my ($result) = @_;
37 0 ▶ if (defined $result && !ref($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and not ref $result) 0 0 0
Condition0%0/3
defined $result and not ref $result
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result and not ref $result
defined $result not ref $result
Truth table0%0/3
A and not(B)
A= defined $resultB= not ref $result
ABresult
0X0
100
111
38 0 ▶ return $result if $result eq DELIVERED || $result eq REJECTED || $result eq FAILED;
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "delivered" or $result eq "rejected" or $result eq "failed" 0 0 0
Condition0%0/6
$result eq "delivered" or $result eq "rejected" or $result eq "failed"
l!l&&r!l&&!r
000
$result eq "delivered" or $result eq "rejected"
l!l&&r!l&&!r
000
MC/DC0%0/3
$result eq "delivered" or $result eq "rejected" or $result eq "failed"
$result eq "delivered" $result eq "rejected" $result eq "failed"
Truth table0%0/4
A or B or C
A= $result eq "delivered"B= $result eq "rejected"C= $result eq "failed"
ABCresult
1XX1
01X1
0011
0000
39 }
40 0 ▶ return $result ? DELIVERED : FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
41 }
42  
43 # The status behind a client's 1/0 answer: its own last_status when it has one (a test double, or a
44 # client written against the old contract, may not), otherwise failed for now.
45 0 ▶sub of_client {
Subroutine of_client: not called CC=5 SCAR=34.0
Pod undocumented
46 0 my ($client, $delivered) = @_;
47 0 ▶ return DELIVERED if $delivered;
Branch: 1 branch
BranchTrueFalseTotal
if $delivered 0 0 0
48 0 ▶ my $status = eval { $client->can('last_status') ? $client->last_status : undef };
Branch: 1 branch
BranchTrueFalseTotal
$client->can('last_status') ? : 0 0 0
49 0 ▶ return (defined $status && $status eq REJECTED) ? REJECTED : FAILED;
Branch: 1 branch
BranchTrueFalseTotal
defined $status && $status eq 'rejected' ? : 0 0 0
Condition0%0/3
defined $status and $status eq "rejected"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $status and $status eq "rejected"
defined $status $status eq "rejected"
Truth table0%0/3
A and B
A= defined $statusB= $status eq "rejected"
ABresult
0X0
100
111
50 }
51  
52 # settle(\$failed_flushes, $status): decides whether the batch a flush just sent is done with.
53 # Returns (1) for a delivered batch, (1, $why) for one to drop ($why is what to log), and (0) for
54 # one to keep for the next flush. $failed_flushes is the caller's own threads::shared counter of
55 # flushes in a row that failed, and the caller holds its lock: a flush can run on the background
56 # thread or on the main one.
57 0 ▶sub settle {
Subroutine settle: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my ($failed_flushes, $status) = @_;
59  
60 0 ▶ if ($status eq DELIVERED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "delivered") 0 0 0
61 0 ${$failed_flushes} = 0;
62 0 return (1);
63 }
64 0 ▶ if ($status eq REJECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "rejected") 0 0 0
65 0 ${$failed_flushes} = 0;
66 0 return (1, 'the server rejected them and will not accept a retry');
67 }
68  
69 0 ${$failed_flushes}++;
70 0 ▶ return (0) if ${$failed_flushes} < MAX_FAILED_FLUSHES;
Branch: 1 branch
BranchTrueFalseTotal
if ${$failed_flushes;} < 5 0 0 0
71 0 ${$failed_flushes} = 0;
72 0 return (1, MAX_FAILED_FLUSHES . ' flushes in a row failed');
73 }
74  
75 11 1;
« blib/lib/ForgeOps/Tracker/PerformanceFlusher.pm ↑ Summary blib/lib/ForgeOps/Tracker/Integrations/Dancer2.pm »