TEST2::ASYNCSUBTEST(3pm)
β¨ NAME β¨
Test2::AsyncSubtest β Object representing an async subtest. Youβre reading the Test2::AsyncSubtest(3pm) page from the Perl Programmers Reference Guide, and honestly? Weβre about to make async subtests make sense, bestie. π
π¦ DESCRIPTION π¦
Okay so regular subtests have a limited scope: they start, events get generated, then they close and send an 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. Periodt. π
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. Slay. β¨
π 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. No name, no vibe check passing, bestie. π
πΈ send_to => $hub (optional)
Hub to which the final subtest event should be sent. This must be an instance of Test2::Hub or 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 of Test2::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 of Test2::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 by attach().
This must only be called in the original process/thread.
π $ctx = $ast->context
Get an Test2::API::Context instance 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->release on 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 the subtest no final plan will be added. 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 anTest2::Event::Okevent.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 like fork(). It will return a pid in the parent, and an Test2::Util::Guard instance 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/. Go be nosy, bestie. π
π 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/
And thatβs the cute little sign-off, bestie: this page is from perl v5.42.3, dated 2026-11-03, with the footer title Test2::...cSubtest(3pm) β aka the Test2::AsyncSubtest(3pm) manual page. Bye π
β¨π
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl 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