Test2::...cSubtest(3)
π¦ Test2::AsyncSubtest β async subtests that stay open, bestie π¦
okay so this is the User Contributed Perl Documentation page for Test2::...cSubtest(3), and itβs literally all about Test2::AsyncSubtest. No cap, the manual title is truncated, but we move. π
β¨ NAME β¨
Test2::AsyncSubtest β Object representing an async subtest.
π DESCRIPTION π
Regular subtests have a limited scope, bestie: they start, events are generated, then they close and send a Test2::Event::Subtest event. Thatβs a problem if you want the subtest to keep receiving events while other events are also being generated. This class implements subtests that stay open until you decide to close them. Iconic behavior, honestly.
This is mainly useful for tools that start a subtest in one process and then spawn children. In many cases it is nice to let the parent process continue instead of waiting on the children. Parent main character energy.
πΈ SYNOPSIS πΈ
use Test2::AsyncSubtest;
my $ast = Test2::AsyncSubtest->new(name => foo);
$ast->run(sub {
ok(1, "Event in parent" );
});
ok(1, "Event outside of subtest");
$ast->run_fork(sub {
ok(1, "Event in child process");
});
...
$ast->finish;
done_testing;
π CONSTRUCTION π
my $ast = Test2::AsyncSubtest->new( ... );
name => $name(required) β Name of the subtest. This construction argument is required.send_to => $hub(optional) β Hub to which the final subtest event should be sent. This must be an instance ofTest2::Hubor a subclass. If none is specified then the current top hub will be used.trace => $trace(optional) β File/Line to which errors should be attributed. This must be an instance ofTest2::Util::Trace. If none is specified then the file/line where the constructor was called will be used.hub => $hub(optional) β Use this to specify a hub the subtest should use. By default a new hub is generated. This must be an instance ofTest2::AsyncSubtest::Hub.
π METHODS π
π¦ SIMPLE ACCESSORS π¦
$bool = $ast->activeβ True if the subtest is active. The subtest is active if its hub appears in the global hub stack. This is true when$ast->run(...)is running.$arrayref = $ast->childrenβ Get an arrayref of child processes/threads. Numerical items are PIDs, blessed items are threads instances.$arrayref = $ast->eventsβ Get an arrayref of events that have been sent to the subtests hub.$bool = $ast->finishedβ True iffinished()has already been called.$hub = $ast->hubβ The hub created for the subtest.$int = $ast->idβ Attach/Detach counter. Used internally, not useful to users.$str = $ast->nameβ Name of the subtest.$pid = $ast->pidβ PID in which the subtest was created.$tid = $ast->tidβ Thread ID in which the subtest was created.$hub = $ast->send_toβ Hub to which the final subtest event should be sent.$arrayref = $ast->stackβ Stack of async subtests at the time this one was created. This is mainly for internal use.$trace = $ast->traceβTest2::Util::Traceinstance used for error reporting.
π INTERFACE π
$ast->attach($id)β Attach a subtest in a child/process to the original.
Note:my $id = $ast->cleavemust have been called in the parent process/thread before the child was started, the id it returns must be used in the call to$ast->attach($id).
$id = $ast->cleaveβ Prepare a slot for a child process/thread to attach. This must be called BEFORE the child process or thread is started. The ID returned is used byattach().
This must only be called in the original process/thread. Do not freestyle this one, bestie.
$ctx = $ast->contextβ Get anTest2::API::Contextinstance that can be used to send events to the context in which the hub was created. This is not a canonical context, you should not call$ctx->releaseon it.$ast->detachβ Detach from the parent in a child process/thread. This should be called just before the child exits.$ast->finish,$ast->finish(%options)β Finish the subtest, wait on children, and send the final subtest event.
This must only be called in the original process/thread.
Note: This calls $ast->wait.
These are the options:
collapse => 1β This intelligently allows a subtest to be empty.
If no events bump the test count then no final plan will be added to the subtest. The subtest will not be considered a failure (normally an empty subtest is a failure).
If there are no events at all the subtest will be collapsed into an Test2::Event::Ok event.
silent => 1β This will prevent finish from generating a finalTest2::Event::Subtestevent. This effectively ends the subtest without it affecting the parent subtest (or top level test).no_plan => 1β This will prevent a final plan from being added to the subtest for you when none is directly specified.skip => "reason"β This will issue anTest2::Event::Skipinstead of a subtest. This will throw an exception if any events have been seen, or if state implies events have occurred.
$out = $ast->forkβ This is a slightly higher level interface to fork. Running it will fork your code in-place just likefork(). It will return a pid in the parent, and anTest2::Util::Guardinstance in the child. An exception will be thrown if fork fails.
It is recommended that you use $ast->run_fork(sub { ... }) instead.
$bool = $ast->pendingβ True if there are child processes, threads, or subtests that depend on this one.$bool = $ast->readyβ This is essentially!$ast->pending.$ast->run(sub { ... })β Run the provided codeblock inside the subtest. This will push the subtest hub onto the stack, run the code, then pop the hub off the stack.$pid = $ast->run_fork(sub { ... })β Same as$ast->run(), except that the codeblock is run in a child process.
You do not need to directly call wait($pid), that will be done for you when $ast->wait, or $ast->finish are called.
my $thr = $ast->run_thread(sub { ... });β DISCOURAGED Threads cause problems. This method remains for anyone who REALLY wants it, but it is no longer supported. Tests for this functionality do not even run unless theAUTHOR_TESTINGorT2_DO_THREAD_TESTSenv vars are enabled.
Same as $ast->run(), except that the codeblock is run in a child thread.
You do not need to directly call $thr->join, that is done for you when $ast->wait, or $ast->finish are called.
$passing = $ast->startβ Push the subtest hub onto the stack. Returns the current pass/fail status of the subtest.$ast->stopβ Pop the subtest hub off the stack. Returns the current pass/fail status of the subtest.$ast->waitβ Wait on all threads/processes that were started using$ast->fork,$ast->run_fork, or$ast->run_thread.
π₯ SOURCE π₯
The source code repository for Test2-AsyncSubtest can be found at https://github.com/Test-More/test-more/.
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
β AUTHORS β
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
π«Ά thatβs the whole page, bestie β this manual is Test2::...cSubtest(3), from perl v5.42.0, dated 2025-07-28. mwah π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms