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 recieving events while other events are also being generated. This class implements subtests that stay pen until you decide to close them.
This is mainly useful for tools that start a subtest in one process or thread and then spawn children. In many cases it is nice to let the parent process continue instead of waiting on the children.
SYNOPSYS
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->run_thread(sub {
ok(1, "Event in child thread");
});
...
$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 cononical 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
-
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
. - $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 Scope::Guard instance in the child. An exception will be thrown if fork fails.It is recomended 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 { ... });
-
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 http://github.com/Test-More/Test2-AsyncSubtest/.
MAINTAINERS
AUTHORS
COPYRIGHT
Copyright 2015 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/