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   3082 use strict;
  249         513  
  249         7364  
3 249     249   1340 use warnings;
  249         2918  
  246         13796  
4              
5             our $VERSION = '1.302182';
6              
7             our @CARP_NOT = qw/Test2::API Test2::API::Instance Test2::IPC::Driver Test2::Formatter/;
8 246     249   1527 use Carp qw/confess carp/;
  246         2404  
  246         15485  
9 246     249   1716 use Scalar::Util qw/reftype/;
  246         552  
  246         19723  
10              
11 246     249   8220 use Test2::Util qw/get_tid USE_THREADS CAN_FORK pkg_to_file try CAN_SIGSYS/;
  246         507  
  246         15652  
12              
13 246     246   106359 use Test2::EventFacet::Trace();
  246         630  
  246         5373  
14 246     246   103322 use Test2::API::Stack();
  246         702  
  246         10091  
15              
16 246         1200 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   1728 };
  246         484  
40              
41             sub DEFAULT_IPC_TIMEOUT() { 30 }
42              
43 219     219 1 2229 sub pid { $_[0]->{+_PID} }
44 212     212 1 1203 sub tid { $_[0]->{+_TID} }
45              
46             # Wrap around the getters that should call _finalize.
47             BEGIN {
48 246     246   1143 for my $finalizer (IPC, FORMATTER) {
49 492         2301 my $orig = __PACKAGE__->can($finalizer);
50             my $new = sub {
51 715     715   1720 my $self = shift;
52 715 100       3557 $self->_finalize unless $self->{+FINALIZED};
53 709         3043 $self->$orig;
54 492         2426 };
55              
56 246     246   1971 no strict 'refs';
  246         561  
  246         9858  
57 246     246   1501 no warnings 'redefine';
  246         529  
  246         10328  
58 492         992 *{$finalizer} = $new;
  492         815448  
59             }
60             }
61              
62 188     188 0 1203 sub has_ipc { !!$_[0]->{+IPC} }
63              
64             sub import {
65 273     273   700 my $class = shift;
66 273 100       1880 return unless @_;
67 246         628 my ($ref) = @_;
68 246         1077 $$ref = $class->new;
69             }
70              
71 298     298 0 988 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     44 if $self->{+FINALIZED} || $self->{+LOADED};
78              
79 1         38 return $self->{+PRELOAD} = 1;
80             }
81              
82             sub stop_preload {
83 1     1 0 3 my $self = shift;
84              
85 1 50       5 return 0 unless $self->{+PRELOAD};
86 1         3 $self->{+PRELOAD} = 0;
87              
88 1         4 $self->post_preload_reset();
89              
90 1         4 return 1;
91             }
92              
93             sub post_preload_reset {
94 1     1 0 3 my $self = shift;
95              
96 1         3 delete $self->{+_PID};
97 1         3 delete $self->{+_TID};
98              
99 1 50       3 $self->{+ADD_UUID_VIA} = undef unless exists $self->{+ADD_UUID_VIA};
100              
101 1         4 $self->{+CONTEXTS} = {};
102              
103 1         7 $self->{+FORMATTERS} = [];
104              
105 1         3 $self->{+FINALIZED} = undef;
106 1         3 $self->{+IPC} = undef;
107 1 50       8 $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         3 $self->{+LOADED} = 0;
112              
113 1   33     10 $self->{+STACK} ||= Test2::API::Stack->new;
114             }
115              
116             sub reset {
117 355     355 1 810 my $self = shift;
118              
119 355         1603 delete $self->{+_PID};
120 355         657 delete $self->{+_TID};
121              
122 355         848 $self->{+ADD_UUID_VIA} = undef;
123              
124 355         818 $self->{+CONTEXTS} = {};
125              
126 355         788 $self->{+IPC_DRIVERS} = [];
127 355         707 $self->{+IPC_POLLING} = undef;
128              
129 355         825 $self->{+FORMATTERS} = [];
130 355         698 $self->{+FORMATTER} = undef;
131              
132 355         659 $self->{+FINALIZED} = undef;
133 355         1037 $self->{+IPC} = undef;
134 355 100       1739 $self->{+IPC_DISABLED} = $ENV{T2_NO_IPC} ? 1 : 0;
135              
136 355 100       1174 $self->{+IPC_TIMEOUT} = DEFAULT_IPC_TIMEOUT() unless defined $self->{+IPC_TIMEOUT};
137              
138 355         691 $self->{+NO_WAIT} = 0;
139 355         632 $self->{+LOADED} = 0;
140              
141 355         764 $self->{+EXIT_CALLBACKS} = [];
142 355         781 $self->{+POST_LOAD_CALLBACKS} = [];
143 355         815 $self->{+CONTEXT_ACQUIRE_CALLBACKS} = [];
144 355         1158 $self->{+CONTEXT_INIT_CALLBACKS} = [];
145 355         771 $self->{+CONTEXT_RELEASE_CALLBACKS} = [];
146 355         754 $self->{+PRE_SUBTEST_CALLBACKS} = [];
147              
148 355         2366 $self->{+STACK} = Test2::API::Stack->new;
149             }
150              
151             sub _finalize {
152 284     284   2926 my $self = shift;
153 284         905 my ($caller) = @_;
154 284   50     4559 $caller ||= [caller(1)];
155              
156             confess "Attempt to initialize Test2::API during preload"
157 284 50       1426 if $self->{+PRELOAD};
158              
159 284         838 $self->{+FINALIZED} = $caller;
160              
161 284 100       1557 $self->{+_PID} = $$ unless defined $self->{+_PID};
162 284 100       1390 $self->{+_TID} = get_tid() unless defined $self->{+_TID};
163              
164 284 100       1262 unless ($self->{+FORMATTER}) {
165 279         911 my ($formatter, $source);
166 279 100       1247 if ($ENV{T2_FORMATTER}) {
    100          
167 34         112 $source = "set by the 'T2_FORMATTER' environment variable";
168              
169 34 50       365 if ($ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
170 34 100       278 $formatter = $1 ? $2 : "Test2::Formatter::$2"
171             }
172             else {
173 0         0 $formatter = '';
174             }
175             }
176 245         1583 elsif (@{$self->{+FORMATTERS}}) {
177 159         457 ($formatter) = @{$self->{+FORMATTERS}};
  159         640  
178 159         506 $source = "Most recently added";
179             }
180             else {
181 86         216 $formatter = 'Test2::Formatter::TAP';
182 86         213 $source = 'default formatter';
183             }
184              
185 279 100 66     5091 unless (ref($formatter) || $formatter->can('write')) {
186 88         532 my $file = pkg_to_file($formatter);
187 88     88   752 my ($ok, $err) = try { require $file };
  88         43868  
188 88 100       603 unless ($ok) {
189 5         30 my $line = "* COULD NOT LOAD FORMATTER '$formatter' ($source) *";
190 5         25 my $border = '*' x length($line);
191 5         55 die "\n\n $border\n $line\n $border\n\n$err";
192             }
193             }
194              
195 274         1175 $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       1426 return if $self->{+IPC_DISABLED};
201 275 100 100     1922 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         308 $self->enable_ipc_polling;
205              
206 58 100       107 unless (@{$self->{+IPC_DRIVERS}}) {
  58         199  
207 55     55   454 my ($ok, $error) = try { require Test2::IPC::Driver::Files };
  55         13816  
208 55 50       349 die $error unless $ok;
209 55         147 push @{$self->{+IPC_DRIVERS}} => 'Test2::IPC::Driver::Files';
  55         281  
210             }
211              
212 58         174 for my $driver (@{$self->{+IPC_DRIVERS}}) {
  58         174  
213 58 100 66     936 next unless $driver->can('is_viable') && $driver->is_viable;
214 57 50       489 $self->{+IPC} = $driver->new or next;
215 57         192 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 220 sub formatter_set { $_[0]->{+FORMATTER} ? 1 : 0 }
222              
223             sub add_formatter {
224 162     162 1 421 my $self = shift;
225 162         528 my ($formatter) = @_;
226 162         340 unshift @{$self->{+FORMATTERS}} => $formatter;
  162         696  
227              
228 162 100       4396 return unless $self->{+FINALIZED};
229              
230             # Why is the @CARP_NOT entry not enough?
231 2         13 local %Carp::Internal = %Carp::Internal;
232 2         7 $Carp::Internal{'Test2::Formatter'} = 1;
233              
234 2         476 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 472 my $self = shift;
239 165         511 my ($code) = @_;
240              
241 165   50     909 my $rtype = reftype($code) || "";
242              
243 165 50 33     1257 confess "Context-acquire callbacks must be coderefs"
244             unless $code && $rtype eq 'CODE';
245              
246 165         400 push @{$self->{+CONTEXT_ACQUIRE_CALLBACKS}} => $code;
  165         775  
247             }
248              
249             sub add_context_init_callback {
250 62     62 1 132 my $self = shift;
251 62         168 my ($code) = @_;
252              
253 62   50     362 my $rtype = reftype($code) || "";
254              
255 62 50 33     458 confess "Context-init callbacks must be coderefs"
256             unless $code && $rtype eq 'CODE';
257              
258 62         111 push @{$self->{+CONTEXT_INIT_CALLBACKS}} => $code;
  62         230  
259             }
260              
261             sub add_context_release_callback {
262 3     3 1 6 my $self = shift;
263 3         6 my ($code) = @_;
264              
265 3   50     12 my $rtype = reftype($code) || "";
266              
267 3 50 33     17 confess "Context-release callbacks must be coderefs"
268             unless $code && $rtype eq 'CODE';
269              
270 3         5 push @{$self->{+CONTEXT_RELEASE_CALLBACKS}} => $code;
  3         11  
271             }
272              
273             sub add_post_load_callback {
274 180     180 1 477 my $self = shift;
275 180         506 my ($code) = @_;
276              
277 180   50     1369 my $rtype = reftype($code) || "";
278              
279 180 100 66     2686 confess "Post-load callbacks must be coderefs"
280             unless $code && $rtype eq 'CODE';
281              
282 175         430 push @{$self->{+POST_LOAD_CALLBACKS}} => $code;
  175         654  
283 175 100       912 $code->() if $self->{+LOADED};
284             }
285              
286             sub add_pre_subtest_callback {
287 17     17 1 51 my $self = shift;
288 17         46 my ($code) = @_;
289              
290 17   50     86 my $rtype = reftype($code) || "";
291              
292 17 100 66     573 confess "Pre-subtest callbacks must be coderefs"
293             unless $code && $rtype eq 'CODE';
294              
295 12         24 push @{$self->{+PRE_SUBTEST_CALLBACKS}} => $code;
  12         38  
296             }
297              
298             sub load {
299 532     532 1 1408 my $self = shift;
300 532 100       2139 unless ($self->{+LOADED}) {
301             confess "Attempt to initialize Test2::API during preload"
302 265 50       1210 if $self->{+PRELOAD};
303              
304 265 100       2965 $self->{+_PID} = $$ unless defined $self->{+_PID};
305 265 100       1305 $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   21763 eval "END { Test2::API::test2_set_is_end() }; 1" or die $@;
  243         21449  
312              
313 265         1942 $self->{+LOADED} = 1;
314 265         724 $_->() for @{$self->{+POST_LOAD_CALLBACKS}};
  265         1550  
315             }
316 532         2799 return $self->{+LOADED};
317             }
318              
319             sub add_exit_callback {
320 180     180 1 488 my $self = shift;
321 180         565 my ($code) = @_;
322 180   50     945 my $rtype = reftype($code) || "";
323              
324 180 100 66     2052 confess "End callbacks must be coderefs"
325             unless $code && $rtype eq 'CODE';
326              
327 175         439 push @{$self->{+EXIT_CALLBACKS}} => $code;
  175         675  
328             }
329              
330             sub ipc_disable {
331 4     4 1 15 my $self = shift;
332              
333             confess "Attempt to disable IPC after it has been initialized"
334 4 100       238 if $self->{+IPC};
335              
336 3         54 $self->{+IPC_DISABLED} = 1;
337             }
338              
339             sub add_ipc_driver {
340 9     9 1 199 my $self = shift;
341 9         39 my ($driver) = @_;
342 9         18 unshift @{$self->{+IPC_DRIVERS}} => $driver;
  9         45  
343              
344 9 100       4752 return unless $self->{+FINALIZED};
345              
346             # Why is the @CARP_NOT entry not enough?
347 2         31 local %Carp::Internal = %Carp::Internal;
348 2         7 $Carp::Internal{'Test2::IPC::Driver'} = 1;
349              
350 2         618 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 165 my $self = shift;
355              
356 61 100       193 $self->{+_PID} = $$ unless defined $self->{+_PID};
357 61 100       178 $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   1937 return unless $self->{+IPC_POLLING};
364 691 50       1713 return unless $self->{+IPC};
365 691 50       2925 return unless $self->{+IPC}->pending();
366 691         2904 return $_[0]->{hub}->cull;
367             }
368 61 100       394 ) unless defined $self->ipc_polling;
369              
370 61         264 $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 137 my $self = shift;
387 35 100       166 return unless $self->{+IPC};
388 29         115 my ($val) = @_;
389              
390 29 50       132 confess "value is required for set_ipc_pending"
391             unless $val;
392              
393 29         662 $self->{+IPC}->set_pending($val);
394             }
395              
396             sub disable_ipc_polling {
397 3     3 1 12 my $self = shift;
398 3 100       14 return unless defined $self->{+IPC_POLLING};
399 2         8 $self->{+IPC_POLLING} = 0;
400             }
401              
402             sub _ipc_wait {
403 29     29   11577 my ($timeout) = @_;
404 29         532 my $fail = 0;
405              
406 29 100       409 $timeout = DEFAULT_IPC_TIMEOUT() unless defined $timeout;
407              
408 29         190 my $ok = eval {
409 29 50       557 if (CAN_FORK) {
410 29     1   1529 local $SIG{ALRM} = sub { die "Timeout waiting on child processes" };
  1         100  
411 29         510 alarm $timeout;
412              
413 29         116 while (1) {
414 46         35084903 my $pid = CORE::wait();
415 45         493 my $err = $?;
416 45 100       333 last if $pid == -1;
417 17 100       141 next unless $err;
418 9         75 $fail++;
419              
420 9         68 my $sig = $err & 127;
421 9         41 my $exit = $err >> 8;
422 9         724 warn "Process $pid did not exit cleanly (wstat: $err, exit: $exit, sig: $sig)\n";
423             }
424              
425 28         832 alarm 0;
426             }
427              
428 28         108 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         107 1;
452             };
453 29         126 my $error = $@;
454              
455 29 100 100     651 return 0 if $ok && !$fail;
456 10 100       172 warn $error unless $ok;
457 10         227 return 255;
458             }
459              
460             sub set_exit {
461 280     280 1 4802 my $self = shift;
462              
463 280 50       1600 return if $self->{+PRELOAD};
464              
465 280         1027 my $exit = $?;
466 280         801 my $new_exit = $exit;
467              
468 280 100 100     2567 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         831 for my $ctx (values %{$self->{+CONTEXTS}}) {
  280         2157  
485 30 100       130 next unless $ctx;
486              
487 1 50 33     14 next if $ctx->_aborted && ${$ctx->_aborted};
  0         0  
488              
489             # Only worry about contexts in this PID
490 1   50     10 my $trace = $ctx->trace || next;
491 1 50 33     11 next unless $trace->pid && $trace->pid == $$;
492              
493             # Do not worry about contexts that have no hub
494 1   50     12 my $hub = $ctx->hub || next;
495              
496             # Do not worry if the state came to a sudden end.
497 1 50       18 next if $hub->bailed_out;
498 1 50       12 next if defined $hub->skip_reason;
499              
500             # now we worry
501 1         6 $trace->alert("context object was never released! This means a testing tool is behaving very badly");
502              
503 1         2 $exit = 255;
504 1         4 $new_exit = 255;
505             }
506              
507 280 100 66     5600 if (!defined($self->{+_PID}) or !defined($self->{+_TID}) or $self->{+_PID} != $$ or $self->{+_TID} != get_tid()) {
      100        
      66        
508 31         106 $? = $exit;
509 31         113 return;
510             }
511              
512 249 50       2343 my @hubs = $self->{+STACK} ? $self->{+STACK}->all : ();
513              
514 249 100 100     2606 if (@hubs and $self->{+IPC} and !$self->{+NO_WAIT}) {
      66        
515 18         124 local $?;
516 18         79 my %seen;
517 18         92 for my $hub (reverse @hubs) {
518 18 50       200 my $ipc = $hub->ipc or next;
519 18 50       241 next if $seen{$ipc}++;
520 18         161 $ipc->waiting();
521             }
522              
523 18         126 my $ipc_exit = _ipc_wait($self->{+IPC_TIMEOUT});
524 18   100     192 $new_exit ||= $ipc_exit;
525             }
526              
527             # None of this is necessary if we never got a root hub
528 249 100       1558 if(my $root = shift @hubs) {
529 243         3048 my $trace = Test2::EventFacet::Trace->new(
530             frame => [__PACKAGE__, __FILE__, 0, __PACKAGE__ . '::END'],
531             detail => __PACKAGE__ . ' END Block finalization',
532             );
533 243         1493 my $ctx = Test2::API::Context->new(
534             trace => $trace,
535             hub => $root,
536             );
537              
538 243 100       1213 if (@hubs) {
539 6         57 $ctx->diag("Test ended with extra hubs on the stack!");
540 6         12 $new_exit = 255;
541             }
542              
543 243 100       1417 unless ($root->no_ending) {
544 224         977 local $?;
545 224 100       1406 $root->finalize($trace) unless $root->ended;
546 224         587 $_->($ctx, $exit, \$new_exit) for @{$self->{+EXIT_CALLBACKS}};
  224         1790  
547 224   100     2025 $new_exit ||= $root->failed;
548 224 100 50     1267 $new_exit ||= 255 unless $root->is_passing;
549             }
550             }
551              
552 249 100       1765 $new_exit = 255 if $new_exit > 255;
553              
554 249 100 66     1759 if ($new_exit && eval { require Test2::API::Breakage; 1 }) {
  24         6843  
  24         143  
555 24         225 my @warn = Test2::API::Breakage->report();
556              
557 24 100       90 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         18 print STDERR "$_\n" for @warn;
560 3         12 print STDERR "\n";
561             }
562             }
563              
564 249         207 $? = $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