1package Test2::API; 2use strict; 3use warnings; 4 5use Test2::Util qw/USE_THREADS/; 6 7BEGIN { 8 $ENV{TEST_ACTIVE} ||= 1; 9 $ENV{TEST2_ACTIVE} = 1; 10} 11 12our $VERSION = '1.302175'; 13 14 15my $INST; 16my $ENDING = 0; 17sub test2_unset_is_end { $ENDING = 0 } 18sub test2_get_is_end { $ENDING } 19 20sub test2_set_is_end { 21 my $before = $ENDING; 22 ($ENDING) = @_ ? @_ : (1); 23 24 # Only send the event in a transition from false to true 25 return if $before; 26 return unless $ENDING; 27 28 return unless $INST; 29 my $stack = $INST->stack or return; 30 my $root = $stack->root or return; 31 32 return unless $root->count; 33 34 return unless $$ == $INST->pid; 35 return unless get_tid() == $INST->tid; 36 37 my $trace = Test2::EventFacet::Trace->new( 38 frame => [__PACKAGE__, __FILE__, __LINE__, __PACKAGE__ . '::test2_set_is_end'], 39 ); 40 my $ctx = Test2::API::Context->new( 41 trace => $trace, 42 hub => $root, 43 ); 44 45 $ctx->send_ev2(control => { phase => 'END', details => 'Transition to END phase' }); 46 47 1; 48} 49 50use Test2::API::Instance(\$INST); 51 52# Set the exit status 53END { 54 test2_set_is_end(); # See gh #16 55 $INST->set_exit(); 56} 57 58sub CLONE { 59 my $init = test2_init_done(); 60 my $load = test2_load_done(); 61 62 return if $init && $load; 63 64 require Carp; 65 Carp::croak "Test2 must be fully loaded before you start a new thread!\n"; 66} 67 68# See gh #16 69{ 70 no warnings; 71 INIT { eval 'END { test2_set_is_end() }; 1' or die $@ } 72} 73 74BEGIN { 75 no warnings 'once'; 76 if($] ge '5.014' || $ENV{T2_CHECK_DEPTH} || $Test2::API::DO_DEPTH_CHECK) { 77 *DO_DEPTH_CHECK = sub() { 1 }; 78 } 79 else { 80 *DO_DEPTH_CHECK = sub() { 0 }; 81 } 82} 83 84use Test2::EventFacet::Trace(); 85use Test2::Util::Trace(); # Legacy 86 87use Test2::Hub::Subtest(); 88use Test2::Hub::Interceptor(); 89use Test2::Hub::Interceptor::Terminator(); 90 91use Test2::Event::Ok(); 92use Test2::Event::Diag(); 93use Test2::Event::Note(); 94use Test2::Event::Plan(); 95use Test2::Event::Bail(); 96use Test2::Event::Exception(); 97use Test2::Event::Waiting(); 98use Test2::Event::Skip(); 99use Test2::Event::Subtest(); 100 101use Carp qw/carp croak confess/; 102use Scalar::Util qw/blessed weaken/; 103use Test2::Util qw/get_tid clone_io pkg_to_file gen_uid/; 104 105our @EXPORT_OK = qw{ 106 context release 107 context_do 108 no_context 109 intercept intercept_deep 110 run_subtest 111 112 test2_init_done 113 test2_load_done 114 test2_load 115 test2_start_preload 116 test2_stop_preload 117 test2_in_preload 118 test2_is_testing_done 119 120 test2_set_is_end 121 test2_unset_is_end 122 test2_get_is_end 123 124 test2_pid 125 test2_tid 126 test2_stack 127 test2_no_wait 128 test2_ipc_wait_enable 129 test2_ipc_wait_disable 130 test2_ipc_wait_enabled 131 132 test2_add_uuid_via 133 134 test2_add_callback_testing_done 135 136 test2_add_callback_context_aquire 137 test2_add_callback_context_acquire 138 test2_add_callback_context_init 139 test2_add_callback_context_release 140 test2_add_callback_exit 141 test2_add_callback_post_load 142 test2_add_callback_pre_subtest 143 test2_list_context_aquire_callbacks 144 test2_list_context_acquire_callbacks 145 test2_list_context_init_callbacks 146 test2_list_context_release_callbacks 147 test2_list_exit_callbacks 148 test2_list_post_load_callbacks 149 test2_list_pre_subtest_callbacks 150 151 test2_ipc 152 test2_has_ipc 153 test2_ipc_disable 154 test2_ipc_disabled 155 test2_ipc_drivers 156 test2_ipc_add_driver 157 test2_ipc_polling 158 test2_ipc_disable_polling 159 test2_ipc_enable_polling 160 test2_ipc_get_pending 161 test2_ipc_set_pending 162 test2_ipc_get_timeout 163 test2_ipc_set_timeout 164 165 test2_formatter 166 test2_formatters 167 test2_formatter_add 168 test2_formatter_set 169 170 test2_stdout 171 test2_stderr 172 test2_reset_io 173}; 174BEGIN { require Exporter; our @ISA = qw(Exporter) } 175 176my $STACK = $INST->stack; 177my $CONTEXTS = $INST->contexts; 178my $INIT_CBS = $INST->context_init_callbacks; 179my $ACQUIRE_CBS = $INST->context_acquire_callbacks; 180 181my $STDOUT = clone_io(\*STDOUT); 182my $STDERR = clone_io(\*STDERR); 183sub test2_stdout { $STDOUT ||= clone_io(\*STDOUT) } 184sub test2_stderr { $STDERR ||= clone_io(\*STDERR) } 185 186sub test2_post_preload_reset { 187 test2_reset_io(); 188 $INST->post_preload_reset; 189} 190 191sub test2_reset_io { 192 $STDOUT = clone_io(\*STDOUT); 193 $STDERR = clone_io(\*STDERR); 194} 195 196sub test2_init_done { $INST->finalized } 197sub test2_load_done { $INST->loaded } 198 199sub test2_load { $INST->load } 200sub test2_start_preload { $ENV{T2_IN_PRELOAD} = 1; $INST->start_preload } 201sub test2_stop_preload { $ENV{T2_IN_PRELOAD} = 0; $INST->stop_preload } 202sub test2_in_preload { $INST->preload } 203 204sub test2_pid { $INST->pid } 205sub test2_tid { $INST->tid } 206sub test2_stack { $INST->stack } 207sub test2_ipc_wait_enable { $INST->set_no_wait(0) } 208sub test2_ipc_wait_disable { $INST->set_no_wait(1) } 209sub test2_ipc_wait_enabled { !$INST->no_wait } 210 211sub test2_is_testing_done { 212 # No instance? VERY DONE! 213 return 1 unless $INST; 214 215 # No stack? tests must be done, it is created pretty early 216 my $stack = $INST->stack or return 1; 217 218 # Nothing on the stack, no root hub yet, likely have not started testing 219 return 0 unless @$stack; 220 221 # Stack has a slot for the root hub (see above) but it is undefined, likely 222 # garbage collected, test is done 223 my $root_hub = $stack->[0] or return 1; 224 225 # If the root hub is ended than testing is done. 226 return 1 if $root_hub->ended; 227 228 # Looks like we are still testing! 229 return 0; 230} 231 232sub test2_no_wait { 233 $INST->set_no_wait(@_) if @_; 234 $INST->no_wait; 235} 236 237sub test2_add_callback_testing_done { 238 my $cb = shift; 239 240 test2_add_callback_post_load(sub { 241 my $stack = test2_stack(); 242 $stack->top; # Insure we have a hub 243 my ($hub) = Test2::API::test2_stack->all; 244 245 $hub->set_active(1); 246 247 $hub->follow_up($cb); 248 }); 249 250 return; 251} 252 253sub test2_add_callback_context_acquire { $INST->add_context_acquire_callback(@_) } 254sub test2_add_callback_context_aquire { $INST->add_context_acquire_callback(@_) } 255sub test2_add_callback_context_init { $INST->add_context_init_callback(@_) } 256sub test2_add_callback_context_release { $INST->add_context_release_callback(@_) } 257sub test2_add_callback_exit { $INST->add_exit_callback(@_) } 258sub test2_add_callback_post_load { $INST->add_post_load_callback(@_) } 259sub test2_add_callback_pre_subtest { $INST->add_pre_subtest_callback(@_) } 260sub test2_list_context_aquire_callbacks { @{$INST->context_acquire_callbacks} } 261sub test2_list_context_acquire_callbacks { @{$INST->context_acquire_callbacks} } 262sub test2_list_context_init_callbacks { @{$INST->context_init_callbacks} } 263sub test2_list_context_release_callbacks { @{$INST->context_release_callbacks} } 264sub test2_list_exit_callbacks { @{$INST->exit_callbacks} } 265sub test2_list_post_load_callbacks { @{$INST->post_load_callbacks} } 266sub test2_list_pre_subtest_callbacks { @{$INST->pre_subtest_callbacks} } 267 268sub test2_add_uuid_via { 269 $INST->set_add_uuid_via(@_) if @_; 270 $INST->add_uuid_via(); 271} 272 273sub test2_ipc { $INST->ipc } 274sub test2_has_ipc { $INST->has_ipc } 275sub test2_ipc_disable { $INST->ipc_disable } 276sub test2_ipc_disabled { $INST->ipc_disabled } 277sub test2_ipc_add_driver { $INST->add_ipc_driver(@_) } 278sub test2_ipc_drivers { @{$INST->ipc_drivers} } 279sub test2_ipc_polling { $INST->ipc_polling } 280sub test2_ipc_enable_polling { $INST->enable_ipc_polling } 281sub test2_ipc_disable_polling { $INST->disable_ipc_polling } 282sub test2_ipc_get_pending { $INST->get_ipc_pending } 283sub test2_ipc_set_pending { $INST->set_ipc_pending(@_) } 284sub test2_ipc_set_timeout { $INST->set_ipc_timeout(@_) } 285sub test2_ipc_get_timeout { $INST->ipc_timeout() } 286sub test2_ipc_enable_shm { 0 } 287 288sub test2_formatter { 289 if ($ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) { 290 my $formatter = $1 ? $2 : "Test2::Formatter::$2"; 291 my $file = pkg_to_file($formatter); 292 require $file; 293 return $formatter; 294 } 295 296 return $INST->formatter; 297} 298 299sub test2_formatters { @{$INST->formatters} } 300sub test2_formatter_add { $INST->add_formatter(@_) } 301sub test2_formatter_set { 302 my ($formatter) = @_; 303 croak "No formatter specified" unless $formatter; 304 croak "Global Formatter already set" if $INST->formatter_set; 305 $INST->set_formatter($formatter); 306} 307 308# Private, for use in Test2::API::Context 309sub _contexts_ref { $INST->contexts } 310sub _context_acquire_callbacks_ref { $INST->context_acquire_callbacks } 311sub _context_init_callbacks_ref { $INST->context_init_callbacks } 312sub _context_release_callbacks_ref { $INST->context_release_callbacks } 313sub _add_uuid_via_ref { \($INST->{Test2::API::Instance::ADD_UUID_VIA()}) } 314 315# Private, for use in Test2::IPC 316sub _set_ipc { $INST->set_ipc(@_) } 317 318sub context_do(&;@) { 319 my $code = shift; 320 my @args = @_; 321 322 my $ctx = context(level => 1); 323 324 my $want = wantarray; 325 326 my @out; 327 my $ok = eval { 328 $want ? @out = $code->($ctx, @args) : 329 defined($want) ? $out[0] = $code->($ctx, @args) : 330 $code->($ctx, @args) ; 331 1; 332 }; 333 my $err = $@; 334 335 $ctx->release; 336 337 die $err unless $ok; 338 339 return @out if $want; 340 return $out[0] if defined $want; 341 return; 342} 343 344sub no_context(&;$) { 345 my ($code, $hid) = @_; 346 $hid ||= $STACK->top->hid; 347 348 my $ctx = $CONTEXTS->{$hid}; 349 delete $CONTEXTS->{$hid}; 350 my $ok = eval { $code->(); 1 }; 351 my $err = $@; 352 353 $CONTEXTS->{$hid} = $ctx; 354 weaken($CONTEXTS->{$hid}); 355 356 die $err unless $ok; 357 358 return; 359}; 360 361my $UUID_VIA = _add_uuid_via_ref(); 362sub context { 363 # We need to grab these before anything else to ensure they are not 364 # changed. 365 my ($errno, $eval_error, $child_error, $extended_error) = (0 + $!, $@, $?, $^E); 366 367 my %params = (level => 0, wrapped => 0, @_); 368 369 # If something is getting a context then the sync system needs to be 370 # considered loaded... 371 $INST->load unless $INST->{loaded}; 372 373 croak "context() called, but return value is ignored" 374 unless defined wantarray; 375 376 my $stack = $params{stack} || $STACK; 377 my $hub = $params{hub} || (@$stack ? $stack->[-1] : $stack->top); 378 379 # Catch an edge case where we try to get context after the root hub has 380 # been garbage collected resulting in a stack that has a single undef 381 # hub 382 if (!$hub && !exists($params{hub}) && @$stack) { 383 my $msg = Carp::longmess("Attempt to get Test2 context after testing has completed (did you attempt a testing event after done_testing?)"); 384 385 # The error message is usually masked by the global destruction, so we have to print to STDER 386 print STDERR $msg; 387 388 # Make sure this is a failure, we are probably already in END, so set $? to change the exit code 389 $? = 1; 390 391 # Now we actually die to interrupt the program flow and avoid undefined his warnings 392 die $msg; 393 } 394 395 my $hid = $hub->{hid}; 396 my $current = $CONTEXTS->{$hid}; 397 398 $_->(\%params) for @$ACQUIRE_CBS; 399 map $_->(\%params), @{$hub->{_context_acquire}} if $hub->{_context_acquire}; 400 401 # This is for https://github.com/Test-More/test-more/issues/16 402 # and https://rt.perl.org/Public/Bug/Display.html?id=127774 403 my $phase = ${^GLOBAL_PHASE} || 'NA'; 404 my $end_phase = $ENDING || $phase eq 'END' || $phase eq 'DESTRUCT'; 405 406 my $level = 1 + $params{level}; 407 my ($pkg, $file, $line, $sub) = $end_phase ? caller(0) : caller($level); 408 unless ($pkg || $end_phase) { 409 confess "Could not find context at depth $level" unless $params{fudge}; 410 ($pkg, $file, $line, $sub) = caller(--$level) while ($level >= 0 && !$pkg); 411 } 412 413 my $depth = $level; 414 $depth++ while DO_DEPTH_CHECK && !$end_phase && (!$current || $depth <= $current->{_depth} + $params{wrapped}) && caller($depth + 1); 415 $depth -= $params{wrapped}; 416 my $depth_ok = !DO_DEPTH_CHECK || $end_phase || !$current || $current->{_depth} < $depth; 417 418 if ($current && $params{on_release} && $depth_ok) { 419 $current->{_on_release} ||= []; 420 push @{$current->{_on_release}} => $params{on_release}; 421 } 422 423 # I know this is ugly.... 424 ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error) and return bless( 425 { 426 %$current, 427 _is_canon => undef, 428 errno => $errno, 429 eval_error => $eval_error, 430 child_error => $child_error, 431 _is_spawn => [$pkg, $file, $line, $sub], 432 }, 433 'Test2::API::Context' 434 ) if $current && $depth_ok; 435 436 # Handle error condition of bad level 437 if ($current) { 438 unless (${$current->{_aborted}}) { 439 _canon_error($current, [$pkg, $file, $line, $sub, $depth]) 440 unless $current->{_is_canon}; 441 442 _depth_error($current, [$pkg, $file, $line, $sub, $depth]) 443 unless $depth_ok; 444 } 445 446 $current->release if $current->{_is_canon}; 447 448 delete $CONTEXTS->{$hid}; 449 } 450 451 # Directly bless the object here, calling new is a noticeable performance 452 # hit with how often this needs to be called. 453 my $trace = bless( 454 { 455 frame => [$pkg, $file, $line, $sub], 456 pid => $$, 457 tid => get_tid(), 458 cid => gen_uid(), 459 hid => $hid, 460 nested => $hub->{nested}, 461 buffered => $hub->{buffered}, 462 463 $$UUID_VIA ? ( 464 huuid => $hub->{uuid}, 465 uuid => ${$UUID_VIA}->('context'), 466 ) : (), 467 }, 468 'Test2::EventFacet::Trace' 469 ); 470 471 # Directly bless the object here, calling new is a noticeable performance 472 # hit with how often this needs to be called. 473 my $aborted = 0; 474 $current = bless( 475 { 476 _aborted => \$aborted, 477 stack => $stack, 478 hub => $hub, 479 trace => $trace, 480 _is_canon => 1, 481 _depth => $depth, 482 errno => $errno, 483 eval_error => $eval_error, 484 child_error => $child_error, 485 $params{on_release} ? (_on_release => [$params{on_release}]) : (), 486 }, 487 'Test2::API::Context' 488 ); 489 490 $CONTEXTS->{$hid} = $current; 491 weaken($CONTEXTS->{$hid}); 492 493 $_->($current) for @$INIT_CBS; 494 map $_->($current), @{$hub->{_context_init}} if $hub->{_context_init}; 495 496 $params{on_init}->($current) if $params{on_init}; 497 498 ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error); 499 500 return $current; 501} 502 503sub _depth_error { 504 _existing_error(@_, <<" EOT"); 505context() was called to retrieve an existing context, however the existing 506context was created in a stack frame at the same, or deeper level. This usually 507means that a tool failed to release the context when it was finished. 508 EOT 509} 510 511sub _canon_error { 512 _existing_error(@_, <<" EOT"); 513context() was called to retrieve an existing context, however the existing 514context has an invalid internal state (!_canon_count). This should not normally 515happen unless something is mucking about with internals... 516 EOT 517} 518 519sub _existing_error { 520 my ($ctx, $details, $msg) = @_; 521 my ($pkg, $file, $line, $sub, $depth) = @$details; 522 523 my $oldframe = $ctx->{trace}->frame; 524 my $olddepth = $ctx->{_depth}; 525 526 # Older versions of Carp do not export longmess() function, so it needs to be called with package name 527 my $mess = Carp::longmess(); 528 529 warn <<" EOT"; 530$msg 531Old context details: 532 File: $oldframe->[1] 533 Line: $oldframe->[2] 534 Tool: $oldframe->[3] 535 Depth: $olddepth 536 537New context details: 538 File: $file 539 Line: $line 540 Tool: $sub 541 Depth: $depth 542 543Trace: $mess 544 545Removing the old context and creating a new one... 546 EOT 547} 548 549sub release($;$) { 550 $_[0]->release; 551 return $_[1]; 552} 553 554sub intercept(&) { 555 my $code = shift; 556 my $ctx = context(); 557 558 my $events = _intercept($code, deep => 0); 559 560 $ctx->release; 561 562 return $events; 563} 564 565sub intercept_deep(&) { 566 my $code = shift; 567 my $ctx = context(); 568 569 my $events = _intercept($code, deep => 1); 570 571 $ctx->release; 572 573 return $events; 574} 575 576sub _intercept { 577 my $code = shift; 578 my %params = @_; 579 my $ctx = context(); 580 581 my $ipc; 582 if (my $global_ipc = test2_ipc()) { 583 my $driver = blessed($global_ipc); 584 $ipc = $driver->new; 585 } 586 587 my $hub = Test2::Hub::Interceptor->new( 588 ipc => $ipc, 589 no_ending => 1, 590 ); 591 592 my @events; 593 $hub->listen(sub { push @events => $_[1] }, inherit => $params{deep}); 594 595 $ctx->stack->top; # Make sure there is a top hub before we begin. 596 $ctx->stack->push($hub); 597 598 my ($ok, $err) = (1, undef); 599 T2_SUBTEST_WRAPPER: { 600 # Do not use 'try' cause it localizes __DIE__ 601 $ok = eval { $code->(hub => $hub, context => $ctx->snapshot); 1 }; 602 $err = $@; 603 604 # They might have done 'BEGIN { skip_all => "whatever" }' 605 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && $err->isa('Test2::Hub::Interceptor::Terminator'))) { 606 $ok = 1; 607 $err = undef; 608 } 609 } 610 611 $hub->cull; 612 $ctx->stack->pop($hub); 613 614 my $trace = $ctx->trace; 615 $ctx->release; 616 617 die $err unless $ok; 618 619 $hub->finalize($trace, 1) 620 if $ok 621 && !$hub->no_ending 622 && !$hub->ended; 623 624 return \@events; 625} 626 627sub run_subtest { 628 my ($name, $code, $params, @args) = @_; 629 630 $_->($name,$code,@args) 631 for Test2::API::test2_list_pre_subtest_callbacks(); 632 633 $params = {buffered => $params} unless ref $params; 634 my $inherit_trace = delete $params->{inherit_trace}; 635 636 my $ctx = context(); 637 638 my $parent = $ctx->hub; 639 640 # If a parent is buffered then the child must be as well. 641 my $buffered = $params->{buffered} || $parent->{buffered}; 642 643 $ctx->note($name) unless $buffered; 644 645 my $stack = $ctx->stack || $STACK; 646 my $hub = $stack->new_hub( 647 class => 'Test2::Hub::Subtest', 648 %$params, 649 buffered => $buffered, 650 ); 651 652 my @events; 653 $hub->listen(sub { push @events => $_[1] }); 654 655 if ($buffered) { 656 if (my $format = $hub->format) { 657 my $hide = $format->can('hide_buffered') ? $format->hide_buffered : 1; 658 $hub->format(undef) if $hide; 659 } 660 } 661 662 if ($inherit_trace) { 663 my $orig = $code; 664 $code = sub { 665 my $base_trace = $ctx->trace; 666 my $trace = $base_trace->snapshot(nested => 1 + $base_trace->nested); 667 my $st_ctx = Test2::API::Context->new( 668 trace => $trace, 669 hub => $hub, 670 ); 671 $st_ctx->do_in_context($orig, @args); 672 }; 673 } 674 675 my ($ok, $err, $finished); 676 T2_SUBTEST_WRAPPER: { 677 # Do not use 'try' cause it localizes __DIE__ 678 $ok = eval { $code->(@args); 1 }; 679 $err = $@; 680 681 # They might have done 'BEGIN { skip_all => "whatever" }' 682 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) { 683 $ok = undef; 684 $err = undef; 685 } 686 else { 687 $finished = 1; 688 } 689 } 690 691 if ($params->{no_fork}) { 692 if ($$ != $ctx->trace->pid) { 693 warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err; 694 exit 255; 695 } 696 697 if (get_tid() != $ctx->trace->tid) { 698 warn $ok ? "Started new thread inside subtest, but thread never finished!\n" : $err; 699 exit 255; 700 } 701 } 702 elsif (!$parent->is_local && !$parent->ipc) { 703 warn $ok ? "A new process or thread was started inside subtest, but IPC is not enabled!\n" : $err; 704 exit 255; 705 } 706 707 $stack->pop($hub); 708 709 my $trace = $ctx->trace; 710 711 my $bailed = $hub->bailed_out; 712 713 if (!$finished) { 714 if ($bailed && !$buffered) { 715 $ctx->bail($bailed->reason); 716 } 717 elsif ($bailed && $buffered) { 718 $ok = 1; 719 } 720 else { 721 my $code = $hub->exit_code; 722 $ok = !$code; 723 $err = "Subtest ended with exit code $code" if $code; 724 } 725 } 726 727 $hub->finalize($trace->snapshot(huuid => $hub->uuid, hid => $hub->hid, nested => $hub->nested, buffered => $buffered), 1) 728 if $ok 729 && !$hub->no_ending 730 && !$hub->ended; 731 732 my $pass = $ok && $hub->is_passing; 733 my $e = $ctx->build_event( 734 'Subtest', 735 pass => $pass, 736 name => $name, 737 subtest_id => $hub->id, 738 subtest_uuid => $hub->uuid, 739 buffered => $buffered, 740 subevents => \@events, 741 ); 742 743 my $plan_ok = $hub->check_plan; 744 745 $ctx->hub->send($e); 746 747 $ctx->failure_diag($e) unless $e->pass; 748 749 $ctx->diag("Caught exception in subtest: $err") unless $ok; 750 751 $ctx->diag("Bad subtest plan, expected " . $hub->plan . " but ran " . $hub->count) 752 if defined($plan_ok) && !$plan_ok; 753 754 $ctx->bail($bailed->reason) if $bailed && $buffered; 755 756 $ctx->release; 757 return $pass; 758} 759 760# There is a use-cycle between API and API/Context. Context needs to use some 761# API functions as the package is compiling. Test2::API::context() needs 762# Test2::API::Context to be loaded, but we cannot 'require' the module there as 763# it causes a very noticeable performance impact with how often context() is 764# called. 765require Test2::API::Context; 766 7671; 768 769__END__ 770 771=pod 772 773=encoding UTF-8 774 775=head1 NAME 776 777Test2::API - Primary interface for writing Test2 based testing tools. 778 779=head1 ***INTERNALS NOTE*** 780 781B<The internals of this package are subject to change at any time!> The public 782methods provided will not change in backwards-incompatible ways (once there is 783a stable release), but the underlying implementation details might. 784B<Do not break encapsulation here!> 785 786Currently the implementation is to create a single instance of the 787L<Test2::API::Instance> Object. All class methods defer to the single 788instance. There is no public access to the singleton, and that is intentional. 789The class methods provided by this package provide the only functionality 790publicly exposed. 791 792This is done primarily to avoid the problems Test::Builder had by exposing its 793singleton. We do not want anyone to replace this singleton, rebless it, or 794directly muck with its internals. If you need to do something and cannot 795because of the restrictions placed here, then please report it as an issue. If 796possible, we will create a way for you to implement your functionality without 797exposing things that should not be exposed. 798 799=head1 DESCRIPTION 800 801This package exports all the functions necessary to write and/or verify testing 802tools. Using these building blocks you can begin writing test tools very 803quickly. You are also provided with tools that help you to test the tools you 804write. 805 806=head1 SYNOPSIS 807 808=head2 WRITING A TOOL 809 810The C<context()> method is your primary interface into the Test2 framework. 811 812 package My::Ok; 813 use Test2::API qw/context/; 814 815 our @EXPORT = qw/my_ok/; 816 use base 'Exporter'; 817 818 # Just like ok() from Test::More 819 sub my_ok($;$) { 820 my ($bool, $name) = @_; 821 my $ctx = context(); # Get a context 822 $ctx->ok($bool, $name); 823 $ctx->release; # Release the context 824 return $bool; 825 } 826 827See L<Test2::API::Context> for a list of methods available on the context object. 828 829=head2 TESTING YOUR TOOLS 830 831The C<intercept { ... }> tool lets you temporarily intercept all events 832generated by the test system: 833 834 use Test2::API qw/intercept/; 835 836 use My::Ok qw/my_ok/; 837 838 my $events = intercept { 839 # These events are not displayed 840 my_ok(1, "pass"); 841 my_ok(0, "fail"); 842 }; 843 844 my_ok(@$events == 2, "got 2 events, the pass and the fail"); 845 my_ok($events->[0]->pass, "first event passed"); 846 my_ok(!$events->[1]->pass, "second event failed"); 847 848=head3 DEEP EVENT INTERCEPTION 849 850Normally C<intercept { ... }> only intercepts events sent to the main hub (as 851added by intercept itself). Nested hubs, such as those created by subtests, 852will not be intercepted. This is normally what you will still see the nested 853events by inspecting the subtest event. However there are times where you want 854to verify each event as it is sent, in that case use C<intercept_deep { ... }>. 855 856 my $events = intercept_Deep { 857 buffered_subtest foo => sub { 858 ok(1, "pass"); 859 }; 860 }; 861 862C<$events> in this case will contain 3 items: 863 864=over 4 865 866=item The event from C<ok(1, "pass")> 867 868=item The plan event for the subtest 869 870=item The subtest event itself, with the first 2 events nested inside it as children. 871 872=back 873 874This lets you see the order in which the events were sent, unlike 875C<intercept { ... }> which only lets you see events as the main hub sees them. 876 877=head2 OTHER API FUNCTIONS 878 879 use Test2::API qw{ 880 test2_init_done 881 test2_stack 882 test2_set_is_end 883 test2_get_is_end 884 test2_ipc 885 test2_formatter_set 886 test2_formatter 887 test2_is_testing_done 888 }; 889 890 my $init = test2_init_done(); 891 my $stack = test2_stack(); 892 my $ipc = test2_ipc(); 893 894 test2_formatter_set($FORMATTER) 895 my $formatter = test2_formatter(); 896 897 ... And others ... 898 899=head1 MAIN API EXPORTS 900 901All exports are optional. You must specify subs to import. 902 903 use Test2::API qw/context intercept run_subtest/; 904 905This is the list of exports that are most commonly needed. If you are simply 906writing a tool, then this is probably all you need. If you need something and 907you cannot find it here, then you can also look at L</OTHER API EXPORTS>. 908 909These exports lack the 'test2_' prefix because of how important/common they 910are. Exports in the L</OTHER API EXPORTS> section have the 'test2_' prefix to 911ensure they stand out. 912 913=head2 context(...) 914 915Usage: 916 917=over 4 918 919=item $ctx = context() 920 921=item $ctx = context(%params) 922 923=back 924 925The C<context()> function will always return the current context. If 926there is already a context active, it will be returned. If there is not an 927active context, one will be generated. When a context is generated it will 928default to using the file and line number where the currently running sub was 929called from. 930 931Please see L<Test2::API::Context/"CRITICAL DETAILS"> for important rules about 932what you can and cannot do with a context once it is obtained. 933 934B<Note> This function will throw an exception if you ignore the context object 935it returns. 936 937B<Note> On perls 5.14+ a depth check is used to insure there are no context 938leaks. This cannot be safely done on older perls due to 939L<https://rt.perl.org/Public/Bug/Display.html?id=127774> 940You can forcefully enable it either by setting C<$ENV{T2_CHECK_DEPTH} = 1> or 941C<$Test2::API::DO_DEPTH_CHECK = 1> B<BEFORE> loading L<Test2::API>. 942 943=head3 OPTIONAL PARAMETERS 944 945All parameters to C<context> are optional. 946 947=over 4 948 949=item level => $int 950 951If you must obtain a context in a sub deeper than your entry point you can use 952this to tell it how many EXTRA stack frames to look back. If this option is not 953provided the default of C<0> is used. 954 955 sub third_party_tool { 956 my $sub = shift; 957 ... # Does not obtain a context 958 $sub->(); 959 ... 960 } 961 962 third_party_tool(sub { 963 my $ctx = context(level => 1); 964 ... 965 $ctx->release; 966 }); 967 968=item wrapped => $int 969 970Use this if you need to write your own tool that wraps a call to C<context()> 971with the intent that it should return a context object. 972 973 sub my_context { 974 my %params = ( wrapped => 0, @_ ); 975 $params{wrapped}++; 976 my $ctx = context(%params); 977 ... 978 return $ctx; 979 } 980 981 sub my_tool { 982 my $ctx = my_context(); 983 ... 984 $ctx->release; 985 } 986 987If you do not do this, then tools you call that also check for a context will 988notice that the context they grabbed was created at the same stack depth, which 989will trigger protective measures that warn you and destroy the existing 990context. 991 992=item stack => $stack 993 994Normally C<context()> looks at the global hub stack. If you are maintaining 995your own L<Test2::API::Stack> instance you may pass it in to be used 996instead of the global one. 997 998=item hub => $hub 999 1000Use this parameter if you want to obtain the context for a specific hub instead 1001of whatever one happens to be at the top of the stack. 1002 1003=item on_init => sub { ... } 1004 1005This lets you provide a callback sub that will be called B<ONLY> if your call 1006to C<context()> generated a new context. The callback B<WILL NOT> be called if 1007C<context()> is returning an existing context. The only argument passed into 1008the callback will be the context object itself. 1009 1010 sub foo { 1011 my $ctx = context(on_init => sub { 'will run' }); 1012 1013 my $inner = sub { 1014 # This callback is not run since we are getting the existing 1015 # context from our parent sub. 1016 my $ctx = context(on_init => sub { 'will NOT run' }); 1017 $ctx->release; 1018 } 1019 $inner->(); 1020 1021 $ctx->release; 1022 } 1023 1024=item on_release => sub { ... } 1025 1026This lets you provide a callback sub that will be called when the context 1027instance is released. This callback will be added to the returned context even 1028if an existing context is returned. If multiple calls to context add callbacks, 1029then all will be called in reverse order when the context is finally released. 1030 1031 sub foo { 1032 my $ctx = context(on_release => sub { 'will run second' }); 1033 1034 my $inner = sub { 1035 my $ctx = context(on_release => sub { 'will run first' }); 1036 1037 # Neither callback runs on this release 1038 $ctx->release; 1039 } 1040 $inner->(); 1041 1042 # Both callbacks run here. 1043 $ctx->release; 1044 } 1045 1046=back 1047 1048=head2 release($;$) 1049 1050Usage: 1051 1052=over 4 1053 1054=item release $ctx; 1055 1056=item release $ctx, ...; 1057 1058=back 1059 1060This is intended as a shortcut that lets you release your context and return a 1061value in one statement. This function will get your context, and an optional 1062return value. It will release your context, then return your value. Scalar 1063context is always assumed. 1064 1065 sub tool { 1066 my $ctx = context(); 1067 ... 1068 1069 return release $ctx, 1; 1070 } 1071 1072This tool is most useful when you want to return the value you get from calling 1073a function that needs to see the current context: 1074 1075 my $ctx = context(); 1076 my $out = some_tool(...); 1077 $ctx->release; 1078 return $out; 1079 1080We can combine the last 3 lines of the above like so: 1081 1082 my $ctx = context(); 1083 release $ctx, some_tool(...); 1084 1085=head2 context_do(&;@) 1086 1087Usage: 1088 1089 sub my_tool { 1090 context_do { 1091 my $ctx = shift; 1092 1093 my (@args) = @_; 1094 1095 $ctx->ok(1, "pass"); 1096 1097 ... 1098 1099 # No need to call $ctx->release, done for you on scope exit. 1100 } @_; 1101 } 1102 1103Using this inside your test tool takes care of a lot of boilerplate for you. It 1104will ensure a context is acquired. It will capture and rethrow any exception. It 1105will insure the context is released when you are done. It preserves the 1106subroutine call context (array, scalar, void). 1107 1108This is the safest way to write a test tool. The only two downsides to this are a 1109slight performance decrease, and some extra indentation in your source. If the 1110indentation is a problem for you then you can take a peek at the next section. 1111 1112=head2 no_context(&;$) 1113 1114Usage: 1115 1116=over 4 1117 1118=item no_context { ... }; 1119 1120=item no_context { ... } $hid; 1121 1122 sub my_tool(&) { 1123 my $code = shift; 1124 my $ctx = context(); 1125 ... 1126 1127 no_context { 1128 # Things in here will not see our current context, they get a new 1129 # one. 1130 1131 $code->(); 1132 }; 1133 1134 ... 1135 $ctx->release; 1136 }; 1137 1138=back 1139 1140This tool will hide a context for the provided block of code. This means any 1141tools run inside the block will get a completely new context if they acquire 1142one. The new context will be inherited by tools nested below the one that 1143acquired it. 1144 1145This will normally hide the current context for the top hub. If you need to 1146hide the context for a different hub you can pass in the optional C<$hid> 1147parameter. 1148 1149=head2 intercept(&) 1150 1151Usage: 1152 1153 my $events = intercept { 1154 ok(1, "pass"); 1155 ok(0, "fail"); 1156 ... 1157 }; 1158 1159This function takes a codeblock as its only argument, and it has a prototype. 1160It will execute the codeblock, intercepting any generated events in the 1161process. It will return an array reference with all the generated event 1162objects. All events should be subclasses of L<Test2::Event>. 1163 1164This is a very low-level subtest tool. This is useful for writing tools which 1165produce subtests. This is not intended for people simply writing tests. 1166 1167=head2 run_subtest(...) 1168 1169Usage: 1170 1171 run_subtest($NAME, \&CODE, $BUFFERED, @ARGS) 1172 1173 # or 1174 1175 run_subtest($NAME, \&CODE, \%PARAMS, @ARGS) 1176 1177This will run the provided codeblock with the args in C<@args>. This codeblock 1178will be run as a subtest. A subtest is an isolated test state that is condensed 1179into a single L<Test2::Event::Subtest> event, which contains all events 1180generated inside the subtest. 1181 1182=head3 ARGUMENTS: 1183 1184=over 4 1185 1186=item $NAME 1187 1188The name of the subtest. 1189 1190=item \&CODE 1191 1192The code to run inside the subtest. 1193 1194=item $BUFFERED or \%PARAMS 1195 1196If this is a simple scalar then it will be treated as a boolean for the 1197'buffered' setting. If this is a hash reference then it will be used as a 1198parameters hash. The param hash will be used for hub construction (with the 1199specified keys removed). 1200 1201Keys that are removed and used by run_subtest: 1202 1203=over 4 1204 1205=item 'buffered' => $bool 1206 1207Toggle buffered status. 1208 1209=item 'inherit_trace' => $bool 1210 1211Normally the subtest hub is pushed and the sub is allowed to generate its own 1212root context for the hub. When this setting is turned on a root context will be 1213created for the hub that shares the same trace as the current context. 1214 1215Set this to true if your tool is producing subtests without user-specified 1216subs. 1217 1218=item 'no_fork' => $bool 1219 1220Defaults to off. Normally forking inside a subtest will actually fork the 1221subtest, resulting in 2 final subtest events. This parameter will turn off that 1222behavior, only the original process/thread will return a final subtest event. 1223 1224=back 1225 1226=item @ARGS 1227 1228Any extra arguments you want passed into the subtest code. 1229 1230=back 1231 1232=head3 BUFFERED VS UNBUFFERED (OR STREAMED) 1233 1234Normally all events inside and outside a subtest are sent to the formatter 1235immediately by the hub. Sometimes it is desirable to hold off sending events 1236within a subtest until the subtest is complete. This usually depends on the 1237formatter being used. 1238 1239=over 4 1240 1241=item Things not effected by this flag 1242 1243In both cases events are generated and stored in an array. This array is 1244eventually used to populate the C<subevents> attribute on the 1245L<Test2::Event::Subtest> event that is generated at the end of the subtest. 1246This flag has no effect on this part, it always happens. 1247 1248At the end of the subtest, the final L<Test2::Event::Subtest> event is sent to 1249the formatter. 1250 1251=item Things that are effected by this flag 1252 1253The C<buffered> attribute of the L<Test2::Event::Subtest> event will be set to 1254the value of this flag. This means any formatter, listener, etc which looks at 1255the event will know if it was buffered. 1256 1257=item Things that are formatter dependant 1258 1259Events within a buffered subtest may or may not be sent to the formatter as 1260they happen. If a formatter fails to specify then the default is to B<NOT SEND> 1261the events as they are generated, instead the formatter can pull them from the 1262C<subevents> attribute. 1263 1264A formatter can specify by implementing the C<hide_buffered()> method. If this 1265method returns true then events generated inside a buffered subtest will not be 1266sent independently of the final subtest event. 1267 1268=back 1269 1270An example of how this is used is the L<Test2::Formatter::TAP> formatter. For 1271unbuffered subtests the events are rendered as they are generated. At the end 1272of the subtest, the final subtest event is rendered, but the C<subevents> 1273attribute is ignored. For buffered subtests the opposite occurs, the events are 1274NOT rendered as they are generated, instead the C<subevents> attribute is used 1275to render them all at once. This is useful when running subtests tests in 1276parallel, since without it the output from subtests would be interleaved 1277together. 1278 1279=head1 OTHER API EXPORTS 1280 1281Exports in this section are not commonly needed. These all have the 'test2_' 1282prefix to help ensure they stand out. You should look at the L</MAIN API 1283EXPORTS> section before looking here. This section is one where "Great power 1284comes with great responsibility". It is possible to break things badly if you 1285are not careful with these. 1286 1287All exports are optional. You need to list which ones you want at import time: 1288 1289 use Test2::API qw/test2_init_done .../; 1290 1291=head2 STATUS AND INITIALIZATION STATE 1292 1293These provide access to internal state and object instances. 1294 1295=over 4 1296 1297=item $bool = test2_init_done() 1298 1299This will return true if the stack and IPC instances have already been 1300initialized. It will return false if they have not. Init happens as late as 1301possible. It happens as soon as a tool requests the IPC instance, the 1302formatter, or the stack. 1303 1304=item $bool = test2_load_done() 1305 1306This will simply return the boolean value of the loaded flag. If Test2 has 1307finished loading this will be true, otherwise false. Loading is considered 1308complete the first time a tool requests a context. 1309 1310=item test2_set_is_end() 1311 1312=item test2_set_is_end($bool) 1313 1314This is used to toggle Test2's belief that the END phase has already started. 1315With no arguments this will set it to true. With arguments it will set it to 1316the first argument's value. 1317 1318This is used to prevent the use of C<caller()> in END blocks which can cause 1319segfaults. This is only necessary in some persistent environments that may have 1320multiple END phases. 1321 1322=item $bool = test2_get_is_end() 1323 1324Check if Test2 believes it is the END phase. 1325 1326=item $stack = test2_stack() 1327 1328This will return the global L<Test2::API::Stack> instance. If this has not 1329yet been initialized it will be initialized now. 1330 1331=item $bool = test2_is_testing_done() 1332 1333This will return true if testing is complete and no other events should be 1334sent. This is useful in things like warning handlers where you might want to 1335turn warnings into events, but need them to start acting like normal warnings 1336when testing is done. 1337 1338 $SIG{__WARN__} = sub { 1339 my ($warning) = @_; 1340 1341 if (test2_is_testing_done()) { 1342 warn @_; 1343 } 1344 else { 1345 my $ctx = context(); 1346 ... 1347 $ctx->release 1348 } 1349 } 1350 1351=item test2_ipc_disable 1352 1353Disable IPC. 1354 1355=item $bool = test2_ipc_diabled 1356 1357Check if IPC is disabled. 1358 1359=item test2_ipc_wait_enable() 1360 1361=item test2_ipc_wait_disable() 1362 1363=item $bool = test2_ipc_wait_enabled() 1364 1365These can be used to turn IPC waiting on and off, or check the current value of 1366the flag. 1367 1368Waiting is turned on by default. Waiting will cause the parent process/thread 1369to wait until all child processes and threads are finished before exiting. You 1370will almost never want to turn this off. 1371 1372=item $bool = test2_no_wait() 1373 1374=item test2_no_wait($bool) 1375 1376B<DISCOURAGED>: This is a confusing interface, it is better to use 1377C<test2_ipc_wait_enable()>, C<test2_ipc_wait_disable()> and 1378C<test2_ipc_wait_enabled()>. 1379 1380This can be used to get/set the no_wait status. Waiting is turned on by 1381default. Waiting will cause the parent process/thread to wait until all child 1382processes and threads are finished before exiting. You will almost never want 1383to turn this off. 1384 1385=item $fh = test2_stdout() 1386 1387=item $fh = test2_stderr() 1388 1389These functions return the filehandles that test output should be written to. 1390They are primarily useful when writing a custom formatter and code that turns 1391events into actual output (TAP, etc.). They will return a dupe of the original 1392filehandles that formatted output can be sent to regardless of whatever state 1393the currently running test may have left STDOUT and STDERR in. 1394 1395=item test2_reset_io() 1396 1397Re-dupe the internal filehandles returned by C<test2_stdout()> and 1398C<test2_stderr()> from the current STDOUT and STDERR. You shouldn't need to do 1399this except in very peculiar situations (for example, you're testing a new 1400formatter and you need control over where the formatter is sending its output.) 1401 1402=back 1403 1404=head2 BEHAVIOR HOOKS 1405 1406These are hooks that allow you to add custom behavior to actions taken by Test2 1407and tools built on top of it. 1408 1409=over 4 1410 1411=item test2_add_callback_exit(sub { ... }) 1412 1413This can be used to add a callback that is called after all testing is done. This 1414is too late to add additional results, the main use of this callback is to set the 1415exit code. 1416 1417 test2_add_callback_exit( 1418 sub { 1419 my ($context, $exit, \$new_exit) = @_; 1420 ... 1421 } 1422 ); 1423 1424The C<$context> passed in will be an instance of L<Test2::API::Context>. The 1425C<$exit> argument will be the original exit code before anything modified it. 1426C<$$new_exit> is a reference to the new exit code. You may modify this to 1427change the exit code. Please note that C<$$new_exit> may already be different 1428from C<$exit> 1429 1430=item test2_add_callback_post_load(sub { ... }) 1431 1432Add a callback that will be called when Test2 is finished loading. This 1433means the callback will be run once, the first time a context is obtained. 1434If Test2 has already finished loading then the callback will be run immediately. 1435 1436=item test2_add_callback_testing_done(sub { ... }) 1437 1438This adds your coderef as a follow-up to the root hub after Test2 is finished loading. 1439 1440This is essentially a helper to do the following: 1441 1442 test2_add_callback_post_load(sub { 1443 my $stack = test2_stack(); 1444 $stack->top; # Insure we have a hub 1445 my ($hub) = Test2::API::test2_stack->all; 1446 1447 $hub->set_active(1); 1448 1449 $hub->follow_up(sub { ... }); # <-- Your coderef here 1450 }); 1451 1452=item test2_add_callback_context_acquire(sub { ... }) 1453 1454Add a callback that will be called every time someone tries to acquire a 1455context. This will be called on EVERY call to C<context()>. It gets a single 1456argument, a reference to the hash of parameters being used the construct the 1457context. This is your chance to change the parameters by directly altering the 1458hash. 1459 1460 test2_add_callback_context_acquire(sub { 1461 my $params = shift; 1462 $params->{level}++; 1463 }); 1464 1465This is a very scary API function. Please do not use this unless you need to. 1466This is here for L<Test::Builder> and backwards compatibility. This has you 1467directly manipulate the hash instead of returning a new one for performance 1468reasons. 1469 1470=item test2_add_callback_context_init(sub { ... }) 1471 1472Add a callback that will be called every time a new context is created. The 1473callback will receive the newly created context as its only argument. 1474 1475=item test2_add_callback_context_release(sub { ... }) 1476 1477Add a callback that will be called every time a context is released. The 1478callback will receive the released context as its only argument. 1479 1480=item test2_add_callback_pre_subtest(sub { ... }) 1481 1482Add a callback that will be called every time a subtest is going to be 1483run. The callback will receive the subtest name, coderef, and any 1484arguments. 1485 1486=item @list = test2_list_context_acquire_callbacks() 1487 1488Return all the context acquire callback references. 1489 1490=item @list = test2_list_context_init_callbacks() 1491 1492Returns all the context init callback references. 1493 1494=item @list = test2_list_context_release_callbacks() 1495 1496Returns all the context release callback references. 1497 1498=item @list = test2_list_exit_callbacks() 1499 1500Returns all the exit callback references. 1501 1502=item @list = test2_list_post_load_callbacks() 1503 1504Returns all the post load callback references. 1505 1506=item @list = test2_list_pre_subtest_callbacks() 1507 1508Returns all the pre-subtest callback references. 1509 1510=item test2_add_uuid_via(sub { ... }) 1511 1512=item $sub = test2_add_uuid_via() 1513 1514This allows you to provide a UUID generator. If provided UUIDs will be attached 1515to all events, hubs, and contexts. This is useful for storing, tracking, and 1516linking these objects. 1517 1518The sub you provide should always return a unique identifier. Most things will 1519expect a proper UUID string, however nothing in Test2::API enforces this. 1520 1521The sub will receive exactly 1 argument, the type of thing being tagged 1522'context', 'hub', or 'event'. In the future additional things may be tagged, in 1523which case new strings will be passed in. These are purely informative, you can 1524(and usually should) ignore them. 1525 1526=back 1527 1528=head2 IPC AND CONCURRENCY 1529 1530These let you access, or specify, the IPC system internals. 1531 1532=over 4 1533 1534=item $bool = test2_has_ipc() 1535 1536Check if IPC is enabled. 1537 1538=item $ipc = test2_ipc() 1539 1540This will return the global L<Test2::IPC::Driver> instance. If this has not yet 1541been initialized it will be initialized now. 1542 1543=item test2_ipc_add_driver($DRIVER) 1544 1545Add an IPC driver to the list. This will add the driver to the start of the 1546list. 1547 1548=item @drivers = test2_ipc_drivers() 1549 1550Get the list of IPC drivers. 1551 1552=item $bool = test2_ipc_polling() 1553 1554Check if polling is enabled. 1555 1556=item test2_ipc_enable_polling() 1557 1558Turn on polling. This will cull events from other processes and threads every 1559time a context is created. 1560 1561=item test2_ipc_disable_polling() 1562 1563Turn off IPC polling. 1564 1565=item test2_ipc_enable_shm() 1566 1567Legacy, this is currently a no-op that returns 0; 1568 1569=item test2_ipc_set_pending($uniq_val) 1570 1571Tell other processes and events that an event is pending. C<$uniq_val> should 1572be a unique value no other thread/process will generate. 1573 1574B<Note:> After calling this C<test2_ipc_get_pending()> will return 1. This is 1575intentional, and not avoidable. 1576 1577=item $pending = test2_ipc_get_pending() 1578 1579This returns -1 if there is no way to check (assume yes) 1580 1581This returns 0 if there are (most likely) no pending events. 1582 1583This returns 1 if there are (likely) pending events. Upon return it will reset, 1584nothing else will be able to see that there were pending events. 1585 1586=item $timeout = test2_ipc_get_timeout() 1587 1588=item test2_ipc_set_timeout($timeout) 1589 1590Get/Set the timeout value for the IPC system. This timeout is how long the IPC 1591system will wait for child processes and threads to finish before aborting. 1592 1593The default value is C<30> seconds. 1594 1595=back 1596 1597=head2 MANAGING FORMATTERS 1598 1599These let you access, or specify, the formatters that can/should be used. 1600 1601=over 4 1602 1603=item $formatter = test2_formatter 1604 1605This will return the global formatter class. This is not an instance. By 1606default the formatter is set to L<Test2::Formatter::TAP>. 1607 1608You can override this default using the C<T2_FORMATTER> environment variable. 1609 1610Normally 'Test2::Formatter::' is prefixed to the value in the 1611environment variable: 1612 1613 $ T2_FORMATTER='TAP' perl test.t # Use the Test2::Formatter::TAP formatter 1614 $ T2_FORMATTER='Foo' perl test.t # Use the Test2::Formatter::Foo formatter 1615 1616If you want to specify a full module name you use the '+' prefix: 1617 1618 $ T2_FORMATTER='+Foo::Bar' perl test.t # Use the Foo::Bar formatter 1619 1620=item test2_formatter_set($class_or_instance) 1621 1622Set the global formatter class. This can only be set once. B<Note:> This will 1623override anything specified in the 'T2_FORMATTER' environment variable. 1624 1625=item @formatters = test2_formatters() 1626 1627Get a list of all loaded formatters. 1628 1629=item test2_formatter_add($class_or_instance) 1630 1631Add a formatter to the list. Last formatter added is used at initialization. If 1632this is called after initialization a warning will be issued. 1633 1634=back 1635 1636=head1 OTHER EXAMPLES 1637 1638See the C</Examples/> directory included in this distribution. 1639 1640=head1 SEE ALSO 1641 1642L<Test2::API::Context> - Detailed documentation of the context object. 1643 1644L<Test2::IPC> - The IPC system used for threading/fork support. 1645 1646L<Test2::Formatter> - Formatters such as TAP live here. 1647 1648L<Test2::Event> - Events live in this namespace. 1649 1650L<Test2::Hub> - All events eventually funnel through a hub. Custom hubs are how 1651C<intercept()> and C<run_subtest()> are implemented. 1652 1653=head1 MAGIC 1654 1655This package has an END block. This END block is responsible for setting the 1656exit code based on the test results. This end block also calls the callbacks that 1657can be added to this package. 1658 1659=head1 SOURCE 1660 1661The source code repository for Test2 can be found at 1662F<http://github.com/Test-More/test-more/>. 1663 1664=head1 MAINTAINERS 1665 1666=over 4 1667 1668=item Chad Granum E<lt>exodist@cpan.orgE<gt> 1669 1670=back 1671 1672=head1 AUTHORS 1673 1674=over 4 1675 1676=item Chad Granum E<lt>exodist@cpan.orgE<gt> 1677 1678=back 1679 1680=head1 COPYRIGHT 1681 1682Copyright 2019 Chad Granum E<lt>exodist@cpan.orgE<gt>. 1683 1684This program is free software; you can redistribute it and/or 1685modify it under the same terms as Perl itself. 1686 1687See F<http://dev.perl.org/licenses/> 1688 1689=cut 1690