Test-Stream

 view release on metacpan or  search on metacpan

t/modules/Sync.t  view on Meta::CPAN

    eval { $sync->set_formatter() };
    def like => ($@, qr/No formatter specified/, "must specifty formatter");

    $reset->();
    {
        local $ENV{TS_FORMATTER} = 'Fake';
        eval { $sync->formatter };
        def like => (
            $@,
            qr/COULD NOT LOAD FORMATTER 'Fake' \(set by the 'TS_FORMATTER' environment variable\)/,
            "Bad formatter"
        );
    }

    $reset->();
    {
        local $ENV{TS_FORMATTER} = '+Foo';
        local $INC{'Foo.pm'} = __FILE__;
        def is => ($sync->formatter, 'Foo', "Used env var formatter (full)");
    }

    $reset->();
    {
        local $ENV{TS_FORMATTER} = 'Foo';
        local $INC{'Test/Stream/Formatter/Foo.pm'} = __FILE__;
        def is => ($sync->formatter, 'Test::Stream::Formatter::Foo', "Used env var formatter (short)");
    }

    $reset->();
    def is => ($sync->no_wait, 0, "no_wait is off");
    $sync->no_wait(1);
    def is => ($sync->no_wait, 1, "no_wait is on");
    $sync->no_wait(0);
    def is => ($sync->no_wait, 0, "no_wait is off again");
    $sync->_init;
    def is => ($sync->no_wait, 0, "no_wait is off");
    $sync->no_wait(1);
    def is => ($sync->no_wait, 1, "no_wait is on");
    $sync->no_wait(0);
    def is => ($sync->no_wait, 0, "no_wait is off again");

    if (CAN_REALLY_FORK) {
        $reset->();
        my $pid = fork;
        die "Failed to fork!" unless defined $pid;
        unless($pid) { exit 0 }

        def is => ($sync->_ipc_wait, 0, "No errors");

        $pid = fork;
        die "Failed to fork!" unless defined $pid;
        unless($pid) { exit 255 }
        my @warnings;
        {
            local $SIG{__WARN__} = sub { push @warnings => @_ };
            def is => ($sync->_ipc_wait, 255, "Process exited badly");
        }
        def like => (\@warnings, [qr/Process .* did not exit cleanly \(status: 255\)/], "Warn about exit");
    }

    if (CAN_THREAD && $] ge '5.010') {
        require threads;
        $reset->();

        threads->new(sub { 1 });
        def is => ($sync->_ipc_wait, 0, "No errors");

        if (threads->can('error')) {
            threads->new(sub {
                close(STDERR);
                close(STDOUT);
                die "xxx"
            });
            my @warnings;
            {
                local $SIG{__WARN__} = sub { push @warnings => @_ };
                def is => ($sync->_ipc_wait, 255, "Process exited badly");
            }
            def like => (\@warnings, [qr/Thread .* did not end cleanly: xxx/], "Warn about exit");
        }
    }

    $reset->();
    local $? = 0;
    $sync->_set_exit;
    def is => ($?, 0, "no errors on exit");

    $reset->();
    def is => (Test::Stream::Sync->loaded, 0, "not loaded");
    def is => (Test::Stream::Sync->loaded, 0, "not modified by checking");

    my ($ranA, $ranB) = (0, 0);
    Test::Stream::Sync->post_load(sub { $ranA++ });
    Test::Stream::Sync->post_load(sub { $ranB++ });
    def is => (Test::Stream::Sync->loaded, 0, "not loaded");
    def is => ($ranA, 0, "Did not run");
    def is => ($ranB, 0, "Did not run");
    def is => (Test::Stream::Sync->post_loads, 2, "2 loads");

    Test::Stream::Sync->loaded(0); # False value
    def is => (Test::Stream::Sync->loaded, 0, "not loaded");
    def is => ($ranA, 0, "Did not run");
    def is => ($ranB, 0, "Did not run");

    Test::Stream::Sync->loaded('true');
    def is => ($ranA, 1, "ran once");
    def is => ($ranB, 1, "ran once");
    def is => (Test::Stream::Sync->loaded, 1, "loaded");

    Test::Stream::Sync->loaded(1);
    def is => ($ranA, 1, "Only ran once");
    def is => ($ranB, 1, "Only ran once");
    def is => (Test::Stream::Sync->loaded, 1, "loaded");

    Test::Stream::Sync->post_load(sub { $ranA++ });
    def is => ($ranA, 2, "Ran right away");

    #####################
    # Reset everything
    $reset->();
}



( run in 1.225 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )