The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Stream::Hub - The conduit through which all events flow.

EXPERIMENTAL CODE WARNING

This is an experimental release! Test-Stream, and all its components are still in an experimental phase. This dist has been released to cpan in order to allow testers and early adopters the chance to write experimental new tools with it, or to add experimental support for it into old tools.

PLEASE DO NOT COMPLETELY CONVERT OLD TOOLS YET. This experimental release is very likely to see a lot of code churn. API's may break at any time. Test-Stream should NOT be depended on by any toolchain level tools until the experimental phase is over.

SYNOPSIS

    use Test::Stream::Hub;

    my $hub = Test::Stream::Hub->new();
    $hub->send(...);

DESCRIPTION

The hub is the place where all events get processed and handed off to the formatter. The hub also tracks test state, and provides everal hooks into the event pipeline.

COMMON TASKS

SENDING EVENTS

    $hub->send($event)

The send() method is used to issue an event to the hub. This method will handle thread/fork sync, mungers, listeners, TAP output, etc.

ALTERING EVENTS

    $hub->munge(sub {
        my ($hub, $event) = @_;

        ... Modify the event object ...

        # return is ignored.
    });

By default mungers are not inherited by child hubs, that means if you start a subtest, the subtest will not inherit the munger. You can change this behavior with the inherit parameter:

    $hub->munge(sub { ... }, inherit => 1);

DESTROYING OR REPLACING AN EVENT

@_ elements are aliased to the arguments passed into the munger from the caller. Because of this you can destroy the event so that nothing ever sees it. This will be supported so long as perl supports this behavior. There is a check in send() to return if a munger destroys the event.

    $hub->munge(sub {
        my ($hub, $event) = @_;
        return unless ...;

        $_[1] = undef;
    });

LISTENING FOR EVENTS

    $hub->listen(sub {
        my ($hub, $event, $number) = @_;

        ... do whatever you want with the event ...

        # return is ignored
    });

By default listeners are not inherited by child hubs, that means if you start a subtest, the subtest will not inherit the listener. You can change this behavior with the inherit parameter:

    $hub->listen(sub { ... }, inherit => 1);

POST-TEST BEHAVIORS

    $hub->follow_up(sub {
        my ($dbg, $hub) = @_;

        ... do whatever you need to ...

        # Return is ignored
    });

follow_up subs are called only once, ether when done_testing is called, or in an END block.

SETTING THE FORMATTER

By default an instance of Test::Stream::Formatter::TAP is created and used.

    my $old = $hub->format(My::Formatter->new);

Setting the formatter will REPLACE any existing formatter. You may set the formatter to undef to prevent output. The old formatter will be returned if one was already set. Only 1 formatter is allowed at a time.

METHODS

$hub->send($event)

This is where all events enter the hub for processing.

$hub->process($event)

This is called by send after it does any IPC handling. You can use this to bypass the IPC process, but in general you should avoid using this.

$val = $hub->meta($key)
$val = $hub->meta($key, $default)

This method is made available to allow third party plugins to associate meta-data with a hub. It is recommended that all third party plugins use their module namespace as their meta-data key.

This method always returns the value for the key. If there is no value it will be initialized to $default, in which case $default is also returned.

Recommended usage:

    my $meta = $hub->meta(__PACKAGE__, {});
    unless ($meta->{foo}) {
        $meta->{foo} = 1;
        $meta->{bar} = 2;
    }
$val = $hub->delete_meta($key)

This will delete all data in the specified metadata key.

$val = $hub->get_meta($key)

This method will retrieve the value of any meta-data key specified.

$string = $hub->get_todo()

Get the current TODO reason. This will be undef if there is no active todo. Please note that 0 and '' (empty string) count as active todo.

$ref = $hub->set_todo($reason)

This will set the todo message. The todo will remain in effect until you let go of the reference returned by this method.

    {
        my $todo = $hub->set_todo("Broken");

        # These ok events will be TODO
        ok($foo->doit, "do it!");
        ok($foo->doit, "do it again!");

        # The todo setting goes away at the end of this scope.
    }

    # This result will not be TODO.
    ok(1, "pass");

You can also do it without the indentation:

    my $todo = $hub->set_todo("Broken");

    # These ok events will be TODO
    ok($foo->doit, "do it!");
    ok($foo->doit, "do it again!");

    # Unset the todo
    $todo = undef;

    # This result will not be TODO.
    ok(1, "pass");

This method can be called while TODO is already in effect and it will work in a sane way:

    {
        my $first_todo = $hub->set_todo("Will fix soon");

        ok(0, "Not fixed"); # TODO: Will fix soon

        {
            my $second_todo = $hub->set_todo("Will fix eventually");
            ok(0, "Not fixed"); # TODO: Will fix eventually
        }

        ok(0, "Not fixed"); # TODO: Will fix soon
    }

This also works if you free todo's out of order. The most recently set todo that is still active will always be used as the todo.

$old = $hub->format($formatter)

Replace the existing formatter instance with a new one. Formatters must be objects that implement a $formatter->write($event) method.

$sub = $hub->munge(sub { ... })
$sub = $hub->munge(sub { ... }, inherit => 1)

This adds your codeblock as a callback. Every event that hits this hub will be given to your munger BEFORE it is sent to the formatter. You can make any modifications you want to the event object.

    $hub->munge(sub {
        my ($hub, $event) = @_;

        ... Modify the event object ...

        # return is ignored.
    });

You can also completely remove the event from the stream:

    $hub->munge(sub {
        my ($hub, $event) = @_;
        return unless ...;

        $_[1] = undef;
    });

Normally mungers are not inherited by child hubs such as subtests. You can add the inherit => 1 parameter to allow a munger to be inherited.

$hub->unmunge($sub)

You can use this to remove a munge callback. You must pass in the coderef returned by the munge() method.

$sub = $hub->listen(sub { ... })

You can use this to record all events AFTER they have been sent to the formatter. No changes made here will be meaningful, except possibly to other listeners.

    $hub->listen(sub {
        my ($hub, $event, $number) = @_;

        ... do whatever you want with the event ...

        # return is ignored
    });

Normally listeners are not inherited by child hubs such as subtests. You can add the inherit => 1 parameter to allow a listener to be inherited.

$hub->unlisten($sub)

You can use this to remove a listen callback. You must pass in the coderef returned by the listen() method.

$hub->follow_op(sub { ... })

Use this to add behaviors that are called just before the Test::Stream::State for the hub is finalized. The only argument to your codeblock will be a Test::Stream::DebugInfo instance.

    $hub->follow_up(sub {
        my ($dbg, $hub) = @_;

        ... do whatever you need to ...

        # Return is ignored
    });

follow_up subs are called only once, ether when done_testing is called, or in an END block.

$sub = $hub->add_context_init(sub { ... });

This allows you to add callbacks that will trigger every time a new context is created for the hub. The only argument to the sub will be the Test::Stream::Context instance that was created.

Note Using this hook could have a huge performance impact.

The coderef you provide is returned and can be used to remove the hook later.

$hub->remove_context_init($sub);

This can be used to remove a context init hook.

$sub = $hub->add_context_release(sub { ... });

This allows you to add callbacks that will trigger every time a context for this hub is released. The only argument to the sub will be the Test::Stream::Context instance that was released. These will run in reverse order.

Note Using this hook could have a huge performance impact.

The coderef you provide is returned and can be used to remove the hook later.

$hub->remove_context_release($sub);

This can be used to remove a context release hook.

$hub->cull()

Cull any IPC events (and process them).

$pid = $hub->pid()

Get the process id under which the hub was created.

$tid = $hub->tid()

Get the thread id under which the hub was created.

$hud = $hub->hid()

Get the identifier string of the hub.

$ipc = $hub->ipc()

Get the IPC object used by the hub.

$hub->set_no_ending($bool)
$bool = $hub->no_ending

This can be used to disable auto-ending behavior for a hub. The auto-ending behavior is triggered by an end block and is used to cull IPC events, and output the final plan if the plan was 'no_plan'.

$bool = $hub->parent_todo

This will be true if this hub is a child hub who's parent had todo set.

SOURCE

The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>

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://www.perl.com/perl/misc/Artistic.html