Test2::AsyncSubtest(3p) | Perl Programmers Reference Guide | Test2::AsyncSubtest(3p)

# NAME

Test2::AsyncSubtest - Object representing an async subtest.

# DESCRIPTION

Regular subtests have a limited scope, they start, events are generated, then they close and send an Test2::Event::Subtest event. This is 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.

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.

# 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 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(...)" us 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 if finished() 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::Trace instance used for error reporting.

## INTERFACE

$ast->attach($id)
  Attach a subtest in a child/process to the original.

  Note: "my $id = $ast->cleave" must 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 an Test2::Event::Ok event.

silent => 1
  This will prevent finish from generating a final Test2::Event::Subtest event. This effectively ends the subtest without it effecting 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 an Test2::Event::Skip instead 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 the AUTHOR_TESTING or T2_DO_THREAD_TESTS env 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/Test2-Suite/.

# MAINTAINERS

Chad Granum <exodist@cpan.org>

# AUTHORS

Chad Granum <exodist@cpan.org>

# COPYRIGHT

Copyright 2018 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/

2025-01-28 | perl v5.40.1