blib/lib/Linux/Event/Stream/_Descriptor.pm

filter: Statementstmt 97.9% 94 / 96 Branchbran 87.5% 70 / 80 Conditioncond 72.0% 18 / 25 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 86.7% 202 / 233 CC 55 SCAR 27.3
CC
55
Cov
91%
for_class
23.0
_stream_options_for
20.3
declare_tls
6.9
CRAP 15.4
« bench/run-stream-lifecycle-bench.pl ↑ Summary blib/lib/Linux/Event/Framer/LengthPrefix.pm »
1 package Linux::Event::Stream::_Descriptor;
2 87 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 87 our $VERSION = '0.105';
7  
8 87 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use mro ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 87 ▶use Linux::Event::_SocketConfig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Private descriptor storage is isolated here so Stream.pm can remain the
14 # readable connection-lifecycle implementation.
15 87 my %FRAMER_DEFINITION;
16 my %TLS_DEFINITION;
17 87 my %CLASS_DESCRIPTOR;
18  
19 90 ▶sub declare_framer ($base, $target, $definition) {
Subroutine declare_framer: called CC=5 SCAR=17.2
Pod undocumented
20 90 ▶ croak 'a framer may be declared only for a Linux::Event::Stream subclass'
Branch: 1 branch
BranchTrueFalseTotal
if $target eq $base or not $target->isa($base) 0 90 90
Condition33%1/3
$target eq $base or not $target->isa($base)
l!l&&r!l&&!r
0090
MC/DC0%0/2
$target eq $base or not $target->isa($base)
$target eq $base not $target->isa($base)
Truth table33%1/3
A or not(B)
A= $target eq $baseB= not $target->isa($base)
ABresult
1X1
011
000
21 if $target eq $base || !$target->isa($base);
22 croak "$target already has a Stream descriptor"
23 90 ▶ if exists $CLASS_DESCRIPTOR{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $CLASS_DESCRIPTOR{$target} 0 90 90
24 croak "$target already declares a framer"
25 90 ▶ if exists $FRAMER_DEFINITION{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $FRAMER_DEFINITION{$target} 1 89 90
26 89 $FRAMER_DEFINITION{$target} = $definition;
27 89 return;
28 }
29  
30 197 ▶sub _framer_for ($class) {
Subroutine _framer_for: called CC=3 SCAR=11.0
31 197 for my $package (@{ mro::get_linear_isa($class) }) {
32 return $FRAMER_DEFINITION{$package}
33 316 ▶ if exists $FRAMER_DEFINITION{$package};
Branch: 1 branch
BranchTrueFalseTotal
if exists $FRAMER_DEFINITION{$package} 85 231 316
34 }
35 112 return undef;
36 }
37  
38 8 ▶sub declare_tls ($base, $target, $definition) {
Subroutine declare_tls: called CC=6 SCAR=19.3
Pod undocumented
39 8 ▶ croak 'TLS may be declared only for a Linux::Event::Stream subclass'
Branch: 1 branch
BranchTrueFalseTotal
if $target eq $base or not $target->isa($base) 0 8 8
Condition33%1/3
$target eq $base or not $target->isa($base)
l!l&&r!l&&!r
008
MC/DC0%0/2
$target eq $base or not $target->isa($base)
$target eq $base not $target->isa($base)
Truth table33%1/3
A or not(B)
A= $target eq $baseB= not $target->isa($base)
ABresult
1X1
011
000
40 if $target eq $base || !$target->isa($base);
41 croak "$target already has a Stream descriptor"
42 8 ▶ if exists $CLASS_DESCRIPTOR{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $CLASS_DESCRIPTOR{$target} 0 8 8
43 croak "$target already declares TLS"
44 8 ▶ if exists $TLS_DEFINITION{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $TLS_DEFINITION{$target} 1 7 8
45 7 ▶ croak 'TLS declaration must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $definition ne "HASH" 0 7 7
46 if ref($definition) ne 'HASH';
47 7 $TLS_DEFINITION{$target} = $definition;
48 7 return;
49 }
50  
51 197 ▶sub _tls_for ($class) {
Subroutine _tls_for: called CC=3 SCAR=11.0
52 197 for my $package (@{ mro::get_linear_isa($class) }) {
53 return $TLS_DEFINITION{$package}
54 405 ▶ if exists $TLS_DEFINITION{$package};
Branch: 1 branch
BranchTrueFalseTotal
if exists $TLS_DEFINITION{$package} 7 398 405
55 }
56 190 return undef;
57 }
58  
59 197 ▶sub _stream_options_for ($class) {
Subroutine _stream_options_for: called CC=20 SCAR=30.1
60 my %option = (
61 high_watermark => 1_048_576,
62 low_watermark => 262_144,
63 max_pending_bytes => 0,
64 read_size => 65_536,
65 read_batch_bytes => 0,
66 message_batch_size => 0,
67 max_buffer => 8_388_608,
68 idle_timeout => 0,
69 read_timeout => 0,
70 write_timeout => 0,
71 197 map { $_ => undef } Linux::Event::_SocketConfig::names(),
72 );
73  
74 197 ▶ if (my $configure = $class->can('stream_options')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $configure = $class->can("stream_options")) 75 122 197
75 75 my @configured = $configure->($class);
76 75 my %configured;
77 75 ▶ if (@configured == 1 && ref($configured[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@configured == 1 and ref $configured[0] eq 'HASH') { } 1 74 75
Condition100%3/3
@configured == 1 and ref $configured[0] eq "HASH"
!ll&&!rl&&r
7311
MC/DC100%2/2
@configured == 1 and ref $configured[0] eq "HASH"
@configured == 1 ref $configured[0] eq "HASH"
Truth table100%3/3
A and B
A= @configured == 1B= ref $configured[0] eq "HASH"
ABresult
0X0
100
111
78 1 %configured = %{ $configured[0] };
79 } else {
80 74 ▶ croak "$class stream_options() returned an odd option list"
Branch: 1 branch
BranchTrueFalseTotal
if @configured % 2 1 73 74
81 if @configured % 2;
82 73 %configured = @configured;
83 }
84 74 my @unknown = grep { !exists $option{$_} } keys %configured;
85 74 ▶ croak "$class stream_options() returned unknown options: "
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 73 74
86 . join(', ', sort @unknown) if @unknown;
87 73 @option{keys %configured} = values %configured;
88 }
89  
90 croak "$class high_watermark must be a non-negative integer"
91 195 ▶ if $option{high_watermark} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"high_watermark"} =~ /\A\d+\z/u 0 195 195
92 croak "$class low_watermark must be a non-negative integer"
93 195 ▶ if $option{low_watermark} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"low_watermark"} =~ /\A\d+\z/u 0 195 195
94 croak "$class low_watermark must be <= high_watermark"
95 195 ▶ if $option{low_watermark} > $option{high_watermark};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"low_watermark"} > $option{"high_watermark"} 1 194 195
96 croak "$class max_pending_bytes must be a non-negative integer"
97 194 ▶ if $option{max_pending_bytes} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"max_pending_bytes"} =~ /\A\d+\z/u 1 193 194
98 croak "$class read_size must be a positive integer"
99 193 ▶ if $option{read_size} !~ /\A\d+\z/ || $option{read_size} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $option{"read_size"} =~ /\A\d+\z/u or $option{"read_size"} <= 0 1 192 193
Condition66%2/3
not $option{"read_size"} =~ /\A\d+\z/u or $option{"read_size"} <= 0
l!l&&r!l&&!r
01192
MC/DC50%1/2
not $option{"read_size"} =~ /\A\d+\z/u or $option{"read_size"} <= 0
not $option{"read_size"} =~ /\A\d+\z/u $option{"read_size"} <= 0
Truth table66%2/3
not(A) or B
A= not $option{"read_size"} =~ /\A\d+\z/uB= $option{"read_size"} <= 0
ABresult
1X1
011
000
100 croak "$class read_batch_bytes must be a non-negative integer"
101 192 ▶ if $option{read_batch_bytes} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"read_batch_bytes"} =~ /\A\d+\z/u 1 191 192
102 croak "$class message_batch_size must be a non-negative integer"
103 191 ▶ if $option{message_batch_size} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"message_batch_size"} =~ /\A\d+\z/u 1 190 191
104 croak "$class max_buffer must be a positive integer"
105 190 ▶ if $option{max_buffer} !~ /\A\d+\z/ || $option{max_buffer} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $option{"max_buffer"} =~ /\A\d+\z/u or $option{"max_buffer"} <= 0 0 190 190
Condition33%1/3
not $option{"max_buffer"} =~ /\A\d+\z/u or $option{"max_buffer"} <= 0
l!l&&r!l&&!r
00190
MC/DC0%0/2
not $option{"max_buffer"} =~ /\A\d+\z/u or $option{"max_buffer"} <= 0
not $option{"max_buffer"} =~ /\A\d+\z/u $option{"max_buffer"} <= 0
Truth table33%1/3
not(A) or B
A= not $option{"max_buffer"} =~ /\A\d+\z/uB= $option{"max_buffer"} <= 0
ABresult
1X1
011
000
106 190 for my $name (qw(idle_timeout read_timeout write_timeout)) {
107 $option{$name} = Linux::Event::Stream::_timeout_value(
108 568 $class, $name, $option{$name},
109 );
110 }
111 189 for my $name (Linux::Event::_SocketConfig::names()) {
112 $option{$name} = Linux::Event::_SocketConfig::normalize(
113 $class, $name, $option{$name},
114 1505 ▶ ) if defined $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $option{$name} 11 1494 1505
115 1504 ▶ delete $option{$name} if !defined $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{$name} 1494 10 1504
116 }
117  
118 188 return \%option;
119 }
120  
121 103031 ▶sub for_class ($class) {
Subroutine for_class: called CC=23 SCAR=31.4
Pod undocumented
122 103031 ▶ return $CLASS_DESCRIPTOR{$class} if exists $CLASS_DESCRIPTOR{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $CLASS_DESCRIPTOR{$class} 102833 198 103031
123 198 ▶ croak 'Linux::Event::Stream is a base class; construct a Stream subclass'
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Linux::Event::Stream" 1 197 198
124 if $class eq 'Linux::Event::Stream';
125 197 ▶ croak "$class is not a Linux::Event::Stream subclass"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Linux::Event::Stream") 0 197 197
126 if !$class->isa('Linux::Event::Stream');
127  
128 197 my $framer = _framer_for($class);
129 197 my $tls = _tls_for($class);
130 197 my $option = _stream_options_for($class);
131 188 my %callback = map { $_ => scalar $class->can($_) }
132 qw(on_data on_message on_messages on_drain on_eof on_error on_close
133 on_ready on_transport_ready configure_socket);
134  
135 188 ▶ if ($framer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($framer) { } 84 104 188
136 croak "$class read_batch_bytes is available only to raw Streams"
137 84 ▶ if $option->{read_batch_bytes};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"read_batch_bytes"} 1 83 84
138 croak "$class cannot define on_data() when it declares a framer"
139 83 ▶ if $callback{on_data};
Branch: 1 branch
BranchTrueFalseTotal
if $callback{"on_data"} 1 82 83
140 82 ▶ if ($option->{message_batch_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'message_batch_size'}) { } 18 64 82
141 croak "$class enables message_batch_size but does not define on_messages()"
142 18 ▶ if !$callback{on_messages};
Branch: 1 branch
BranchTrueFalseTotal
unless $callback{"on_messages"} 1 17 18
143 croak "$class cannot define both on_message() and on_messages()"
144 17 ▶ if $callback{on_message};
Branch: 1 branch
BranchTrueFalseTotal
if $callback{"on_message"} 1 16 17
145 } else {
146 croak "$class defines on_messages() without enabling message_batch_size"
147 64 ▶ if $callback{on_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $callback{"on_messages"} 1 63 64
148 croak "$class declares a framer but does not define on_message()"
149 63 ▶ if !$callback{on_message};
Branch: 1 branch
BranchTrueFalseTotal
unless $callback{"on_message"} 1 62 63
150 }
151 } else {
152 croak "$class has no framer and must define on_data()"
153 104 ▶ if !$callback{on_data};
Branch: 1 branch
BranchTrueFalseTotal
unless $callback{"on_data"} 1 103 104
154 croak "$class defines on_message() but does not declare a framer"
155 103 ▶ if $callback{on_message};
Branch: 1 branch
BranchTrueFalseTotal
if $callback{"on_message"} 0 103 103
156 croak "$class defines on_messages() but does not declare a framer"
157 103 ▶ if $callback{on_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $callback{"on_messages"} 1 102 103
158 croak "$class message_batch_size is available only to framed Streams"
159 102 ▶ if $option->{message_batch_size};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"message_batch_size"} 1 101 102
160 }
161  
162 179 ▶ my $native = $framer ? { %{ $framer->{native} } } : { read_mode => 0 };
Branch: 1 branch
BranchTrueFalseTotal
$framer ? : 78 101 179
163  
164 my $xs = Linux::Event::Stream::XSDescriptor->new(
165 $option->{read_size},
166 $option->{read_batch_bytes},
167 $option->{message_batch_size},
168 $option->{high_watermark},
169 $option->{low_watermark},
170 $option->{max_pending_bytes},
171 $option->{max_buffer},
172 $native->{read_mode},
173 $callback{on_data},
174 $callback{on_message},
175 $callback{on_messages},
176 $callback{on_drain} ? \&Linux::Event::Stream::_xs_drain : undef,
177 \&Linux::Event::Stream::_xs_read_eof,
178 \&Linux::Event::Stream::_xs_read_error,
179 \&Linux::Event::Stream::_xs_write_error,
180 \&Linux::Event::Stream::_xs_output_limit,
181 \&Linux::Event::Stream::_xs_write_empty,
182 \&Linux::Event::Stream::_xs_framing_error,
183 $native->{delimiter},
184 $native->{include_delimiter} // 0,
185 $native->{max_frame},
186 $native->{fixed_size} // 0,
187 $native->{prefix_bytes} // 0,
188 $native->{prefix_little} // 0,
189 179 ▶ $native->{include_prefix} // 0,
Branch: 1 branch
BranchTrueFalseTotal
$callback{'on_drain'} ? : 5 174 179
Condition100%10/10
$native->{"include_delimiter"} // 0
l!l
39140
$native->{"fixed_size"} // 0
l!l
11168
$native->{"prefix_bytes"} // 0
l!l
9170
$native->{"prefix_little"} // 0
l!l
9170
$native->{"include_prefix"} // 0
l!l
24155
MC/DC100%1/1
$native->{"include_delimiter"} // 0
$native->{"include_delimiter"}
MC/DC100%1/1
$native->{"fixed_size"} // 0
$native->{"fixed_size"}
MC/DC100%1/1
$native->{"prefix_bytes"} // 0
$native->{"prefix_bytes"}
MC/DC100%1/1
$native->{"prefix_little"} // 0
$native->{"prefix_little"}
MC/DC100%1/1
$native->{"include_prefix"} // 0
$native->{"include_prefix"}
Truth table100%2/2
A
A= $native->{"include_delimiter"}
Aresult
11
00
Truth table100%2/2
A
A= $native->{"fixed_size"}
Aresult
11
00
Truth table100%2/2
A
A= $native->{"prefix_bytes"}
Aresult
11
00
Truth table100%2/2
A
A= $native->{"prefix_little"}
Aresult
11
00
Truth table100%2/2
A
A= $native->{"include_prefix"}
Aresult
11
00
190 );
191  
192 179 my $descriptor = {
193 class => $class,
194 xs => $xs,
195 options => $option,
196 native => $native,
197 framer => $framer,
198 tls => $tls,
199 callbacks => \%callback,
200 };
201 179 $CLASS_DESCRIPTOR{$class} = $descriptor;
202 179 return $descriptor;
203 }
204  
205 0 ▶sub clear_cache () {
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod undocumented
206 0 %CLASS_DESCRIPTOR = ();
207 0 return;
208 }
209  
210 87 1;
« bench/run-stream-lifecycle-bench.pl ↑ Summary blib/lib/Linux/Event/Framer/LengthPrefix.pm »