Developer-Dashboard

 view release on metacpan or  search on metacpan

t/233-goroutine-shared-object-coverage.t  view on Meta::CPAN

is( $fresh->status, 'pending', 'a fresh routine reports pending' );
ok( !$fresh->is_running, 'a pending routine is not running' );
ok( !$fresh->is_killed, 'a pending routine is not killed' );
ok( !$fresh->cancel_requested, 'a pending routine has no cancel requested' );
ok( $fresh->age >= 0, 'a fresh routine reports a non-negative age' );
ok( $fresh->kill, 'kill cancels a pending routine at once' );
is( $fresh->status, 'killed', 'killed pending routine reports killed' );
ok( $fresh->is_killed, 'killed routine is marked as killed' );
ok( !$fresh->kill, 'kill on a finished routine returns false' );

my $chained = go sub { 42 };
is( $chained->timeout(0.25), $chained, 'valid timeout returns the routine for chaining' );
wait_all();
is( $chained->status, 'done', 'finished routine reports done' );
ok( !$chained->is_killed, 'done routine is not marked as killed' );

my $slow = go(sub { 1 }, { after => 10 });
$slow->timeout(0.05);
Developer::Dashboard::GoRoutine::pump(0.5);
is( $slow->status, 'timed_out', 'a routine past its timeout reports timed_out' );
ok( $slow->is_killed, 'timed-out routine is marked as killed' );

my $no_timeout = go(sub { 1 }, { after => 0.2 });
$no_timeout->timeout(0.01);
$no_timeout->timeout(0);
Developer::Dashboard::GoRoutine::pump(0.05);
is( $no_timeout->status, 'pending', 'timeout(0) disables a pending timeout' );
wait_all();
is( $no_timeout->status, 'done', 'routine with a disabled timeout still runs' );

my $exiter = go sub { exit };
wait_all();
is( $exiter->status, 'exited', 'exit inside a routine reports exited' );
ok( !$exiter->is_killed, 'exited routine is not marked as killed' );

my $running_seen;
my $self_observed;
$self_observed = go sub { $running_seen = $self_observed->is_running };
wait_all();
ok( $running_seen, 'routine reports running while its callback executes' );

my $co_op;
my $saw_cancel = 0;
$co_op = go sub {
    for (1 .. 500) {
        Developer::Dashboard::GoRoutine::pump(0.01);
        if ($co_op->cancel_requested) { $saw_cancel = 1; last; }
    }
};
my $killer = go(sub { $co_op->kill }, { after => 0.05 });
wait_all();
ok( $saw_cancel, 'running routine observes the cooperative cancel flag' );
is( $co_op->status, 'killed', 'cooperatively cancelled routine reports killed' );
ok( $co_op->is_killed, 'cooperatively cancelled routine is marked as killed' );
is( $killer->status, 'done', 'killer routine finishes normally' );

# -------------------------------------------------------------------------
# Timeout validation
# -------------------------------------------------------------------------

for my $bad_timeout ( undef, [], 'not-a-number', 'NaN', 'Inf', '1e999', -1 ) {
    my $routine = bless {}, 'Developer::Dashboard::GoRoutine::Routine';
    my $ok = eval { $routine->timeout($bad_timeout); 1 };
    ok( !$ok, 'invalid timeout is rejected' );
    like( $@, qr/timeout\(\) requires a non-negative number/, 'invalid timeout has a clear diagnostic' );
}

{
    package Local::NaNTimeout;
    use overload
        '""' => sub { return '1' },
        '0+' => sub { return POSIX::nan() },
        fallback => 1;
    package main;

    my $routine = bless {}, 'Developer::Dashboard::GoRoutine::Routine';
    my $nan_timeout = bless {}, 'Local::NaNTimeout';
    my $ok = eval { $routine->timeout($nan_timeout); 1 };
    ok( !$ok, 'timeout rejects a value that stringifies numerically but numifies to NaN' );
    like( $@, qr/timeout\(\) requires a non-negative number/,
        'NaN timeout rejection uses the public validation diagnostic' );
}

# -------------------------------------------------------------------------
# _share_many and share on pre-tied variables
# -------------------------------------------------------------------------

{
    my $scalar_value = 21;
    my @array_values = qw(alpha beta);
    my %hash_values = ( key => 'value' );
    Developer::Dashboard::GoRoutine::_share_many(
        \$scalar_value, \@array_values, \%hash_values
    );
    is( 0 + $scalar_value, 21, '_share_many accepts a scalar reference and preserves its initial value' );
    is_deeply( [@array_values], [qw(alpha beta)], '_share_many accepts an array and preserves its items' );
    is_deeply( { %hash_values }, { key => 'value' }, '_share_many accepts a hash and preserves its entries' );

    my $already_shared = eval { Developer::Dashboard::GoRoutine::_share_many(
            \$scalar_value, \@array_values, \%hash_values
        ); 1 };
    ok( $already_shared, '_share_many accepts its own already-shared scalar, array, and hash values' );

    my $code = sub { return 1 };
    my $unsupported = eval {
        Developer::Dashboard::GoRoutine::_share_many($code);
        1;
    };
    ok( !$unsupported, '_share_many rejects a code reference' );
    like( $@, qr/expects scalar, array, or hash/, 'unsupported shared-reference error names accepted types' );

    my $unsupported_scalar = eval {
        Developer::Dashboard::GoRoutine::_share_many('ordinary value');
        1;
    };
    ok( !$unsupported_scalar, '_share_many rejects a non-reference value' );
    like( $@, qr/expects scalar, array, or hash/,
        'non-reference rejection names the accepted shared types' );
}

{
    package Local::UnsupportedSharedScalar;
    sub TIESCALAR { return bless {}, shift }
    package Local::UnsupportedSharedArray;
    sub TIEARRAY { return bless {}, shift }
    package Local::UnsupportedSharedHash;
    sub TIEHASH { return bless {}, shift }
    package main;

    tie my $foreign_scalar, 'Local::UnsupportedSharedScalar';
    tie my @foreign_array, 'Local::UnsupportedSharedArray';
    tie my %foreign_hash, 'Local::UnsupportedSharedHash';
    for my $case (
        [ \$foreign_scalar, qr/already tied scalar/ ],
        [ \@foreign_array, qr/already tied array/ ],
        [ \%foreign_hash, qr/already tied hash/ ],
    ) {
        my $accepted = eval { Developer::Dashboard::GoRoutine::_share_many( $case->[0] ); 1 };
        ok( !$accepted, '_share_many rejects an incompatible pre-tied aggregate' );
        like( $@, $case->[1], 'incompatible tied-aggregate diagnostic is explicit' );
    }

    my $share_tied_ok = eval { share $foreign_scalar; 1 };
    ok( !$share_tied_ok, 'share rejects an already tied scalar' );
    like( $@, qr/already tied scalar/, 'already tied scalar diagnostic is explicit' );
}

done_testing;

__END__

=head1 NAME

t/233-goroutine-shared-object-coverage.t - exercise GoRoutine shared values as plain Perl values

=head1 PURPOSE

This test exercises shared scalars, hashes, arrays, scalar references,
blessed values, regexps, code references, filehandles, and formats under the
single-threaded GoRoutine engine, where C<share> is a no-op and every shared
value is just a plain Perl value in the one interpreter. It also covers the
channel and routine-handle public API with real routines: pending/killed/
timed-out/exited/done statuses, cooperative cancellation, timeout validation
(including a NaN overload), and the C<_share_many> helper. Routines observe
the same shared values the main program sees, and channel values pass by
reference.

=head1 WHY IT EXISTS

The old version of this file covered the tied-proxy marshalling layer that
adapted native shared nodes to ordinary Perl references. That layer is gone
with the perl_clone engine, so the file now pins the new contract instead:
plain values, final observable values, and the public routine/channel API.
The separate integration tests exercise real routine scheduling outside
Devel::Cover instrumentation.

=head1 WHEN TO USE

Run this test whenever GoRoutine shared values, the source filter, timeout
validation, or routine and channel handle behaviour change:

    prove -Iblib/lib -Iblib/arch t/233-goroutine-shared-object-coverage.t

=head1 HOW TO USE

The test requires the checkout's built XS object and loads it through the
existing C<Developer::Dashboard::HtmlEscape> distribution module. It uses
ordinary Perl operations against shared values and real C<go> routines, so it
never creates a worker thread. Run it with the normal test library paths and
the coverage gate will include its Perl-module execution in the lib-only
report.

=head1 EXAMPLES

Run the focused shared-value contract:

    prove -lv t/233-goroutine-shared-object-coverage.t

Run it as part of all tests and coverage:

    d2 docker compose exec dev perl script/coverage-gate

=head1 WHAT USES IT

The repository test and coverage workflows use this file to measure the Perl
shared-value and handle code while the integration tests independently verify
routine scheduling. It is not installed as an end-user command.

=cut



( run in 1.509 second using v1.01-cache-2.11-cpan-60884a8df76 )