blib/lib/Test2/AsyncSubtest.pm

filter: Statementstmt 82.7% 240 / 290 Branchbran 60.0% 66 / 110 Conditioncond 57.8% 33 / 57 MC/DCmcdc 26.4% 9 / 34 Subroutinesub 92.5% 37 / 40 Podpod 82.3% 14 / 17 totaltotal 72.8% 399 / 548 CC 79 SCAR 23.6
CC
79
Cov
74%
finish
25.7
DESTROY
21.3
__ANON__
12.9
CRAP 10.6
« blib/lib/Test/Builder.pm ↑ Summary blib/lib/Test2/API/Context.pm »
1 package Test2::AsyncSubtest;
2 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 69 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 69 ▶use Test2::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 69 our $VERSION = '1.302226';
8  
9 69 our @CARP_NOT = qw/Test2::Util::HashBase/;
10  
11 69 ▶use Carp qw/croak cluck confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 69 ▶use Test2::Util qw/get_tid CAN_THREAD CAN_FORK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 69 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 69 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 69 ▶use Test2::API();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 69 ▶use Test2::API::Context();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 69 ▶use Test2::Util::Trace();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 69 ▶use Test2::Util::Guard();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 69 ▶use Time::HiRes();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 69 ▶use Test2::AsyncSubtest::Hub();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 69 ▶use Test2::AsyncSubtest::Event::Attach();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 69 ▶use Test2::AsyncSubtest::Event::Detach();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 69 ▶use Test2::Util::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
27 name hub
28 trace frame send_to
29 events
30 finished
31 active
32 stack
33 id cid uuid
34 children
35 _in_use
36 _attached pid tid
37 start_stamp stop_stamp
38 69 };
39  
40 44 ▶sub CAN_REALLY_THREAD {
Subroutine CAN_REALLY_THREAD: called CC=2 SCAR=15.4
Pod undocumented
41 44 return 0 unless CAN_THREAD;
42 0 ▶ return 0 unless eval { require threads; threads->VERSION('1.34'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require threads; "threads"->VERSION("1.34"); 1 } } 0 0 0
43 0 return 1;
44 }
45  
46  
47 69 my $UUID_VIA = Test2::API::_add_uuid_via_ref();
48 69 my $CID = 1;
49 69 my @STACK;
50  
51 0 ▶sub TOP { @STACK ? $STACK[-1] : undef }
Subroutine TOP: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@STACK ? : 0 0 0
52  
53 510 ▶sub init {
Subroutine init: called CC=10 SCAR=23.3
Pod undocumented
54 510 my $self = shift;
55  
56 croak "'name' is a required attribute"
57 510 ▶ unless $self->{+NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} 1 509 510
58  
59 509 ▶ my $to = $self->{+SEND_TO} ||= Test2::API::test2_stack()->top;
Condition33%1/3
$self->{"send_to"} ||= Test2::API::test2_stack()->top
l!l&&r!l&&!r
05090
MC/DC0%0/2
$self->{"send_to"} ||= Test2::API::test2_stack()->top
$self->{"send_to"} Test2::API::test2_stack()->top
Truth table33%1/3
A ||= B
A= $self->{"send_to"}B= Test2::API::test2_stack()->top
ABresult
1X1
011
000
60  
61 509 $self->{+STACK} = [@STACK];
62 509 $_->{+_IN_USE}++ for reverse @STACK;
63  
64 509 $self->{+TID} = get_tid;
65 509 $self->{+PID} = $$;
66 509 $self->{+CID} = 'AsyncSubtest-' . $CID++;
67 509 $self->{+ID} = 1;
68 509 $self->{+FINISHED} = 0;
69 509 $self->{+ACTIVE} = 0;
70 509 $self->{+_IN_USE} = 0;
71 509 $self->{+CHILDREN} = [];
72 509 ▶ $self->{+UUID} = ${$UUID_VIA}->() if defined $$UUID_VIA;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$UUID_VIA 1 508 509
73  
74 509 ▶ unless($self->{+HUB}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"hub"}) 509 0 509
75 509 my $ipc = Test2::API::test2_ipc();
76 509 my $formatter = Test2::API::test2_stack->top->format;
77 509 ▶ my $args = delete $self->{hub_init_args} || {};
Condition100%2/2
delete $self->{'hub_init_args'} || {}
l!l
17492
MC/DC100%1/1
delete $self->{'hub_init_args'} || {}
delete $self->{'hub_init_args'}
Truth table100%2/2
A
A= delete $self->{'hub_init_args'}
Aresult
11
00
78 509 my $hub = Test2::AsyncSubtest::Hub->new(
79 %$args,
80 ipc => $ipc,
81 nested => $to->nested + 1,
82 buffered => 1,
83 formatter => $formatter,
84 );
85 509 weaken($hub->{ast} = $self);
86 509 $self->{+HUB} = $hub;
87 }
88  
89 $self->{+TRACE} ||= Test2::Util::Trace->new(
90 frame => $self->{+FRAME} || [caller(1)],
91 buffered => $to->buffered,
92 nested => $to->nested,
93 cid => $self->{+CID},
94 509 ▶ uuid => $self->{+UUID},
Condition60%3/5
$self->{"trace"} ||= "Test2::Util::Trace"->new("frame", $self->{"frame"} || [caller 1], "buffered", $to->buffered, "nested", $to->nested, "cid", $self->{"cid"}, "uuid", $self->{"uuid"}, "hid", $to->hid, "huuid", $to->uuid)
l!l&&r!l&&!r
05090
$self->{'frame'} || [caller 1]
l!l
289220
MC/DC0%0/2
$self->{"trace"} ||= "Test2::Util::Trace"->new("frame", $self->{"frame"} || [caller 1], "buffered", $to->buffered, "nested", $to->nested, "cid", $self->{"cid"}, "uuid", $self->{"uuid"}, "hid", $to->hid, "huuid", $to->uuid)
$self->{"trace"} "Test2::Util::Trace"->new("frame", $self->{"frame"} || [caller 1], "buffered", $to->buffered, "nested", $to->nested, "cid", $self->{"cid"}, "uuid", $self->{"uuid"}, "hid", $to->hid, "huuid", $to->uuid)
MC/DC100%1/1
$self->{'frame'} || [caller 1]
$self->{'frame'}
Truth table33%1/3
A ||= B
A= $self->{"trace"}B= "Test2::Util::Trace"->new("frame", $self->{"frame"} || [caller 1], "buffered", $to->buffered, "nested", $to->nested, "cid", $self->{"cid"}, "uuid", $self->{"uuid"}, "hid", $to->hid, "huuid", $to->uuid)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{'frame'}
Aresult
11
00
95 hid => $to->hid,
96 huuid => $to->uuid,
97 );
98  
99 509 my $hub = $self->{+HUB};
100 509 ▶ $hub->set_ast_ids({}) unless $hub->ast_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $hub->ast_ids 509 0 509
101 509 $hub->listen($self->_listener);
102 }
103  
104 509 ▶sub _listener {
Subroutine _listener: called CC=2 SCAR=7.0
105 509 my $self = shift;
106  
107 509 ▶ my $events = $self->{+EVENTS} ||= [];
Condition50%1/2
$self->{"events"} ||= []
l!l
0509
MC/DC0%0/1
$self->{"events"} ||= []
$self->{"events"}
Truth table50%1/2
A
A= $self->{"events"}
Aresult
11
00
108  
109 509 ▶ sub { push @$events => $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
110 }
111  
112 404 ▶sub context {
Subroutine context: called CC=2 SCAR=6.9
Pod documented
113 404 my $self = shift;
114  
115 404 my $send_to = $self->{+SEND_TO};
116  
117 404 ▶ confess "Attempt to close AsyncSubtest when original parent hub (a non async-subtest?) has ended"
Branch: 1 branch
BranchTrueFalseTotal
if $send_to->ended 1 403 404
118 if $send_to->ended;
119  
120 return Test2::API::Context->new(
121 403 trace => $self->{+TRACE},
122 hub => $send_to,
123 );
124 }
125  
126 96 ▶sub _gen_event {
Subroutine _gen_event: called CC=1 SCAR=0.0
127 96 my $self = shift;
128 96 my ($type, $id, $hub) = @_;
129  
130 96 my $class = "Test2::AsyncSubtest::Event::$type";
131  
132 return $class->new(
133 id => $id,
134 trace => Test2::Util::Trace->new(
135 frame => [caller(1)],
136 buffered => $hub->buffered,
137 nested => $hub->nested,
138 cid => $self->{+CID},
139 96 uuid => $self->{+UUID},
140 hid => $hub->hid,
141 huuid => $hub->uuid,
142 ),
143 );
144 }
145  
146 298 ▶sub cleave {
Subroutine cleave: called CC=1 SCAR=0.0
Pod documented
147 298 my $self = shift;
148 298 my $id = $self->{+ID}++;
149 298 $self->{+HUB}->ast_ids->{$id} = 0;
150 298 return $id;
151 }
152  
153 50 ▶sub attach {
Subroutine attach: called CC=5 SCAR=16.8
Pod documented
154 50 my $self = shift;
155 50 my ($id) = @_;
156  
157 50 ▶ croak "An ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 50 50
158  
159 croak "ID $id is not valid"
160 50 ▶ unless defined $self->{+HUB}->ast_ids->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"hub"}->ast_ids->{$id} 0 50 50
161  
162 croak "ID $id is already attached"
163 50 ▶ if $self->{+HUB}->ast_ids->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hub"}->ast_ids->{$id} 0 50 50
164  
165 croak "You must attach INSIDE the child process/thread"
166 50 ▶ if $self->{+HUB}->is_local;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hub"}->is_local 0 50 50
167  
168 50 $self->{+_ATTACHED} = [ $$, get_tid, $id ];
169 50 $self->{+HUB}->send($self->_gen_event('Attach', $id, $self->{+HUB}));
170 }
171  
172 46 ▶sub detach {
Subroutine detach: called CC=6 SCAR=21.8
Pod documented
173 46 my $self = shift;
174  
175 46 ▶ if ($self->{+PID} == $$ && $self->{+TID} == get_tid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} == $$ and $self->{"tid"} == 0) 0 46 46
Condition33%1/3
$self->{"pid"} == $$ and $self->{"tid"} == 0
!ll&&!rl&&r
4600
MC/DC0%0/2
$self->{"pid"} == $$ and $self->{"tid"} == 0
$self->{"pid"} == $$ $self->{"tid"} == 0
Truth table33%1/3
A and B
A= $self->{"pid"} == $$B= $self->{"tid"} == 0
ABresult
0X0
100
111
176 0 cluck "You must detach INSIDE the child process/thread ($$, " . get_tid . " instead of $self->{+PID}, $self->{+TID})";
177 0 return;
178 }
179  
180 46 ▶ my $att = $self->{+_ATTACHED}
Branch: 1 branch
BranchTrueFalseTotal
my $att = $self->{"_attached"} or croak("Not attached") 46 0 46
181 or croak "Not attached";
182  
183 46 ▶ croak "Attempt to detach from wrong child"
Branch: 1 branch
BranchTrueFalseTotal
unless $att->[0] == $$ and $att->[1] == 0 0 46 46
Condition33%1/3
$att->[0] == $$ and $att->[1] == 0
!ll&&!rl&&r
0046
MC/DC0%0/2
$att->[0] == $$ and $att->[1] == 0
$att->[0] == $$ $att->[1] == 0
Truth table33%1/3
A and B
A= $att->[0] == $$B= $att->[1] == 0
ABresult
0X0
100
111
184 unless $att->[0] == $$ && $att->[1] == get_tid;
185  
186 46 my $id = $att->[2];
187  
188 46 $self->{+HUB}->send($self->_gen_event('Detach', $id, $self->{+HUB}));
189  
190 46 delete $self->{+_ATTACHED};
191 }
192  
193 1327 ▶sub ready { return !shift->pending }
Subroutine ready: called CC=1 SCAR=0.0
Pod documented
194 1329 ▶sub pending {
Subroutine pending: called CC=2 SCAR=6.9
Pod documented
195 1329 my $self = shift;
196 1329 my $hub = $self->{+HUB};
197 1329 ▶ return -1 unless $hub->is_local;
Branch: 1 branch
BranchTrueFalseTotal
unless $hub->is_local 85 1244 1329
198  
199 1244 $hub->cull;
200  
201 1244 return $self->{+_IN_USE} + keys %{$self->{+HUB}->ast_ids};
202 }
203  
204 60 ▶sub run {
Subroutine run: called CC=9 SCAR=23.5
Pod documented
205 60 my $self = shift;
206 60 my ($code, @args) = @_;
207  
208 60 ▶ croak "AsyncSubtest->run() takes a codeblock as the first argument"
Branch: 1 branch
BranchTrueFalseTotal
unless $code and ref $code eq "CODE" 0 60 60
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
0060
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
209 unless $code && ref($code) eq 'CODE';
210  
211 60 $self->start;
212  
213 60 my ($ok, $err, $finished);
214 T2_SUBTEST_WRAPPER: {
215 60 $ok = eval { $code->(@args); 1 };
216 41 $err = $@;
217  
218 # They might have done 'BEGIN { skip_all => "whatever" }'
219 41 ▶ if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/) { } 0 41 41
Condition66%2/3
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
!ll&&!rl&&r
4010
MC/DC0%0/2
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
not $ok $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
Truth table66%2/3
not(A) and B
A= not $okB= $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
ABresult
0X0
100
111
220 0 $ok = undef;
221 0 $err = undef;
222 }
223 else {
224 41 $finished = 1;
225 }
226 }
227  
228 57 $self->stop;
229  
230 57 my $hub = $self->{+HUB};
231  
232 57 ▶ if (!$finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($finished) 16 41 57
233 16 ▶ if(my $bailed = $hub->bailed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bailed = $hub->bailed_out) 0 16 16
234 0 my $ctx = $self->context;
235 0 $ctx->bail($bailed->reason);
236 0 return;
237 }
238 16 my $code = $hub->exit_code;
239 16 $ok = !$code;
240 16 ▶ $err = "Subtest ended with exit code $code" if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 16 16
241 }
242  
243 57 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 56 57
244 my $e = Test2::Event::Exception->new(
245 error => $err,
246 trace => Test2::Util::Trace->new(
247 frame => [caller(0)],
248 buffered => $hub->buffered,
249 nested => $hub->nested,
250 cid => $self->{+CID},
251 1 uuid => $self->{+UUID},
252 hid => $hub->hid,
253 huuid => $hub->uuid,
254 ),
255 );
256 1 $hub->send($e);
257 }
258  
259 57 return $hub->is_passing;
260 }
261  
262 252 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
263 252 my $self = shift;
264  
265 croak "Subtest is already complete"
266 252 ▶ if $self->{+FINISHED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 252 252
267  
268 252 ▶ $self->{+START_STAMP} = Time::HiRes::time() unless defined $self->{+START_STAMP};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"start_stamp"} 245 7 252
269  
270 252 $self->{+ACTIVE}++;
271  
272 252 push @STACK => $self;
273 252 my $hub = $self->{+HUB};
274 252 my $stack = Test2::API::test2_stack();
275 252 $stack->push($hub);
276  
277 252 return $hub->is_passing;
278 }
279  
280 236 ▶sub stop {
Subroutine stop: called CC=4 SCAR=14.5
Pod documented
281 236 my $self = shift;
282  
283 croak "Subtest is not active"
284 236 ▶ unless $self->{+ACTIVE}--;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"}-- 0 236 236
285  
286 236 ▶ croak "AsyncSubtest stack mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless @STACK and $self == $STACK[-1] 0 236 236
Condition33%1/3
@STACK and $self == $STACK[-1]
!ll&&!rl&&r
00236
MC/DC0%0/2
@STACK and $self == $STACK[-1]
@STACK $self == $STACK[-1]
Truth table33%1/3
A and B
A= @STACKB= $self == $STACK[-1]
ABresult
0X0
100
111
287 unless @STACK && $self == $STACK[-1];
288  
289 236 $self->{+STOP_STAMP} = Time::HiRes::time();
290  
291 236 pop @STACK;
292  
293 236 my $hub = $self->{+HUB};
294 236 my $stack = Test2::API::test2_stack();
295 236 $stack->pop($hub);
296 236 return $hub->is_passing;
297 }
298  
299 404 ▶sub finish {
Subroutine finish: called CC=23 SCAR=32.5
Pod documented
300 404 my $self = shift;
301 404 my %params = @_;
302  
303 404 my $hub = $self->hub;
304  
305 croak "Subtest is already finished"
306 404 ▶ if $self->{+FINISHED}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"}++ 0 404 404
307  
308 404 ▶ croak "Subtest can only be finished in the process/thread that created it"
Branch: 1 branch
BranchTrueFalseTotal
unless $hub->is_local 0 404 404
309 unless $hub->is_local;
310  
311 croak "Subtest is still active"
312 404 ▶ if $self->{+ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active"} 0 404 404
313  
314 404 $self->wait;
315 404 ▶ $self->{+STOP_STAMP} = Time::HiRes::time() unless defined $self->{+STOP_STAMP};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"stop_stamp"} 229 175 404
316 404 my $stop_stamp = $self->{+STOP_STAMP};
317  
318 404 my $todo = $params{todo};
319 404 my $skip = $params{skip};
320 404 my $empty = !@{$self->{+EVENTS}};
321 404 my $no_asserts = !$hub->count;
322 404 my $collapse = $params{collapse};
323 404 ▶ my $no_plan = $params{no_plan} || ($collapse && $no_asserts) || $skip;
Condition88%8/9
$params{'no_plan'} || $collapse && $no_asserts || $skip
l!l&&r!l&&!r
515384
$params{"no_plan"} or $collapse and $no_asserts
l!l&&r!l&&!r
05399
$collapse and $no_asserts
!ll&&!rl&&r
39725
MC/DC75%3/4
$params{'no_plan'} || $collapse && $no_asserts || $skip
$params{"no_plan"} $collapse $no_asserts $skip
Truth table66%4/6
A or B and C || D
A= $params{"no_plan"}B= $collapseC= $no_assertsD= $skip
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
324  
325 my $trace = Test2::Util::Trace->new(
326 frame => $self->{+TRACE}->{frame},
327 buffered => $hub->buffered,
328 nested => $hub->nested,
329 cid => $self->{+CID},
330 404 uuid => $self->{+UUID},
331 hid => $hub->hid,
332 huuid => $hub->uuid,
333 );
334  
335 404 ▶ $hub->finalize($trace, !$no_plan)
Branch: 1 branch
BranchTrueFalseTotal
unless $hub->no_ending or $hub->ended 404 0 404
Condition33%1/3
$hub->no_ending or $hub->ended
l!l&&r!l&&!r
00404
MC/DC0%0/2
$hub->no_ending or $hub->ended
$hub->no_ending $hub->ended
Truth table33%1/3
A or B
A= $hub->no_endingB= $hub->ended
ABresult
1X1
011
000
336 unless $hub->no_ending || $hub->ended;
337  
338 404 ▶ if ($hub->ipc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hub->ipc) 404 0 404
339 404 $hub->ipc->drop_hub($hub->hid);
340 404 $hub->set_ipc(undef);
341 }
342  
343 404 ▶ return $hub->is_passing if $params{silent};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"silent"} 0 404 404
344  
345 404 my $ctx = $self->context;
346  
347 403 my $pass = 1;
348 403 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) { } 15 388 403
349 15 $ctx->skip($self->{+NAME}, $skip);
350 }
351 else {
352 388 ▶ if ($collapse && $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collapse and $empty) 3 385 388
Condition100%3/3
$collapse and $empty
!ll&&!rl&&r
38143
MC/DC100%2/2
$collapse and $empty
$collapse $empty
Truth table100%3/3
A and B
A= $collapseB= $empty
ABresult
0X0
100
111
353 3 $ctx->ok($hub->is_passing, $self->{+NAME});
354 3 return $hub->is_passing;
355 }
356  
357 385 ▶ if ($collapse && $no_asserts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collapse and $no_asserts) 2 383 385
Condition100%3/3
$collapse and $no_asserts
!ll&&!rl&&r
38122
MC/DC100%2/2
$collapse and $no_asserts
$collapse $no_asserts
Truth table100%3/3
A and B
A= $collapseB= $no_asserts
ABresult
0X0
100
111
358 2 push @{$self->{+EVENTS}} => Test2::Event::Plan->new(trace => $trace, max => 0, directive => 'SKIP', reason => "No assertions");
359 }
360  
361 my $e = $ctx->build_event(
362 'Subtest',
363 pass => $hub->is_passing,
364 subtest_id => $hub->id,
365 subtest_uuid => $hub->uuid,
366 name => $self->{+NAME},
367 buffered => 1,
368 subevents => $self->{+EVENTS},
369 start_stamp => $self->{+START_STAMP},
370 385 ▶ stop_stamp => $self->{+STOP_STAMP},
Branch: 1 branch
BranchTrueFalseTotal
$todo ? : 0 385 385
371 $todo ? (
372 todo => $todo,
373 effective_pass => 1,
374 ) : (),
375 );
376  
377 385 $ctx->hub->send($e);
378  
379 385 ▶ unless ($e->effective_pass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->effective_pass) 5 380 385
380 5 $ctx->failure_diag($e);
381  
382 $ctx->diag("Bad subtest plan, expected " . $hub->plan . " but ran " . $hub->count)
383 0 ▶ if $hub->plan && !$hub->check_plan && !grep {$_->causes_fail} @{$self->{+EVENTS}};
Branch: 1 branch
BranchTrueFalseTotal
if $hub->plan and not $hub->check_plan and not grep({$_->causes_fail;} @{$$self{"events"};}) 0 5 5
Condition50%3/6
$hub->plan and not $hub->check_plan and not grep({$_->causes_fail;} @{$$self{"events"};})
!ll&&!rl&&r
500
$hub->plan and not $hub->check_plan
!ll&&!rl&&r
320
MC/DC0%0/3
$hub->plan and not $hub->check_plan and not grep({$_->causes_fail;} @{$$self{"events"};})
$hub->plan not $hub->check_plan not grep({$_->causes_fail;} @{$$self{"events"};})
Truth table50%2/4
A and not(B) and not(C)
A= $hub->planB= not $hub->check_planC= not grep({$_->causes_fail;} @{$$self{"events"};})
ABCresult
0XX0
10X0
1100
1111
384 }
385  
386 385 $pass = $e->pass;
387 }
388  
389 400 $_->{+_IN_USE}-- for reverse @{$self->{+STACK}};
390  
391 400 return $pass;
392 }
393  
394 404 ▶sub wait {
Subroutine wait: called CC=6 SCAR=19.3
Pod documented
395 404 my $self = shift;
396  
397 404 my $hub = $self->{+HUB};
398 404 my $children = $self->{+CHILDREN};
399  
400 404 ▶ while (@$children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$children) 210 404 614
401 210 $hub->cull;
402 210 ▶ if (my $child = pop @$children) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = pop @$children) { } 210 0 210
403 210 ▶ if (blessed($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($child)) { } 0 210 210
404 0 $child->join;
405 }
406 else {
407 210 waitpid($child, 0);
408 }
409 }
410 else {
411 0 Time::HiRes::sleep('0.01');
412 }
413 }
414  
415 404 $hub->cull;
416  
417 cluck "Subtest '$self->{+NAME}': All children have completed, but we still appear to be pending"
418 404 ▶ if $hub->is_local && keys %{$self->{+HUB}->ast_ids};
Branch: 1 branch
BranchTrueFalseTotal
if $hub->is_local and keys %{$self->{"hub"}->ast_ids;} 0 404 404
Condition33%1/3
$hub->is_local and keys %{$self->{"hub"}->ast_ids;}
!ll&&!rl&&r
04040
MC/DC0%0/2
$hub->is_local and keys %{$self->{"hub"}->ast_ids;}
$hub->is_local keys %{$self->{"hub"}->ast_ids;}
Truth table33%1/3
A and B
A= $hub->is_localB= keys %{$self->{"hub"}->ast_ids;}
ABresult
0X0
100
111
419 }
420  
421 293 ▶sub fork {
Subroutine fork: called CC=4 SCAR=14.2
Pod documented
422 293 ▶ croak "Forking is not supported" unless CAN_FORK;
Branch: 1 branch
BranchTrueFalseTotal
unless &CAN_FORK() 0 293 293
423 293 my $self = shift;
424 293 my $id = $self->cleave;
425 293 my $pid = CORE::fork();
426  
427 293 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 293 293
428 0 delete $self->{+HUB}->ast_ids->{$id};
429 0 croak "Failed to fork";
430 }
431  
432 293 ▶ if($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 245 48 293
433 245 push @{$self->{+CHILDREN}} => $pid;
434 245 return $pid;
435 }
436  
437 48 $self->attach($id);
438  
439 48 return $self->_guard;
440 }
441  
442 182 ▶sub run_fork {
Subroutine run_fork: called CC=2 SCAR=6.9
Pod documented
443 182 my $self = shift;
444 182 my ($code, @args) = @_;
445  
446 182 my $f = $self->fork;
447 182 ▶ return $f unless blessed($f);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) 159 23 182
448  
449 23 $self->run($code, @args);
450  
451 22 $self->detach();
452 22 $f->dismiss();
453 22 exit 0;
454 }
455  
456 0 ▶sub run_thread {
Subroutine run_thread: not called CC=2 SCAR=17.9
Pod documented
457 0 ▶ croak "Threading is not supported"
Branch: 1 branch
BranchTrueFalseTotal
unless CAN_REALLY_THREAD() 0 0 0
458 unless CAN_REALLY_THREAD;
459  
460 0 my $self = shift;
461 0 my ($code, @args) = @_;
462  
463 0 my $id = $self->cleave;
464 0 ▶ my $thr = threads->create(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
465 0 $self->attach($id);
466  
467 0 $self->run($code, @args);
468  
469 0 $self->detach(get_tid);
470 0 return 0;
471 0 });
472  
473 0 push @{$self->{+CHILDREN}} => $thr;
474  
475 0 return $thr;
476 }
477  
478 48 ▶sub _guard {
Subroutine _guard: called CC=1 SCAR=2.9
479 48 my $self = shift;
480  
481 48 my ($pid, $tid) = ($$, get_tid);
482  
483 1 ▶ return Test2::Util::Guard->new(sub {
Subroutine __ANON__: called CC=4 SCAR=25.6
484 1 ▶ return unless $$ == $pid && get_tid == $tid;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $pid and 0 == $tid 1 0 1
Condition33%1/3
$$ == $pid and 0 == $tid
!ll&&!rl&&r
100
MC/DC0%0/2
$$ == $pid and 0 == $tid
$$ == $pid 0 == $tid
Truth table33%1/3
A and B
A= $$ == $pidB= 0 == $tid
ABresult
0X0
100
111
485  
486 0 my $error = "Scope Leak";
487 0 ▶ if (my $ex = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex = $@) 0 0 0
488 0 chomp($ex);
489 0 $error .= " ($ex)";
490 }
491  
492 0 cluck $error;
493  
494 0 my $e = $self->context->build_event(
495 'Exception',
496 error => "$error\n",
497 );
498 0 $self->{+HUB}->send($e);
499 0 $self->detach();
500 0 exit 255;
501 48 });
502 }
503  
504 431 ▶sub DESTROY {
Subroutine DESTROY: called CC=8 SCAR=30.6
505 431 my $self = shift;
506 431 ▶ return unless $self->{+NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} 1 430 431
507  
508 430 ▶ if (my $att = $self->{+_ATTACHED}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $att = $self->{"_attached"}) 0 430 430
509 0 ▶ return unless $self->{+HUB};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"hub"} 0 0 0
510 0 eval { $self->detach() };
511 }
512  
513 430 ▶ return if $self->{+FINISHED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 386 44 430
514 44 ▶ return unless $self->{+PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} == $$ 44 0 44
515 0 ▶ return unless $self->{+TID} == get_tid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"tid"} == 0 0 0 0
516  
517 0 local $@;
518 0 eval { $_->{+_IN_USE}-- for reverse @{$self->{+STACK}} };
519  
520 0 warn "Subtest $self->{+NAME} did not finish!";
521 0 exit 255;
522 }
523  
524 69 1;
525  
526 __END__
« blib/lib/Test/Builder.pm ↑ Summary blib/lib/Test2/API/Context.pm »