blib/lib/Unblock/HTTP3/Body/Stream.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 65.9% 29 / 44 Conditioncond 52.3% 11 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 78.8% 149 / 189 CC 31 SCAR 17.7
CC
31
Cov
83%
_new
12.5
_configure
7.7
_drain
5.0
CRAP 5.9
« blib/lib/Unblock/HTTP3/Capsule.pm ↑ Summary blib/lib/Unblock/HTTP3/Client.pm »
1 package Unblock::HTTP3::Body::Stream;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Unblock::HTTP3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Unblock::HTTP3::_Bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = '0.12';
13  
14 21 ▶sub _new {
Subroutine _new: called CC=10 SCAR=25.3
15 21 my ($class, $transaction, $kind, %option) = @_;
16  
17 21 ▶ croak 'body stream kind must be request or response'
Branch: 1 branch
BranchTrueFalseTotal
if $kind ne "request" and $kind ne "response" 0 21 21
Condition66%2/3
$kind ne "request" and $kind ne "response"
!ll&&!rl&&r
1290
MC/DC0%0/2
$kind ne "request" and $kind ne "response"
$kind ne "request" $kind ne "response"
Truth table66%2/3
A and B
A= $kind ne "request"B= $kind ne "response"
ABresult
0X0
100
111
18 if $kind ne 'request' && $kind ne 'response';
19 21 ▶ croak 'body stream requires a Unblock::HTTP3::Transaction'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction") 0 21 21
Condition33%1/3
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
!ll&&!rl&&r
0021
MC/DC0%0/2
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
builtin::blessed($transaction) $transaction->isa("Unblock::HTTP3::Transaction")
Truth table33%1/3
A and B
A= builtin::blessed($transaction)B= $transaction->isa("Unblock::HTTP3::Transaction")
ABresult
0X0
100
111
20 unless blessed($transaction)
21 && $transaction->isa('Unblock::HTTP3::Transaction');
22  
23 21 my $on_drain = delete $option{on_drain};
24 21 my $on_cancel = delete $option{on_cancel};
25 21 my $operation = $kind . '_body';
26  
27 21 ▶ croak "$operation(): on_drain must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_drain and ref $on_drain ne "CODE" 0 21 21
Condition66%2/3
defined $on_drain and ref $on_drain ne "CODE"
!ll&&!rl&&r
1920
MC/DC0%0/2
defined $on_drain and ref $on_drain ne "CODE"
defined $on_drain ref $on_drain ne "CODE"
Truth table66%2/3
A and B
A= defined $on_drainB= ref $on_drain ne "CODE"
ABresult
0X0
100
111
28 if defined($on_drain) && ref($on_drain) ne 'CODE';
29 21 ▶ croak "$operation(): on_cancel must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_cancel and ref $on_cancel ne "CODE" 0 21 21
Condition66%2/3
defined $on_cancel and ref $on_cancel ne "CODE"
!ll&&!rl&&r
1830
MC/DC0%0/2
defined $on_cancel and ref $on_cancel ne "CODE"
defined $on_cancel ref $on_cancel ne "CODE"
Truth table66%2/3
A and B
A= defined $on_cancelB= ref $on_cancel ne "CODE"
ABresult
0X0
100
111
30 if defined($on_cancel) && ref($on_cancel) ne 'CODE';
31 21 ▶ croak "$operation(): unknown option: " . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 21 21
32 if %option;
33  
34 21 my $self = bless {
35 transaction => $transaction,
36 kind => $kind,
37 on_drain => $on_drain,
38 on_cancel => $on_cancel,
39 complete => 0,
40 cancelled => 0,
41 flow_blocked => 0,
42 }, $class;
43  
44 21 weaken($self->{transaction});
45  
46 21 return $self;
47 }
48  
49 1 ▶sub _configure {
Subroutine _configure: called CC=7 SCAR=20.4
50 1 my ($self, %option) = @_;
51  
52 1 my $operation = $self->{kind} . '_body';
53  
54 1 for my $name (qw(on_drain on_cancel)) {
55 2 ▶ next unless exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$name} 1 1 2
56  
57 1 my $callback = delete $option{$name};
58  
59 1 ▶ croak "$operation(): $name must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback and ref $callback ne "CODE" 0 1 1
Condition33%1/3
defined $callback and ref $callback ne "CODE"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback ref $callback ne "CODE"
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
ABresult
0X0
100
111
60 if defined($callback) && ref($callback) ne 'CODE';
61 croak "$operation(): $name is already configured"
62 1 ▶ if defined($self->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$name} 0 1 1
63  
64 1 $self->{$name} = $callback;
65 }
66  
67 1 ▶ croak "$operation(): unknown option: " . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 1 1
68 if %option;
69  
70 1 return $self;
71 }
72  
73 36 ▶sub is_complete {
Subroutine is_complete: called CC=2 SCAR=6.9
Pod documented
74 36 my ($self) = @_;
75 36 ▶ return $self->{complete} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'complete'} ? : 25 11 36
76 }
77  
78 1 ▶sub is_cancelled {
Subroutine is_cancelled: called CC=2 SCAR=7.2
Pod documented
79 1 my ($self) = @_;
80 1 ▶ return $self->{cancelled} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cancelled'} ? : 1 0 1
81 }
82  
83 35 ▶sub _assert_writable {
Subroutine _assert_writable: called CC=4 SCAR=14.6
84 35 my ($self, $operation) = @_;
85  
86 croak "$operation(): streaming body is already complete"
87 35 ▶ if $self->{complete};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"complete"} 0 35 35
88 croak "$operation(): streaming body was cancelled"
89 35 ▶ if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 35 35
90  
91 my $transaction = $self->{transaction}
92 35 ▶ or croak "$operation(): HTTP/3 Transaction is no longer available";
Branch: 1 branch
BranchTrueFalseTotal
my $transaction = $self->{"transaction"} or croak("$operation(): HTTP/3 Transaction is no longer available") 35 0 35
93  
94 35 return $transaction;
95 }
96  
97 18 ▶sub write {
Subroutine write: called CC=3 SCAR=11.1
Pod documented
98 18 my ($self, $bytes) = @_;
99  
100 18 my $operation = $self->{kind} . '_body->write';
101 18 my $transaction = $self->_assert_writable($operation);
102  
103 18 $bytes = Unblock::HTTP3::_Bytes::byte_string('body', $bytes);
104  
105 18 $self->{flow_blocked} = 1;
106  
107 18 my ($can_continue, $ok, $error);
108  
109 {
110 18 local $@;
111 18 $ok = eval {
112 $can_continue = $transaction->_write_body(
113 $self->{kind},
114 18 $bytes,
115 0,
116 $operation,
117 );
118 18 1;
119 };
120 18 $error = $@;
121 }
122  
123 18 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 18 18
124 0 $self->{flow_blocked} = 0;
125 0 die $error;
126 }
127  
128 18 ▶ $self->{flow_blocked} = 0 if $can_continue;
Branch: 1 branch
BranchTrueFalseTotal
if $can_continue 16 2 18
129 18 return $can_continue;
130 }
131  
132 17 ▶sub complete {
Subroutine complete: called CC=2 SCAR=6.9
Pod documented
133 17 my ($self, $bytes) = @_;
134 17 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 10 7 17
135  
136 17 my $operation = $self->{kind} . '_body->complete';
137 17 my $transaction = $self->_assert_writable($operation);
138  
139 17 $bytes = Unblock::HTTP3::_Bytes::byte_string('body', $bytes);
140  
141 $transaction->_write_body(
142 $self->{kind},
143 17 $bytes,
144 1,
145 $operation,
146 );
147  
148 17 $self->{complete} = 1;
149 17 $self->{flow_blocked} = 0;
150  
151 17 return $self;
152 }
153  
154 75 ▶sub _drain {
Subroutine _drain: called CC=5 SCAR=16.2
155 75 my ($self) = @_;
156  
157 75 ▶ return if $self->{complete} || $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"complete"} or $self->{"cancelled"} 37 38 75
Condition66%2/3
$self->{"complete"} or $self->{"cancelled"}
l!l&&r!l&&!r
37038
MC/DC50%1/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"} $self->{"cancelled"}
Truth table66%2/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
ABresult
1X1
011
000
158 38 ▶ return unless $self->{flow_blocked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"flow_blocked"} 37 1 38
159  
160 1 $self->{flow_blocked} = 0;
161  
162 1 ▶ my $callback = $self->{on_drain} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $self->{"on_drain"} or return 1 0 1
163 1 $callback->($self);
164  
165 1 return;
166 }
167  
168 7 ▶sub _cancel {
Subroutine _cancel: called CC=4 SCAR=14.2
169 7 my ($self) = @_;
170  
171 7 ▶ return if $self->{complete} || $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"complete"} or $self->{"cancelled"} 0 7 7
Condition33%1/3
$self->{"complete"} or $self->{"cancelled"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"} $self->{"cancelled"}
Truth table33%1/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
ABresult
1X1
011
000
172  
173 7 $self->{cancelled} = 1;
174 7 $self->{flow_blocked} = 0;
175  
176 7 ▶ my $callback = $self->{on_cancel} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $self->{"on_cancel"} or return 1 6 7
177 1 $callback->($self);
178  
179 1 return;
180 }
181  
182 5 1;
183  
184 __END__
« blib/lib/Unblock/HTTP3/Capsule.pm ↑ Summary blib/lib/Unblock/HTTP3/Client.pm »