Perlito5X/Test2/API/Instance.pm untested

filter: Statementstmt 0.0% 0 / 306 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 60 Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 678 CC 73 SCAR 47.1
CC
73
Cov
0%
set_exit
380.0
_finalize
240.0
_ipc_wait
72.0
CRAP 111.5
1 package Test2::API::Instance;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '1.302073';
6  
7  
8 our @CARP_NOT = qw/Test2::API Test2::API::Instance Test2::IPC::Driver Test2::Formatter/;
9 use Carp qw/confess carp/;
10 use Scalar::Util qw/reftype/;
11  
12 use Test2::Util qw/get_tid USE_THREADS CAN_FORK pkg_to_file try/;
13  
14 use Test2::Util::Trace();
15 use Test2::API::Stack();
16  
17 use Test2::Util::HashBase qw{
18 _pid _tid
19 no_wait
20 finalized loaded
21 ipc stack formatter
22 contexts
23  
24 ipc_shm_size
25 ipc_shm_last
26 ipc_shm_id
27 ipc_polling
28 ipc_drivers
29 formatters
30  
31 exit_callbacks
32 post_load_callbacks
33 context_acquire_callbacks
34 context_init_callbacks
35 context_release_callbacks
36 };
37  
38 sub pid { $_[0]->{+_PID} ||= $$ }
39 sub tid { $_[0]->{+_TID} ||= get_tid() }
40  
41 # Wrap around the getters that should call _finalize.
42 BEGIN {
43 for my $finalizer (IPC, FORMATTER) {
44 my $orig = __PACKAGE__->can($finalizer);
45 my $new = sub {
46 my $self = shift;
47 $self->_finalize unless $self->{+FINALIZED};
48 $self->$orig;
49 };
50  
51 no strict 'refs';
52 no warnings 'redefine';
53 *{$finalizer} = $new;
54 }
55 }
56  
57 sub import {
58 my $class = shift;
59 return unless @_;
60 my ($ref) = @_;
61 $$ref = $class->new;
62 }
63  
64 sub init { $_[0]->reset }
65  
66 sub reset {
67 my $self = shift;
68  
69 delete $self->{+_PID};
70 delete $self->{+_TID};
71  
72 $self->{+CONTEXTS} = {};
73  
74 $self->{+IPC_DRIVERS} = [];
75 $self->{+IPC_POLLING} = undef;
76  
77 $self->{+FORMATTERS} = [];
78 $self->{+FORMATTER} = undef;
79  
80 $self->{+FINALIZED} = undef;
81 $self->{+IPC} = undef;
82  
83 $self->{+NO_WAIT} = 0;
84 $self->{+LOADED} = 0;
85  
86 $self->{+EXIT_CALLBACKS} = [];
87 $self->{+POST_LOAD_CALLBACKS} = [];
88 $self->{+CONTEXT_ACQUIRE_CALLBACKS} = [];
89 $self->{+CONTEXT_INIT_CALLBACKS} = [];
90 $self->{+CONTEXT_RELEASE_CALLBACKS} = [];
91  
92 $self->{+STACK} = Test2::API::Stack->new;
93 }
94  
95 sub _finalize {
96 my $self = shift;
97 my ($caller) = @_;
98 $caller ||= [caller(1)];
99  
100 $self->{+FINALIZED} = $caller;
101  
102 $self->{+_PID} = $$ unless defined $self->{+_PID};
103 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
104  
105 unless ($self->{+FORMATTER}) {
106 my ($formatter, $source);
107 if ($ENV{T2_FORMATTER}) {
108 $source = "set by the 'T2_FORMATTER' environment variable";
109  
110 if ($ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
111 $formatter = $1 ? $2 : "Test2::Formatter::$2"
112 }
113 else {
114 $formatter = '';
115 }
116 }
117 elsif (@{$self->{+FORMATTERS}}) {
118 ($formatter) = @{$self->{+FORMATTERS}};
119 $source = "Most recently added";
120 }
121 else {
122 $formatter = 'Test2::Formatter::TAP';
123 $source = 'default formatter';
124 }
125  
126 unless (ref($formatter) || $formatter->can('write')) {
127 my $file = pkg_to_file($formatter);
128 my ($ok, $err) = try { require $file };
129 unless ($ok) {
130 my $line = "* COULD NOT LOAD FORMATTER '$formatter' ($source) *";
131 my $border = '*' x length($line);
132 die "\n\n $border\n $line\n $border\n\n$err";
133 }
134 }
135  
136 $self->{+FORMATTER} = $formatter;
137 }
138  
139 # Turn on IPC if threads are on, drivers are registered, or the Test2::IPC
140 # module is loaded.
141 return unless USE_THREADS || $INC{'Test2/IPC.pm'} || @{$self->{+IPC_DRIVERS}};
142  
143 # Turn on polling by default, people expect it.
144 $self->enable_ipc_polling;
145  
146 unless (@{$self->{+IPC_DRIVERS}}) {
147 my ($ok, $error) = try { require Test2::IPC::Driver::Files };
148 die $error unless $ok;
149 push @{$self->{+IPC_DRIVERS}} => 'Test2::IPC::Driver::Files';
150 }
151  
152 for my $driver (@{$self->{+IPC_DRIVERS}}) {
153 next unless $driver->can('is_viable') && $driver->is_viable;
154 $self->{+IPC} = $driver->new or next;
155 $self->ipc_enable_shm if $self->{+IPC}->use_shm;
156 return;
157 }
158  
159 die "IPC has been requested, but no viable drivers were found. Aborting...\n";
160 }
161  
162 sub formatter_set { $_[0]->{+FORMATTER} ? 1 : 0 }
163  
164 sub add_formatter {
165 my $self = shift;
166 my ($formatter) = @_;
167 unshift @{$self->{+FORMATTERS}} => $formatter;
168  
169 return unless $self->{+FINALIZED};
170  
171 # Why is the @CARP_NOT entry not enough?
172 local %Carp::Internal = %Carp::Internal;
173 $Carp::Internal{'Test2::Formatter'} = 1;
174  
175 carp "Formatter $formatter loaded too late to be used as the global formatter";
176 }
177  
178 sub add_context_acquire_callback {
179 my $self = shift;
180 my ($code) = @_;
181  
182 my $rtype = reftype($code) || "";
183  
184 confess "Context-acquire callbacks must be coderefs"
185 unless $code && $rtype eq 'CODE';
186  
187 push @{$self->{+CONTEXT_ACQUIRE_CALLBACKS}} => $code;
188 }
189  
190 sub add_context_init_callback {
191 my $self = shift;
192 my ($code) = @_;
193  
194 my $rtype = reftype($code) || "";
195  
196 confess "Context-init callbacks must be coderefs"
197 unless $code && $rtype eq 'CODE';
198  
199 push @{$self->{+CONTEXT_INIT_CALLBACKS}} => $code;
200 }
201  
202 sub add_context_release_callback {
203 my $self = shift;
204 my ($code) = @_;
205  
206 my $rtype = reftype($code) || "";
207  
208 confess "Context-release callbacks must be coderefs"
209 unless $code && $rtype eq 'CODE';
210  
211 push @{$self->{+CONTEXT_RELEASE_CALLBACKS}} => $code;
212 }
213  
214 sub add_post_load_callback {
215 my $self = shift;
216 my ($code) = @_;
217  
218 my $rtype = reftype($code) || "";
219  
220 confess "Post-load callbacks must be coderefs"
221 unless $code && $rtype eq 'CODE';
222  
223 push @{$self->{+POST_LOAD_CALLBACKS}} => $code;
224 $code->() if $self->{+LOADED};
225 }
226  
227 sub load {
228 my $self = shift;
229 unless ($self->{+LOADED}) {
230 $self->{+_PID} = $$ unless defined $self->{+_PID};
231 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
232  
233 # This is for https://github.com/Test-More/test-more/issues/16
234 # and https://rt.perl.org/Public/Bug/Display.html?id=127774
235 # END blocks run in reverse order. This insures the END block is loaded
236 # as late as possible. It will not solve all cases, but it helps.
237 eval "END { Test2::API::test2_set_is_end() }; 1" or die $@;
238  
239 $self->{+LOADED} = 1;
240 $_->() for @{$self->{+POST_LOAD_CALLBACKS}};
241 }
242 return $self->{+LOADED};
243 }
244  
245 sub add_exit_callback {
246 my $self = shift;
247 my ($code) = @_;
248 my $rtype = reftype($code) || "";
249  
250 confess "End callbacks must be coderefs"
251 unless $code && $rtype eq 'CODE';
252  
253 push @{$self->{+EXIT_CALLBACKS}} => $code;
254 }
255  
256 sub add_ipc_driver {
257 my $self = shift;
258 my ($driver) = @_;
259 unshift @{$self->{+IPC_DRIVERS}} => $driver;
260  
261 return unless $self->{+FINALIZED};
262  
263 # Why is the @CARP_NOT entry not enough?
264 local %Carp::Internal = %Carp::Internal;
265 $Carp::Internal{'Test2::IPC::Driver'} = 1;
266  
267 carp "IPC driver $driver loaded too late to be used as the global ipc driver";
268 }
269  
270 sub enable_ipc_polling {
271 my $self = shift;
272  
273 $self->{+_PID} = $$ unless defined $self->{+_PID};
274 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
275  
276 $self->add_context_init_callback(
277 # This is called every time a context is created, it needs to be fast.
278 # $_[0] is a context object
279 sub {
280 return unless $self->{+IPC_POLLING};
281 return $_[0]->{hub}->cull unless $self->{+IPC_SHM_ID};
282  
283 my $val;
284 {
285 shmread($self->{+IPC_SHM_ID}, $val, 0, $self->{+IPC_SHM_SIZE}) or return;
286  
287 return if $val eq $self->{+IPC_SHM_LAST};
288 $self->{+IPC_SHM_LAST} = $val;
289 }
290  
291 $_[0]->{hub}->cull;
292 }
293 ) unless defined $self->ipc_polling;
294  
295 $self->set_ipc_polling(1);
296 }
297  
298 sub ipc_enable_shm {
299 my $self = shift;
300  
301 return 1 if defined $self->{+IPC_SHM_ID};
302  
303 $self->{+_PID} = $$ unless defined $self->{+_PID};
304 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
305  
306 my ($ok, $err) = try {
307 # SysV IPC can be available but not enabled.
308 #
309 # In some systems (*BSD) accessing the SysV IPC APIs without
310 # them being enabled can cause a SIGSYS. We suppress the SIGSYS
311 # and then get ENOSYS from the calls.
312 local $SIG{SYS} = 'IGNORE';
313  
314 require IPC::SysV;
315  
316 my $ipc_key = IPC::SysV::IPC_PRIVATE();
317 my $shm_size = $self->{+IPC}->can('shm_size') ? $self->{+IPC}->shm_size : 64;
318 my $shm_id = shmget($ipc_key, $shm_size, 0666) or die;
319  
320 my $initial = 'a' x $shm_size;
321 shmwrite($shm_id, $initial, 0, $shm_size) or die;
322  
323 $self->{+IPC_SHM_SIZE} = $shm_size;
324 $self->{+IPC_SHM_ID} = $shm_id;
325 $self->{+IPC_SHM_LAST} = $initial;
326 };
327  
328 return $ok;
329 }
330  
331 sub ipc_free_shm {
332 my $self = shift;
333  
334 my $id = delete $self->{+IPC_SHM_ID};
335 return unless defined $id;
336  
337 shmctl($id, IPC::SysV::IPC_RMID(), 0);
338 }
339  
340 sub get_ipc_pending {
341 my $self = shift;
342 return -1 unless defined $self->{+IPC_SHM_ID};
343 my $val;
344 shmread($self->{+IPC_SHM_ID}, $val, 0, $self->{+IPC_SHM_SIZE}) or return -1;
345 return 0 if $val eq $self->{+IPC_SHM_LAST};
346 $self->{+IPC_SHM_LAST} = $val;
347 return 1;
348 }
349  
350 sub set_ipc_pending {
351 my $self = shift;
352  
353 return undef unless defined $self->{+IPC_SHM_ID};
354  
355 my ($val) = @_;
356  
357 confess "value is required for set_ipc_pending"
358 unless $val;
359  
360 shmwrite($self->{+IPC_SHM_ID}, $val, 0, $self->{+IPC_SHM_SIZE});
361 }
362  
363 sub disable_ipc_polling {
364 my $self = shift;
365 return unless defined $self->{+IPC_POLLING};
366 $self->{+IPC_POLLING} = 0;
367 }
368  
369 sub _ipc_wait {
370 my $fail = 0;
371  
372 if (CAN_FORK) {
373 while (1) {
374 my $pid = CORE::wait();
375 my $err = $?;
376 last if $pid == -1;
377 next unless $err;
378 $fail++;
379 $err = $err >> 8;
380 warn "Process $pid did not exit cleanly (status: $err)\n";
381 }
382 }
383  
384 if (USE_THREADS) {
385 for my $t (threads->list()) {
386 $t->join;
387 # In older threads we cannot check if a thread had an error unless
388 # we control it and its return.
389 my $err = $t->can('error') ? $t->error : undef;
390 next unless $err;
391 my $tid = $t->tid();
392 $fail++;
393 chomp($err);
394 warn "Thread $tid did not end cleanly: $err\n";
395 }
396 }
397  
398 return 0 unless $fail;
399 return 255;
400 }
401  
402 sub DESTROY {
403 my $self = shift;
404  
405 return unless defined($self->{+_PID}) && $self->{+_PID} == $$;
406 return unless defined($self->{+_TID}) && $self->{+_TID} == get_tid();
407  
408 shmctl($self->{+IPC_SHM_ID}, IPC::SysV::IPC_RMID(), 0)
409 if defined $self->{+IPC_SHM_ID};
410 }
411  
412 sub set_exit {
413 my $self = shift;
414  
415 my $exit = $?;
416 my $new_exit = $exit;
417  
418 if ($INC{'Test/Builder.pm'} && $Test::Builder::VERSION ne $Test2::API::VERSION) {
419 print STDERR <<" EOT";
420  
421 ********************************************************************************
422 * *
423 * Test::Builder -- Test2::API version mismatch detected *
424 * *
425 ********************************************************************************
426 Test2::API Version: $Test2::API::VERSION
427 Test::Builder Version: $Test::Builder::VERSION
428  
429 This is not a supported configuration, you will have problems.
430  
431 EOT
432 }
433  
434 for my $ctx (values %{$self->{+CONTEXTS}}) {
435 next unless $ctx;
436  
437 next if $ctx->_aborted && ${$ctx->_aborted};
438  
439 # Only worry about contexts in this PID
440 my $trace = $ctx->trace || next;
441 next unless $trace->pid && $trace->pid == $$;
442  
443 # Do not worry about contexts that have no hub
444 my $hub = $ctx->hub || next;
445  
446 # Do not worry if the state came to a sudden end.
447 next if $hub->bailed_out;
448 next if defined $hub->skip_reason;
449  
450 # now we worry
451 $trace->alert("context object was never released! This means a testing tool is behaving very badly");
452  
453 $exit = 255;
454 $new_exit = 255;
455 }
456  
457 if (!defined($self->{+_PID}) or !defined($self->{+_TID}) or $self->{+_PID} != $$ or $self->{+_TID} != get_tid()) {
458 $? = $exit;
459 return;
460 }
461  
462 my @hubs = $self->{+STACK} ? $self->{+STACK}->all : ();
463  
464 if (@hubs and $self->{+IPC} and !$self->{+NO_WAIT}) {
465 local $?;
466 my %seen;
467 for my $hub (reverse @hubs) {
468 my $ipc = $hub->ipc or next;
469 next if $seen{$ipc}++;
470 $ipc->waiting();
471 }
472  
473 my $ipc_exit = _ipc_wait();
474 $new_exit ||= $ipc_exit;
475 }
476  
477 # None of this is necessary if we never got a root hub
478 if(my $root = shift @hubs) {
479 my $trace = Test2::Util::Trace->new(
480 frame => [__PACKAGE__, __FILE__, 0, __PACKAGE__ . '::END'],
481 detail => __PACKAGE__ . ' END Block finalization',
482 );
483 my $ctx = Test2::API::Context->new(
484 trace => $trace,
485 hub => $root,
486 );
487  
488 if (@hubs) {
489 $ctx->diag("Test ended with extra hubs on the stack!");
490 $new_exit = 255;
491 }
492  
493 unless ($root->no_ending) {
494 local $?;
495 $root->finalize($trace) unless $root->ended;
496 $_->($ctx, $exit, \$new_exit) for @{$self->{+EXIT_CALLBACKS}};
497 $new_exit ||= $root->failed;
498 $new_exit ||= 255 unless $root->is_passing;
499 }
500 }
501  
502 $new_exit = 255 if $new_exit > 255;
503  
504 if ($new_exit && eval { require Test2::API::Breakage; 1 }) {
505 my @warn = Test2::API::Breakage->report();
506  
507 if (@warn) {
508 print STDERR "\nYou have loaded versions of test modules known to have problems with Test2.\nThis could explain some test failures.\n";
509 print STDERR "$_\n" for @warn;
510 print STDERR "\n";
511 }
512 }
513  
514 $? = $new_exit;
515 }
516  
517 1;
518  
519 __END__