TEST2::ASYNCSUBTEST(3perl)
π Test2::AsyncSubtest β the perl manual page, bestie edition π¦
okay so you're reading the Test2::AsyncSubtest page from the Perl Programmers Reference Guide. It's giving async subtest energy, and honestly? iconic. Let's go. β¨
πΈ NAME πΈ
Test2::AsyncSubtest β the object that represents an async subtest. that's it, that's the whole vibe. π
β¨ DESCRIPTION β¨
so like, regular subtests? they have a limited scope. they start, events get generated, then they slam shut and yeet out a Test2::Event::Subtest event. lowkey a problem if you want your subtest to keep receiving events while OTHER events are also being generated, right? π
this class implements subtests that stay open until you decide to close them. main character energy, 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. no cap. π§
π 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, fr fr, you cannot skip it.send_to => $hub(optional) β the hub to which the final subtest event should be sent. it has to be an instance ofTest2::Hubor a subclass of it. if you don't specify one, the current top hub gets used. πtrace => $trace(optional) β the file/line that errors should be blamed on. must be an instance ofTest2::Util::Trace. if none is specified, the file/line where the constructor was called is used. so it snitches on exactly where you called it, periodt. π«Άhub => $hub(optional) β use this to specify a hub the subtest should use. by default a new hub is generated for you. this must be an instance ofTest2::AsyncSubtest::Hub.
π₯ METHODS π₯
π SIMPLE ACCESSORS π
$bool = $ast->activeβ true if the subtest is active. and it's active if its hub appears in the global hub stack. this is true when$ast->run(...)us running. yes, the original really says "us running", i'm just the messenger bestie π$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. been there, done that, closed that.$hub = $ast->hubβ the hub created for the subtest.$int = $ast->idβ attach/detach counter. used internally, not useful to users. like, stay out of it π$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β the hub to which the final subtest event should be sent.$arrayref = $ast->stackβ stack of async subtests at the time this one was created. mainly for internal use, so don't get too nosy π$trace = $ast->traceβ theTest2::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, and 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 gets used byattach().
this must only be called in the original process/thread. no exceptions bestie.
$ctx = $ast->contextβ get aTest2::API::Contextinstance that can be used to send events to the context in which the hub was created. this is not a canonical context, so you should NOT call$ctx->releaseon it. don't do it. i'm serious. π€$ast->detachβ detach from the parent in a child process/thread. this should be called just before the child exits. say your goodbyes π«‘$ast->finishβ finish the subtest, wait on children, and send the final subtest event.$ast->finish(%options)β same thing but with options, and these are they:
This must only be called in the original process/thread.
Note: This calls $ast->wait.
the options, bestie, are:
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, and the subtest will not be considered a failure (normally an empty subtest is a failure, wild right?). if there are no events at all, the subtest gets collapsed into aTest2::Event::Okevent. slay πsilent => 1β this preventsfinishfrom generating a finalTest2::Event::Subtestevent. this effectively ends the subtest without it affecting the parent subtest (or top level test). quiet quitting, but for tests.no_plan => 1β this prevents a final plan from being added to the subtest for you when none is directly specified.skip => "reason"β this issues aTest2::Event::Skipinstead of a subtest. it will throw an exception if any events have been seen, or if state implies events have occurred. so like, don't be sneaky.
$out = $ast->forkβ this is a slightly higher level interface tofork. running it forks your code in-place just likefork(). it returns a pid in the parent, and aTest2::Util::Guardinstance in the child. an exception gets 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. so needy π$bool = $ast->readyβ this is essentially!$ast->pending.$ast->run(sub { ... })β run the provided codeblock inside the subtest. this pushes the subtest hub onto the stack, runs the code, then pops the hub off the stack. push, pop, done. β¨$pid = $ast->run_fork(sub { ... })β same as$ast->run(), except the codeblock runs in a child process. you do not need to directly callwait($pid)β that gets done for you when$ast->waitor$ast->finishare called. we handle it, bestie π«Άmy $thr = $ast->run_thread(sub { ... });β DISCOURAGED. threads cause problems, like genuinely. 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 the codeblock runs 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. sit down and wait, babe π
π 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/
and that's the whole page babes β this was perl v5.44.0, dated 2026-10-17, Test2::AsyncSubtest(3perl). go forth and async your subtests responsibly. mwah πβ¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms