Async-Event-Interval

 view release on metacpan or  search on metacpan

t/53-timeout.t  view on Meta::CPAN

{
    my $e = Async::Event::Interval->new(0.1, sub {
        select(undef, undef, undef, 5);
    });
    $e->timeout(1);
    $e->start;
    select(undef, undef, undef, 2);
    is $e->errors, 1, "interval over timeout: errors incremented";
    like $e->error_message, qr/Callback timed out after 1 second/,
        "interval over timeout: error_message records timeout";
    is $e->status, 0,
        "interval over timeout: status() is 0 (child no longer running)";
    is $e->error, 1,
        "interval over timeout: error() is 1 (event needs restart)";
    is $e->pid, undef,
        "interval over timeout: pid() cleared by _detect_crash";
}

# Changing timeout() mid-stream takes effect on the next iteration.
# _run_callback reads $self->timeout from shared %events on each entry,
# so a setter call in the parent is visible to the child's next call.
{
    my $e = Async::Event::Interval->new(0.1, sub {
        select(undef, undef, undef, 2);
    });
    $e->timeout(5);                          # generous, 2s callback completes
    $e->start;
    my $waited = 0;
    until ($e->runs >= 1 || $waited >= 10) {
        select(undef, undef, undef, 0.1);
        $waited += 0.1;
    }
    is $e->errors, 0,
        "dynamic timeout: no errors under generous initial timeout";
    cmp_ok $e->runs, '>=', 1,
        "dynamic timeout: at least one iteration completed";

    $e->timeout(1);                          # shorten below callback runtime
    select(undef, undef, undef, 3.5);        # next iteration starts & fires
    is $e->errors, 1,
        "dynamic timeout: error recorded after timeout was shortened";
    like $e->error_message, qr/timed out after 1 second/,
        "dynamic timeout: error_message reflects the new timeout";
}

# info() snapshot includes the timeout value.
{
    my $e = Async::Event::Interval->new(0.5, sub {});
    $e->timeout(30);
    is $e->info->{timeout}, 30, "info() includes timeout value";
}

# events() snapshot includes the timeout value.
{
    my $e = Async::Event::Interval->new(0.5, sub {});
    $e->timeout(15);
    my $snap = Async::Event::Interval::events();
    is $snap->{$e->id}{timeout}, 15, "events() includes timeout value";
}

# Timeout persists across restart.
{
    my $e = Async::Event::Interval->new(0, sub {
        select(undef, undef, undef, 5);
    });
    $e->timeout(1);
    $e->start;
    select(undef, undef, undef, 2);
    is $e->errors, 1, "first run timed out";

    $e->error;  # trigger _detect_crash so _started is cleared

    $e->restart;
    select(undef, undef, undef, 2);
    is $e->errors, 2, "second run also timed out (timeout persisted)";
    like $e->error_message, qr/timed out after 1 second/,
        "error_message still matches timeout pattern after restart";
}



( run in 1.789 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )