ParallelSubtest.pm

filter: Statementstmt 75.1% 145 / 193 Branchbran 58.3% 42 / 72 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 25 / 30 Podpod 100% 4 / 4 totaltotal 70.2% 217 / 309 CC 38 SCAR 22.2
CC
38
Cov
69%
_wait_for_next_kid
31.3
_parse_outer_output_line
26.1
_run_test_from_kid_in_parent
8.2
CRAP 9.2
« ParallelSubtest/Capture.pm ↑ Summary
1 package Test::ParallelSubtest;
2 224 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 224 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 224 our $VERSION = '0.05';
6 224 $VERSION = eval $VERSION;
7  
8 224 ▶use Test::Builder::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 224 our @ISA = qw(Test::Builder::Module);
10 224 our @EXPORT = qw(bg_subtest_wait bg_subtest max_parallel);
11  
12 224 our @_kids;
13 224 our $_i_am_a_child = 0;
14  
15 224 our $MaxParallel = 4;
16  
17 224 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 224 ▶use Sub::Prepend ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 224 ▶use TAP::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 224 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 224 ▶use Test::ParallelSubtest::Capture;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 224 ▶Test::Builder->new->can('subtest') or croak
Branch: 1 branch
BranchTrueFalseTotal
"Test::Builder"->new->can("subtest") or croak("Need a version of Test::Builder with subtest support") 224 0 224
24 "Need a version of Test::Builder with subtest support";
25  
26 224 ▶END { bg_subtest_wait() if @_kids }
Subroutine END: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if @Test::ParallelSubtest::_kids 83 141 224
27  
28 224 Sub::Prepend::prepend(
29 'Test::Builder::done_testing' => \&bg_subtest_wait
30 );
31 Sub::Prepend::prepend(
32 207 ▶ 'Test::Builder::subtest' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 # Wait for kids just before entering a subtest.
34 207 bg_subtest_wait();
35  
36 # Wait for kids just before leaving a subtest.
37 207 my $subtests = $_[2];
38 207 ▶ $_[2] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 207 $subtests->();
40 183 bg_subtest_wait();
41 207 };
42 }
43 224 );
44  
45 224 ▶sub import_extra {
Subroutine import_extra: called CC=3 SCAR=11.0
Pod documented
46 224 my ($class, $list) = @_;
47  
48 224 my @other;
49 224 ▶ while (@$list >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list >= 2) 9 224 233
50 9 my $item = shift @$list;
51 9 ▶ if ($item eq 'max_parallel') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq 'max_parallel') { } 3 6 9
52 3 max_parallel(shift @$list);
53 }
54 else {
55 6 push @other, $item;
56 }
57 }
58  
59 224 @$list = @other;
60  
61 224 return;
62 }
63  
64 11 ▶sub max_parallel (;$) {
Subroutine max_parallel: called CC=3 SCAR=11.0
Pod documented
65 11 my $new_value = shift;
66  
67 11 my $old_value = $MaxParallel;
68 11 ▶ if (defined $new_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 5 6 11
69 5 ▶ $new_value =~ /^[0-9]+\z/ or croak "non-numeric max_parallel value";
Branch: 1 branch
BranchTrueFalseTotal
$new_value =~ /^[0-9]+\z/ or croak("non-numeric max_parallel value") 5 0 5
70 5 $MaxParallel = $new_value;
71 }
72  
73 11 return $old_value;
74 }
75  
76 298 ▶sub bg_subtest ($&) {
Subroutine bg_subtest: called CC=6 SCAR=17.9
Pod documented
77 298 my ($name, $subtests) = @_;
78  
79 298 ▶ $_i_am_a_child and croak "bg_subtest() called from a child process";
Branch: 1 branch
BranchTrueFalseTotal
$Test::ParallelSubtest::_i_am_a_child and croak("bg_subtest() called from a child process") 1 297 298
80  
81 297 my $tb = Test::Builder->new;
82  
83 297 ▶ if ($MaxParallel < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::ParallelSubtest::MaxParallel < 1) 1 296 297
84 # We've been told not to fork.
85 1 return $tb->subtest($name, $subtests);
86 }
87  
88 296 ▶ while (@_kids >= $MaxParallel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Test::ParallelSubtest::_kids >= $Test::ParallelSubtest::MaxParallel) 1 295 296
89 1 _wait_for_next_kid();
90 }
91  
92 295 my ($read_pipe, $write_pipe, $pid) = _pipe_and_fork();
93  
94 295 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 81 214 295
95 # Can't fork, fall back to a subtest() call.
96 81 return $tb->subtest($name, $subtests);
97 }
98  
99 214 my $out_fh = $tb->output;
100 214 my $fail_fh = $tb->failure_output;
101 214 my $todo_fh = $tb->todo_output;
102  
103 214 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 126 88 214
104 # parent
105 126 close $write_pipe;
106 126 my @caller = caller();
107 126 push @_kids, {
108 Pid => $pid,
109 Ppid => $$,
110 Pipe => $read_pipe,
111 Out => [$out_fh, $fail_fh, $todo_fh],
112 TestName => $name,
113 Caller => "$caller[1] line $caller[2]",
114 };
115 126 return;
116 }
117 else {
118 # child
119 88 close $read_pipe;
120 88 local $Test::Builder::Level = $Test::Builder::Level + 1;
121 88 _child($write_pipe, $name, $subtests);
122 }
123 }
124  
125 88 ▶sub _child {
Subroutine _child: called CC=1 SCAR=0.0
126 88 my ($write_pipe, $name, $subtests) = @_;
127  
128 88 $_i_am_a_child = 1;
129  
130 # Capture the outputs of the subtest as strings.
131 88 my $outer_out = '';
132 88 my $outer_fail = '';
133 88 my $outer_todo = '';
134 88 my $tb = Test::Builder->new;
135 88 $tb->output(\$outer_out);
136 88 $tb->failure_output(\$outer_fail);
137 88 $tb->todo_output(\$outer_todo);
138  
139 # Capture the outputs of tests within the subtest for replay
140 # in the parent.
141 88 my $capture = Test::ParallelSubtest::Capture->new;
142  
143 88 ▶ $tb->subtest($name, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 88 $capture->install($tb);
145 88 $subtests->();
146 88 });
147  
148 # Pass all the captured output to the parent process.
149 88 _len_prefixed_writes($write_pipe,
150 \$outer_out, \$outer_fail, \$outer_todo, $capture->as_string_ref
151 );
152  
153 88 close $write_pipe;
154  
155 224 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
156 88 ▶ *Test::Builder::DESTROY = sub {}; # For T::B fork in subtest bug
Subroutine __ANON__: not called CC=1 SCAR=0.0
157 88 exit(0);
158 }
159  
160 611 ▶sub bg_subtest_wait {
Subroutine bg_subtest_wait: called CC=3 SCAR=11.0
Pod documented
161 611 ▶ return if $_i_am_a_child;
Branch: 1 branch
BranchTrueFalseTotal
if $Test::ParallelSubtest::_i_am_a_child 264 347 611
162
163 347 ▶ _wait_for_next_kid() while @_kids;
Branch: 1 branch
BranchTrueFalseTotal
@Test::ParallelSubtest::_kids and do { _wait_for_next_kid() } 62 285 347
164 }
165  
166 295 ▶sub _pipe_and_fork {
Subroutine _pipe_and_fork: called CC=4 SCAR=13.9
167 295 my ($read_pipe, $write_pipe, $pid);
168  
169 295 while (1) {
170 295 my $pipe_ok = pipe $read_pipe, $write_pipe;
171 295 ▶ if ($pipe_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pipe_ok) 291 4 295
172 291 $pid = fork;
173 291 ▶ if (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 214 77 291
174 214 return ($read_pipe, $write_pipe, $pid);
175 }
176 }
177  
178 81 ▶ if (@_kids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Test::ParallelSubtest::_kids) { } 0 81 81
179 # The pipe or fork failure could be due to a resource limit,
180 # reap a kid and try again.
181 0 _wait_for_next_kid();
182 }
183 else {
184 # No kids and pipe+fork won't work, give up.
185 81 return;
186 }
187 }
188 }
189  
190 63 ▶sub _wait_for_next_kid {
Subroutine _wait_for_next_kid: called CC=8 SCAR=34.4
191 63 my $kid = shift @_kids;
192  
193 63 ▶ return unless $kid and $kid->{Ppid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $kid and $kid->{"Ppid"} == $$ 0 63 63
Condition33%1/3
$kid and $kid->{"Ppid"} == $$
!ll&&!rl&&r
0063
MC/DC0%0/2
$kid and $kid->{"Ppid"} == $$
$kid $kid->{"Ppid"} == $$
Truth table33%1/3
A and B
A= $kidB= $kid->{"Ppid"} == $$
ABresult
0X0
100
111
194  
195 63 my $tb = Test::Builder->new;
196 63 my ($out_dest, $fail_dest, $todo_dest) = @{ $kid->{Out} };
197  
198 my ($outer_out, $outer_fail, $outer_todo, $inner_capture)
199 63 = _len_prefixed_reads($kid->{Pipe}, 4);
200  
201 63 waitpid $kid->{Pid}, 0; # Don't let zombies build up.
202  
203 63 ▶ if (!defined $outer_out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outer_out) 0 63 63
204 0 my $name = "failed child process for '$kid->{TestName}'";
205 0 _run_test_from_kid_in_parent(0, $name, undef, undef, <<END);
206 ERROR: bg_subtest "$kid->{TestName}" ($kid->{Caller}) aborted:
207 Lost contact with the child process.
208 END
209 0 return;
210 }
211  
212 63 my ($ok, $name, $todo, $skip) = _parse_outer_output_line($$outer_out);
213  
214 0 ▶ if (!defined $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ok) 0 0 0
215 0 $name = "parse child output for '$kid->{TestName}'";
216 0 _run_test_from_kid_in_parent(0, $name, undef, undef, <<END);
217 ERROR: bg_subtest "$kid->{TestName}" ($kid->{Caller}) aborted:
218 Parsing failure in Test::ParallelSubtest - cannot parse:
219 [$$outer_out]
220 END
221 0 return;
222 }
223  
224 0 ▶ if (defined $todo and $Test::Builder::VERSION >= 0.95_02) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $todo and $Test::Builder::VERSION >= "0.9502") 0 0 0
Condition0%0/3
defined $todo and $Test::Builder::VERSION >= "0.9502"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $todo and $Test::Builder::VERSION >= "0.9502"
defined $todo $Test::Builder::VERSION >= "0.9502"
Truth table0%0/3
A and B
A= defined $todoB= $Test::Builder::VERSION >= "0.9502"
ABresult
0X0
100
111
225 # Recent Test::Builder redirects the fail output to the todo
226 # output for a todo subtest.
227 0 $fail_dest = $todo_dest;
228 }
229  
230 0 my $cap = Test::ParallelSubtest::Capture->new($inner_capture);
231 0 ▶ if ( ! $cap->replay_writes($out_dest, $fail_dest, $todo_dest) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cap->replay_writes($out_dest, $fail_dest, $todo_dest)) 0 0 0
232 0 $name = "garbled child output for '$name'";
233 0 _run_test_from_kid_in_parent(0, $name, undef, undef, <<END);
234 ERROR: bg_subtest "$name" ($kid->{Caller}) aborted:
235 Garbled captured output from the child process
236 END
237 0 return;
238 }
239  
240 0 _run_test_from_kid_in_parent($ok, $name, $todo, $skip);
241  
242 0 print $fail_dest $$outer_fail;
243 0 print $todo_dest $$outer_todo;
244 }
245  
246 0 ▶sub _run_test_from_kid_in_parent {
Subroutine _run_test_from_kid_in_parent: not called CC=3 SCAR=21.1
247 0 my ($ok, $name, $todo, $skip, $internal_failure) = @_;
248  
249 224 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 0 ▶ local *Test::Builder::todo = sub { $todo };
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 ▶ local *Test::Builder::in_todo = sub { defined $todo };
Subroutine __ANON__: not called CC=1 SCAR=6.9
252  
253 0 my $tb = Test::Builder->new;
254
255 0 ▶ if ($internal_failure) {
Branch: 2 branches
BranchTrueFalseTotal
if ($internal_failure) { } 0 0 0
elsif (defined $skip) { } 0 0 0
256 0 $tb->ok($ok, $name);
257 0 $tb->diag($internal_failure);
258 }
259 elsif (defined $skip) {
260 0 $tb->skip($skip);
261 }
262 else {
263 0 _ok_without_diag_output($tb, $ok, $name);
264 }
265 }
266  
267 0 ▶sub _ok_without_diag_output {
Subroutine _ok_without_diag_output: not called CC=1 SCAR=6.9
268 0 my ($tb, $pass, $name) = @_;
269  
270 0 my $discard = '';
271 0 my $save_fail = $tb->failure_output;
272 0 my $save_todo = $tb->todo_output;
273 0 $tb->failure_output(\$discard);
274 0 $tb->todo_output(\$discard);
275  
276 0 $tb->ok($pass, $name);
277  
278 0 $tb->todo_output($save_todo);
279 0 $tb->failure_output($save_fail);
280 }
281  
282 63 ▶sub _parse_outer_output_line {
Subroutine _parse_outer_output_line: called CC=7 SCAR=32.6
283 63 my $output = shift;
284  
285 63 ▶ while ($output =~ s/^(\s*#\s*.*\n)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ s/^(\s*#\s*.*\n)//) 63 63 126
286 63 _print_to(Test::Builder->new->output, $1);
287 }
288 63 $output =~ s/^\s*//; # it may have been generated within a subtest
289  
290 63 ▶ my $parser = TAP::Parser->new( { tap => $output } ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "TAP::Parser"->new({"tap", $output}) or return 0 0 0
291 0 ▶ my $result = $parser->next or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result = $parser->next or return 0 0 0
292 0 ▶ $result->is_test or return;
Branch: 1 branch
BranchTrueFalseTotal
$result->is_test or return 0 0 0
293  
294 0 my $ok = $result->is_actual_ok;
295 0 my $name = $result->description;
296 0 $name =~ s/- //;
297 0 $name =~ s/\\#/#/g;
298 0 ▶ my $todo = $result->has_todo ? $result->explanation : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result->has_todo ? : 0 0 0
299 0 ▶ my $skip = $result->has_skip ? $result->explanation : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result->has_skip ? : 0 0 0
300  
301 0 return ($ok, $name, $todo, $skip);
302 }
303  
304 88 ▶sub _len_prefixed_writes {
Subroutine _len_prefixed_writes: called CC=1 SCAR=0.0
305 88 my ($fh, @data) = @_;
306  
307 88 print $fh map { pack('N', length $$_) . $$_ } @data;
308 }
309  
310 63 ▶sub _len_prefixed_reads {
Subroutine _len_prefixed_reads: called CC=7 SCAR=19.7
311 63 my ($fh, $count) = @_;
312  
313 63 my @results;
314 63 ▶ while ($count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 252 63 315
315 252 my $lenbuf = '';
316 252 ▶ read $fh, $lenbuf, 4 or return;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $lenbuf, 4 or return 252 0 252
317 252 ▶ length($lenbuf) == 4 or return;
Branch: 1 branch
BranchTrueFalseTotal
length $lenbuf == 4 or return 252 0 252
318 252 my $wantlen = unpack 'N', $lenbuf;
319  
320 252 my $buf = '';
321 252 ▶ if ($wantlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantlen) 126 126 252
322 126 ▶ read $fh, $buf, $wantlen or return;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buf, $wantlen or return 126 0 126
323 126 ▶ length($buf) == $wantlen or return;
Branch: 1 branch
BranchTrueFalseTotal
length $buf == $wantlen or return 126 0 126
324 }
325 252 push @results, \$buf;
326 }
327  
328 63 return @results;
329 }
330  
331 63 ▶sub _print_to {
Subroutine _print_to: called CC=2 SCAR=7.4
332 63 my ($dest, $msg) = @_;
333  
334 63 ▶ if (ref $dest =~ /^SCALAR/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dest =~ /^SCALAR/) { } 0 63 63
335 0 $$dest .= $msg;
336 } else {
337 63 print {$dest} $msg;
338 }
339 }
340  
341 224 1;
342  
343 __END__
« ParallelSubtest/Capture.pm ↑ Summary