File Coverage

blib/lib/Test2/API/Instance.pm
Criterion Covered Total %
statement 296 304 97.3
branch 126 154 81.8
condition 56 89 62.9
subroutine 43 45 95.5
pod 18 24 75.0
total 539 616 87.5


line stmt bran cond sub pod time code
1             package Test2::API::Instance;
2 254     254   2923 use strict;
  249         493  
  249         9047  
3 249     249   1291 use warnings;
  249         1097  
  246         15602  
4              
5             our $VERSION = '1.302181';
6              
7             our @CARP_NOT = qw/Test2::API Test2::API::Instance Test2::IPC::Driver Test2::Formatter/;
8 246     249   1504 use Carp qw/confess carp/;
  246         462  
  246         15333  
9 246     249   1753 use Scalar::Util qw/reftype/;
  246         546  
  246         19461  
10              
11 246     249   8473 use Test2::Util qw/get_tid USE_THREADS CAN_FORK pkg_to_file try CAN_SIGSYS/;
  246         483  
  246         16161  
12              
13 246     246   109375 use Test2::EventFacet::Trace();
  246         614  
  246         5260  
14 246     246   105198 use Test2::API::Stack();
  246         652  
  246         9803  
15              
16 246         1164 use Test2::Util::HashBase qw{
17             _pid _tid
18             no_wait
19             finalized loaded
20             ipc stack formatter
21             contexts
22              
23             add_uuid_via
24              
25             -preload
26              
27             ipc_disabled
28             ipc_polling
29             ipc_drivers
30             ipc_timeout
31             formatters
32              
33             exit_callbacks
34             post_load_callbacks
35             context_acquire_callbacks
36             context_init_callbacks
37             context_release_callbacks
38             pre_subtest_callbacks
39 246     246   1762 };
  246         455  
40              
41             sub DEFAULT_IPC_TIMEOUT() { 30 }
42              
43 219     219 1 2224 sub pid { $_[0]->{+_PID} }
44 212     212 1 1088 sub tid { $_[0]->{+_TID} }
45              
46             # Wrap around the getters that should call _finalize.
47             BEGIN {
48 246     246   1046 for my $finalizer (IPC, FORMATTER) {
49 492         2200 my $orig = __PACKAGE__->can($finalizer);
50             my $new = sub {
51 715     715   1613 my $self = shift;
52 715 100       3345 $self->_finalize unless $self->{+FINALIZED};
53 709         2923 $self->$orig;
54 492         2265 };
55              
56 246     246   1995 no strict 'refs';
  246         584  
  246         9617  
57 246     246   1511 no warnings 'redefine';
  246         488  
  246         10212  
58 492         1007 *{$finalizer} = $new;
  492         807605  
59             }
60             }
61              
62 188     188 0 1178 sub has_ipc { !!$_[0]->{+IPC} }
63              
64             sub import {
65 273     273   669 my $class = shift;
66 273 100       1963 return unless @_;
67 246         616 my ($ref) = @_;
68 246         1040 $$ref = $class->new;
69             }
70              
71 298     298 0 944 sub init { $_[0]->reset }
72              
73             sub start_preload {
74 1     1 0 3 my $self = shift;
75              
76             confess "preload cannot be started, Test2::API has already been initialized"
77 1 50 33     9 if $self->{+FINALIZED} || $self->{+LOADED};
78              
79 1         28 return $self->{+PRELOAD} = 1;
80             }
81              
82             sub stop_preload {
83 1     1 0 3 my $self = shift;
84              
85 1 50       3 return 0 unless $self->{+PRELOAD};
86 1         3 $self->{+PRELOAD} = 0;
87              
88 1         4 $self->post_preload_reset();
89              
90 1         3 return 1;
91             }
92              
93             sub post_preload_reset {
94 1     1 0 1 my $self = shift;
95              
96 1         3 delete $self->{+_PID};
97 1         2 delete $self->{+_TID};
98              
99 1 50       3 $self->{+ADD_UUID_VIA} = undef unless exists $self->{+ADD_UUID_VIA};
100              
101 1         3 $self->{+CONTEXTS} = {};
102              
103 1         14 $self->{+FORMATTERS} = [];
104              
105 1         3 $self->{+FINALIZED} = undef;
106 1         3 $self->{+IPC} = undef;
107 1 50       6 $self->{+IPC_DISABLED} = $ENV{T2_NO_IPC} ? 1 : 0;
108              
109 1 50       4 $self->{+IPC_TIMEOUT} = DEFAULT_IPC_TIMEOUT() unless defined $self->{+IPC_TIMEOUT};
110              
111 1         2 $self->{+LOADED} = 0;
112              
113 1   33     7 $self->{+STACK} ||= Test2::API::Stack->new;
114             }
115              
116             sub reset {
117 355     355 1 1130 my $self = shift;
118              
119 355         1489 delete $self->{+_PID};
120 355         658 delete $self->{+_TID};
121              
122 355         740 $self->{+ADD_UUID_VIA} = undef;
123              
124 355         809 $self->{+CONTEXTS} = {};
125              
126 355         782 $self->{+IPC_DRIVERS} = [];
127 355         675 $self->{+IPC_POLLING} = undef;
128              
129 355         743 $self->{+FORMATTERS} = [];
130 355         743 $self->{+FORMATTER} = undef;
131              
132 355         679 $self->{+FINALIZED} = undef;
133 355         990 $self->{+IPC} = undef;
134 355 100       1584 $self->{+IPC_DISABLED} = $ENV{T2_NO_IPC} ? 1 : 0;
135              
136 355 100       1197 $self->{+IPC_TIMEOUT} = DEFAULT_IPC_TIMEOUT() unless defined $self->{+IPC_TIMEOUT};
137              
138 355         654 $self->{+NO_WAIT} = 0;
139 355         612 $self->{+LOADED} = 0;
140              
141 355         707 $self->{+EXIT_CALLBACKS} = [];
142 355         771 $self->{+POST_LOAD_CALLBACKS} = [];
143 355         752 $self->{+CONTEXT_ACQUIRE_CALLBACKS} = [];
144 355         1103 $self->{+CONTEXT_INIT_CALLBACKS} = [];
145 355         723 $self->{+CONTEXT_RELEASE_CALLBACKS} = [];
146 355         728 $self->{+PRE_SUBTEST_CALLBACKS} = [];
147              
148 355         2315 $self->{+STACK} = Test2::API::Stack->new;
149             }
150              
151             sub _finalize {
152 284     284   2428 my $self = shift;
153 284         838 my ($caller) = @_;
154 284   50     4589 $caller ||= [caller(1)];
155              
156             confess "Attempt to initialize Test2::API during preload"
157 284 50       1482 if $self->{+PRELOAD};
158              
159 284         831 $self->{+FINALIZED} = $caller;
160              
161 284 100       1501 $self->{+_PID} = $$ unless defined $self->{+_PID};
162 284 100       1234 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
163              
164 284 100       1094 unless ($self->{+FORMATTER}) {
165 279         972 my ($formatter, $source);
166 279 100       1242 if ($ENV{T2_FORMATTER}) {
    100          
167 34         99 $source = "set by the 'T2_FORMATTER' environment variable";
168              
169 34 50       333 if ($ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
170 34 100       787 $formatter = $1 ? $2 : "Test2::Formatter::$2"
171             }
172             else {
173 0         0 $formatter = '';
174             }
175             }
176 245         1502 elsif (@{$self->{+FORMATTERS}}) {
177 159         408 ($formatter) = @{$self->{+FORMATTERS}};
  159         629  
178 159         494 $source = "Most recently added";
179             }
180             else {
181 86         228 $formatter = 'Test2::Formatter::TAP';
182 86         187 $source = 'default formatter';
183             }
184              
185 279 100 66     4914 unless (ref($formatter) || $formatter->can('write')) {
186 88         525 my $file = pkg_to_file($formatter);
187 88     88   690 my ($ok, $err) = try { require $file };
  88         44720  
188 88 100       610 unless ($ok) {
189 5         45 my $line = "* COULD NOT LOAD FORMATTER '$formatter' ($source) *";
190 5         25 my $border = '*' x length($line);
191 5         170 die "\n\n $border\n $line\n $border\n\n$err";
192             }
193             }
194              
195 274         1130 $self->{+FORMATTER} = $formatter;
196             }
197              
198             # Turn on IPC if threads are on, drivers are registered, or the Test2::IPC
199             # module is loaded.
200 279 100       1305 return if $self->{+IPC_DISABLED};
201 275 100 100     1708 return unless USE_THREADS || $INC{'Test2/IPC.pm'} || @{$self->{+IPC_DRIVERS}};
  220         1609  
202              
203             # Turn on polling by default, people expect it.
204 58         294 $self->enable_ipc_polling;
205              
206 58 100       92 unless (@{$self->{+IPC_DRIVERS}}) {
  58         218  
207 55     55   432 my ($ok, $error) = try { require Test2::IPC::Driver::Files };
  55         13702  
208 55 50       315 die $error unless $ok;
209 55         108 push @{$self->{+IPC_DRIVERS}} => 'Test2::IPC::Driver::Files';
  55         266  
210             }
211              
212 58         136 for my $driver (@{$self->{+IPC_DRIVERS}}) {
  58         200  
213 58 100 66     903 next unless $driver->can('is_viable') && $driver->is_viable;
214 57 50       395 $self->{+IPC} = $driver->new or next;
215 57         159 return;
216             }
217              
218 1         12 die "IPC has been requested, but no viable drivers were found. Aborting...\n";
219             }
220              
221 11 100   11 1 251 sub formatter_set { $_[0]->{+FORMATTER} ? 1 : 0 }
222              
223             sub add_formatter {
224 162     162 1 418 my $self = shift;
225 162         502 my ($formatter) = @_;
226 162         323 unshift @{$self->{+FORMATTERS}} => $formatter;
  162         676  
227              
228 162 100       4318 return unless $self->{+FINALIZED};
229              
230             # Why is the @CARP_NOT entry not enough?
231 2         12 local %Carp::Internal = %Carp::Internal;
232 2         6 $Carp::Internal{'Test2::Formatter'} = 1;
233              
234 2         481 carp "Formatter $formatter loaded too late to be used as the global formatter";
235             }
236              
237             sub add_context_acquire_callback {
238 165     165 0 388 my $self = shift;
239 165         457 my ($code) = @_;
240              
241 165   50     860 my $rtype = reftype($code) || "";
242              
243 165 50 33     1244 confess "Context-acquire callbacks must be coderefs"
244             unless $code && $rtype eq 'CODE';
245              
246 165         393 push @{$self->{+CONTEXT_ACQUIRE_CALLBACKS}} => $code;
  165         772  
247             }
248              
249             sub add_context_init_callback {
250 62     62 1 141 my $self = shift;
251 62         152 my ($code) = @_;
252              
253 62   50     405 my $rtype = reftype($code) || "";
254              
255 62 50 33     407 confess "Context-init callbacks must be coderefs"
256             unless $code && $rtype eq 'CODE';
257              
258 62         105 push @{$self->{+CONTEXT_INIT_CALLBACKS}} => $code;
  62         208  
259             }
260              
261             sub add_context_release_callback {
262 3     3 1 8 my $self = shift;
263 3         26 my ($code) = @_;
264              
265 3   50     15 my $rtype = reftype($code) || "";
266              
267 3 50 33     18 confess "Context-release callbacks must be coderefs"
268             unless $code && $rtype eq 'CODE';
269              
270 3         5 push @{$self->{+CONTEXT_RELEASE_CALLBACKS}} => $code;
  3         9  
271             }
272              
273             sub add_post_load_callback {
274 180     180 1 448 my $self = shift;
275 180         503 my ($code) = @_;
276              
277 180   50     1317 my $rtype = reftype($code) || "";
278              
279 180 100 66     2459 confess "Post-load callbacks must be coderefs"
280             unless $code && $rtype eq 'CODE';
281              
282 175         387 push @{$self->{+POST_LOAD_CALLBACKS}} => $code;
  175         605  
283 175 100       880 $code->() if $self->{+LOADED};
284             }
285              
286             sub add_pre_subtest_callback {
287 17     17 1 61 my $self = shift;
288 17         46 my ($code) = @_;
289              
290 17   50     74 my $rtype = reftype($code) || "";
291              
292 17 100 66     580 confess "Pre-subtest callbacks must be coderefs"
293             unless $code && $rtype eq 'CODE';
294              
295 12         20 push @{$self->{+PRE_SUBTEST_CALLBACKS}} => $code;
  12         53  
296             }
297              
298             sub load {
299 532     532 1 1401 my $self = shift;
300 532 100       2040 unless ($self->{+LOADED}) {
301             confess "Attempt to initialize Test2::API during preload"
302 265 50       1091 if $self->{+PRELOAD};
303              
304 265 100       2879 $self->{+_PID} = $$ unless defined $self->{+_PID};
305 265 100       1218 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
306              
307             # This is for https://github.com/Test-More/test-more/issues/16
308             # and https://rt.perl.org/Public/Bug/Display.html?id=127774
309             # END blocks run in reverse order. This insures the END block is loaded
310             # as late as possible. It will not solve all cases, but it helps.
311 265 50   243   20854 eval "END { Test2::API::test2_set_is_end() }; 1" or die $@;
  243         20104  
312              
313 265         1713 $self->{+LOADED} = 1;
314 265         668 $_->() for @{$self->{+POST_LOAD_CALLBACKS}};
  265         1715  
315             }
316 532         2724 return $self->{+LOADED};
317             }
318              
319             sub add_exit_callback {
320 180     180 1 495 my $self = shift;
321 180         523 my ($code) = @_;
322 180   50     915 my $rtype = reftype($code) || "";
323              
324 180 100 66     1774 confess "End callbacks must be coderefs"
325             unless $code && $rtype eq 'CODE';
326              
327 175         420 push @{$self->{+EXIT_CALLBACKS}} => $code;
  175         645  
328             }
329              
330             sub ipc_disable {
331 4     4 1 19 my $self = shift;
332              
333             confess "Attempt to disable IPC after it has been initialized"
334 4 100       173 if $self->{+IPC};
335              
336 3         55 $self->{+IPC_DISABLED} = 1;
337             }
338              
339             sub add_ipc_driver {
340 9     9 1 201 my $self = shift;
341 9         41 my ($driver) = @_;
342 9         19 unshift @{$self->{+IPC_DRIVERS}} => $driver;
  9         42  
343              
344 9 100       4791 return unless $self->{+FINALIZED};
345              
346             # Why is the @CARP_NOT entry not enough?
347 2         34 local %Carp::Internal = %Carp::Internal;
348 2         8 $Carp::Internal{'Test2::IPC::Driver'} = 1;
349              
350 2         626 carp "IPC driver $driver loaded too late to be used as the global ipc driver";
351             }
352              
353             sub enable_ipc_polling {
354 61     61 1 150 my $self = shift;
355              
356 61 100       214 $self->{+_PID} = $$ unless defined $self->{+_PID};
357 61 100       173 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
358              
359             $self->add_context_init_callback(
360             # This is called every time a context is created, it needs to be fast.
361             # $_[0] is a context object
362             sub {
363 693 100   693   1855 return unless $self->{+IPC_POLLING};
364 691 50       1606 return unless $self->{+IPC};
365 691 50       2761 return unless $self->{+IPC}->pending();
366 691         2925 return $_[0]->{hub}->cull;
367             }
368 61 100       363 ) unless defined $self->ipc_polling;
369              
370 61         247 $self->set_ipc_polling(1);
371             }
372              
373             sub get_ipc_pending {
374 0     0 1 0 my $self = shift;
375 0 0       0 return -1 unless $self->{+IPC};
376 0         0 $self->{+IPC}->pending();
377             }
378              
379             sub _check_pid {
380 0     0   0 my $self = shift;
381 0         0 my ($pid) = @_;
382 0         0 return kill(0, $pid);
383             }
384              
385             sub set_ipc_pending {
386 35     35 1 126 my $self = shift;
387 35 100       176 return unless $self->{+IPC};
388 29         124 my ($val) = @_;
389              
390 29 50       108 confess "value is required for set_ipc_pending"
391             unless $val;
392              
393 29         703 $self->{+IPC}->set_pending($val);
394             }
395              
396             sub disable_ipc_polling {
397 3     3 1 6 my $self = shift;
398 3 100       13 return unless defined $self->{+IPC_POLLING};
399 2         7 $self->{+IPC_POLLING} = 0;
400             }
401              
402             sub _ipc_wait {
403 29     29   7914 my ($timeout) = @_;
404 29         516 my $fail = 0;
405              
406 29 100       426 $timeout = DEFAULT_IPC_TIMEOUT() unless defined $timeout;
407              
408 29         232 my $ok = eval {
409 29 50       632 if (CAN_FORK) {
410 29     1   1695 local $SIG{ALRM} = sub { die "Timeout waiting on child processes" };
  1         70  
411 29         352 alarm $timeout;
412              
413 29         83 while (1) {
414 46         34831923 my $pid = CORE::wait();
415 45         513 my $err = $?;
416 45 100       266 last if $pid == -1;
417 17 100       183 next unless $err;
418 9         75 $fail++;
419              
420 9         61 my $sig = $err & 127;
421 9         38 my $exit = $err >> 8;
422 9         769 warn "Process $pid did not exit cleanly (wstat: $err, exit: $exit, sig: $sig)\n";
423             }
424              
425 28         831 alarm 0;
426             }
427              
428 28         137 if (USE_THREADS) {
429             my $start = time;
430              
431             while (1) {
432             last unless threads->list();
433             die "Timeout waiting on child thread" if time - $start >= $timeout;
434             sleep 1;
435             for my $t (threads->list) {
436             # threads older than 1.34 do not have this :-(
437             next if $t->can('is_joinable') && !$t->is_joinable;
438             $t->join;
439             # In older threads we cannot check if a thread had an error unless
440             # we control it and its return.
441             my $err = $t->can('error') ? $t->error : undef;
442             next unless $err;
443             my $tid = $t->tid();
444             $fail++;
445             chomp($err);
446             warn "Thread $tid did not end cleanly: $err\n";
447             }
448             }
449             }
450              
451 28         149 1;
452             };
453 29         173 my $error = $@;
454              
455 29 100 100     615 return 0 if $ok && !$fail;
456 10 100       171 warn $error unless $ok;
457 10         237 return 255;
458             }
459              
460             sub set_exit {
461 280     280 1 4380 my $self = shift;
462              
463 280 50       1524 return if $self->{+PRELOAD};
464              
465 280         1013 my $exit = $?;
466 280         742 my $new_exit = $exit;
467              
468 280 100 100     2320 if ($INC{'Test/Builder.pm'} && $Test::Builder::VERSION ne $Test2::API::VERSION) {
469 3         24 print STDERR <<" EOT";
470              
471             ********************************************************************************
472             * *
473             * Test::Builder -- Test2::API version mismatch detected *
474             * *
475             ********************************************************************************
476             Test2::API Version: $Test2::API::VERSION
477             Test::Builder Version: $Test::Builder::VERSION
478              
479             This is not a supported configuration, you will have problems.
480              
481             EOT
482             }
483              
484 280         761 for my $ctx (values %{$self->{+CONTEXTS}}) {
  280         2115  
485 30 100       132 next unless $ctx;
486              
487 1 50 33     15 next if $ctx->_aborted && ${$ctx->_aborted};
  0         0  
488              
489             # Only worry about contexts in this PID
490 1   50     15 my $trace = $ctx->trace || next;
491 1 50 33     13 next unless $trace->pid && $trace->pid == $$;
492              
493             # Do not worry about contexts that have no hub
494 1   50     18 my $hub = $ctx->hub || next;
495              
496             # Do not worry if the state came to a sudden end.
497 1 50       7 next if $hub->bailed_out;
498 1 50       12 next if defined $hub->skip_reason;
499              
500             # now we worry
501 1         15 $trace->alert("context object was never released! This means a testing tool is behaving very badly");
502              
503 1         9 $exit = 255;
504 1         4 $new_exit = 255;
505             }
506              
507 280 100 66     5454 if (!defined($self->{+_PID}) or !defined($self->{+_TID}) or $self->{+_PID} != $$ or $self->{+_TID} != get_tid()) {
      100        
      66        
508 31         121 $? = $exit;
509 31         105 return;
510             }
511              
512 249 50       2280 my @hubs = $self->{+STACK} ? $self->{+STACK}->all : ();
513              
514 249 100 100     2650 if (@hubs and $self->{+IPC} and !$self->{+NO_WAIT}) {
      66        
515 18         112 local $?;
516 18         39 my %seen;
517 18         85 for my $hub (reverse @hubs) {
518 18 50       193 my $ipc = $hub->ipc or next;
519 18 50       176 next if $seen{$ipc}++;
520 18         156 $ipc->waiting();
521             }
522              
523 18         113 my $ipc_exit = _ipc_wait($self->{+IPC_TIMEOUT});
524 18   100     200 $new_exit ||= $ipc_exit;
525             }
526              
527             # None of this is necessary if we never got a root hub
528 249 100       1535 if(my $root = shift @hubs) {
529 243         3241 my $trace = Test2::EventFacet::Trace->new(
530             frame => [__PACKAGE__, __FILE__, 0, __PACKAGE__ . '::END'],
531             detail => __PACKAGE__ . ' END Block finalization',
532             );
533 243         1537 my $ctx = Test2::API::Context->new(
534             trace => $trace,
535             hub => $root,
536             );
537              
538 243 100       1146 if (@hubs) {
539 6         57 $ctx->diag("Test ended with extra hubs on the stack!");
540 6         15 $new_exit = 255;
541             }
542              
543 243 100       1412 unless ($root->no_ending) {
544 224         1295 local $?;
545 224 100       1392 $root->finalize($trace) unless $root->ended;
546 224         580 $_->($ctx, $exit, \$new_exit) for @{$self->{+EXIT_CALLBACKS}};
  224         1747  
547 224   100     2100 $new_exit ||= $root->failed;
548 224 100 50     1231 $new_exit ||= 255 unless $root->is_passing;
549             }
550             }
551              
552 249 100       1665 $new_exit = 255 if $new_exit > 255;
553              
554 249 100 66     1815 if ($new_exit && eval { require Test2::API::Breakage; 1 }) {
  24         6989  
  24         131  
555 24         213 my @warn = Test2::API::Breakage->report();
556              
557 24 100       95 if (@warn) {
558 3         15 print STDERR "\nYou have loaded versions of test modules known to have problems with Test2.\nThis could explain some test failures.\n";
559 3         15 print STDERR "$_\n" for @warn;
560 3         12 print STDERR "\n";
561             }
562             }
563              
564 249         206 $? = $new_exit;
565             }
566              
567             1;
568              
569             __END__
570              
571             =pod
572              
573             =encoding UTF-8
574              
575             =head1 NAME
576              
577             Test2::API::Instance - Object used by Test2::API under the hood
578              
579             =head1 DESCRIPTION
580              
581             This object encapsulates the global shared state tracked by
582             L<Test2>. A single global instance of this package is stored (and
583             obscured) by the L<Test2::API> package.
584              
585             There is no reason to directly use this package. This package is documented for
586             completeness. This package can change, or go away completely at any time.
587             Directly using, or monkeypatching this package is not supported in any way
588             shape or form.
589              
590             =head1 SYNOPSIS
591              
592             use Test2::API::Instance;
593              
594             my $obj = Test2::API::Instance->new;
595              
596             =over 4
597              
598             =item $pid = $obj->pid
599              
600             PID of this instance.
601              
602             =item $obj->tid
603              
604             Thread ID of this instance.
605              
606             =item $obj->reset()
607              
608             Reset the object to defaults.
609              
610             =item $obj->load()
611              
612             Set the internal state to loaded, and run and stored post-load callbacks.
613              
614             =item $bool = $obj->loaded
615              
616             Check if the state is set to loaded.
617              
618             =item $arrayref = $obj->post_load_callbacks
619              
620             Get the post-load callbacks.
621              
622             =item $obj->add_post_load_callback(sub { ... })
623              
624             Add a post-load callback. If C<load()> has already been called then the callback will
625             be immediately executed. If C<load()> has not been called then the callback will be
626             stored and executed later when C<load()> is called.
627              
628             =item $hashref = $obj->contexts()
629              
630             Get a hashref of all active contexts keyed by hub id.
631              
632             =item $arrayref = $obj->context_acquire_callbacks
633              
634             Get all context acquire callbacks.
635              
636             =item $arrayref = $obj->context_init_callbacks
637              
638             Get all context init callbacks.
639              
640             =item $arrayref = $obj->context_release_callbacks
641              
642             Get all context release callbacks.
643              
644             =item $arrayref = $obj->pre_subtest_callbacks
645              
646             Get all pre-subtest callbacks.
647              
648             =item $obj->add_context_init_callback(sub { ... })
649              
650             Add a context init callback. Subs are called every time a context is created. Subs
651             get the newly created context as their only argument.
652              
653             =item $obj->add_context_release_callback(sub { ... })
654              
655             Add a context release callback. Subs are called every time a context is released. Subs
656             get the released context as their only argument. These callbacks should not
657             call release on the context.
658              
659             =item $obj->add_pre_subtest_callback(sub { ... })
660              
661             Add a pre-subtest callback. Subs are called every time a subtest is
662             going to be run. Subs get the subtest name, coderef, and any
663             arguments.
664              
665             =item $obj->set_exit()
666              
667             This is intended to be called in an C<END { ... }> block. This will look at
668             test state and set $?. This will also call any end callbacks, and wait on child
669             processes/threads.
670              
671             =item $obj->set_ipc_pending($val)
672              
673             Tell other processes and threads there is a pending event. C<$val> should be a
674             unique value no other thread/process will generate.
675              
676             B<Note:> This will also make the current process see a pending event.
677              
678             =item $pending = $obj->get_ipc_pending()
679              
680             This returns -1 if it is not possible to know.
681              
682             This returns 0 if there are no pending events.
683              
684             This returns 1 if there are pending events.
685              
686             =item $timeout = $obj->ipc_timeout;
687              
688             =item $obj->set_ipc_timeout($timeout);
689              
690             How long to wait for child processes and threads before aborting.
691              
692             =item $drivers = $obj->ipc_drivers
693              
694             Get the list of IPC drivers.
695              
696             =item $obj->add_ipc_driver($DRIVER_CLASS)
697              
698             Add an IPC driver to the list. The most recently added IPC driver will become
699             the global one during initialization. If a driver is added after initialization
700             has occurred a warning will be generated:
701              
702             "IPC driver $driver loaded too late to be used as the global ipc driver"
703              
704             =item $bool = $obj->ipc_polling
705              
706             Check if polling is enabled.
707              
708             =item $obj->enable_ipc_polling
709              
710             Turn on polling. This will cull events from other processes and threads every
711             time a context is created.
712              
713             =item $obj->disable_ipc_polling
714              
715             Turn off IPC polling.
716              
717             =item $bool = $obj->no_wait
718              
719             =item $bool = $obj->set_no_wait($bool)
720              
721             Get/Set no_wait. This option is used to turn off process/thread waiting at exit.
722              
723             =item $arrayref = $obj->exit_callbacks
724              
725             Get the exit callbacks.
726              
727             =item $obj->add_exit_callback(sub { ... })
728              
729             Add an exit callback. This callback will be called by C<set_exit()>.
730              
731             =item $bool = $obj->finalized
732              
733             Check if the object is finalized. Finalization happens when either C<ipc()>,
734             C<stack()>, or C<format()> are called on the object. Once finalization happens
735             these fields are considered unchangeable (not enforced here, enforced by
736             L<Test2>).
737              
738             =item $ipc = $obj->ipc
739              
740             Get the one true IPC instance.
741              
742             =item $obj->ipc_disable
743              
744             Turn IPC off
745              
746             =item $bool = $obj->ipc_disabled
747              
748             Check if IPC is disabled
749              
750             =item $stack = $obj->stack
751              
752             Get the one true hub stack.
753              
754             =item $formatter = $obj->formatter
755              
756             Get the global formatter. By default this is the C<'Test2::Formatter::TAP'>
757             package. This could be any package that implements the C<write()> method. This
758             can also be an instantiated object.
759              
760             =item $bool = $obj->formatter_set()
761              
762             Check if a formatter has been set.
763              
764             =item $obj->add_formatter($class)
765              
766             =item $obj->add_formatter($obj)
767              
768             Add a formatter. The most recently added formatter will become the global one
769             during initialization. If a formatter is added after initialization has occurred
770             a warning will be generated:
771              
772             "Formatter $formatter loaded too late to be used as the global formatter"
773              
774             =item $obj->set_add_uuid_via(sub { ... })
775              
776             =item $sub = $obj->add_uuid_via()
777              
778             This allows you to provide a UUID generator. If provided UUIDs will be attached
779             to all events, hubs, and contexts. This is useful for storing, tracking, and
780             linking these objects.
781              
782             The sub you provide should always return a unique identifier. Most things will
783             expect a proper UUID string, however nothing in Test2::API enforces this.
784              
785             The sub will receive exactly 1 argument, the type of thing being tagged
786             'context', 'hub', or 'event'. In the future additional things may be tagged, in
787             which case new strings will be passed in. These are purely informative, you can
788             (and usually should) ignore them.
789              
790             =back
791              
792             =head1 SOURCE
793              
794             The source code repository for Test2 can be found at
795             F<http://github.com/Test-More/test-more/>.
796              
797             =head1 MAINTAINERS
798              
799             =over 4
800              
801             =item Chad Granum E<lt>exodist@cpan.orgE<gt>
802              
803             =back
804              
805             =head1 AUTHORS
806              
807             =over 4
808              
809             =item Chad Granum E<lt>exodist@cpan.orgE<gt>
810              
811             =back
812              
813             =head1 COPYRIGHT
814              
815             Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
816              
817             This program is free software; you can redistribute it and/or
818             modify it under the same terms as Perl itself.
819              
820             See F<http://dev.perl.org/licenses/>
821              
822             =cut