Acme-Parataxis
view release on metacpan or search on metacpan
t/256_driver_mojo.t view on Meta::CPAN
}
return $n;
}
ok !Acme::Parataxis->loop, 'no driver before any attach';
ok !Acme::Parataxis->attach_loop( Mojo::IOLoop->new ), 'attach_loop returns undef the first time';
my $drv = Acme::Parataxis->loop;
ok ref($drv) && $drv->isa('Acme::Parataxis::Driver'), 'current loop is a wrapped driver';
ok $drv->isa('Acme::Parataxis::Driver::Mojo'), 'it is the Mojo driver';
is Acme::Parataxis->attach_loop( Mojo::IOLoop->new ), $drv, 'reattaching returns the previous driver';
# Reattaching installs a *fresh* driver, so compare detach against the driver that is current right now rather than
# the $drv stashed before the reattach (a stashed one is stale for every driver, Mojo included).
my $current = Acme::Parataxis->loop;
is Acme::Parataxis->detach_loop(), $current, 'detach_loop returns the current driver';
ok !Acme::Parataxis->loop, 'no driver after detach';
like dies { Acme::Parataxis->attach_loop('garbage') }, qr[requires an event-loop object], 'attach_loop rejects a non object';
ok !dies { Acme::Parataxis->detach_loop() }, 'a detached handle has no effect';
subtest 'a parked driver read wakes when the peer writes' => sub {
my ( $a, $b ) = socket_pair();
Acme::Parataxis->attach_loop( Mojo::IOLoop->new );
my $e;
my $t0 = time;
Acme::Parataxis::run(
sub {
my $f = fiber { await_sleep(30); syswrite $a, 'ping' };
$e .= 'rc=' . await_read( $b, 2000 );
my $buf = '';
sysread $b, $buf, 4;
$e .= " buf=$buf";
}
);
my $ms = ( time - $t0 ) * 1000;
Acme::Parataxis->detach_loop;
is $e, 'rc=1 buf=ping', 'the parked read returned 1 with the bytes available';
ok $ms >= 20 && $ms < 1200, 'it woke on the write, not on its deadline (ms)';
};
subtest 'a short driver deadline returns -1 (resumed, not thrown)' => sub {
my ( $a, $b ) = socket_pair();
Acme::Parataxis->attach_loop( Mojo::IOLoop->new );
my $got;
my $t0 = time;
Acme::Parataxis::run( sub { $got = await_read( $b, 60 ) } );
my $ms = ( time - $t0 ) * 1000;
Acme::Parataxis->detach_loop;
is $got, -1, 'own deadline returned -1 like the pool path';
ok $ms >= 40 && $ms < 1100, 'the deadline really waited before firing (ms)';
};
subtest 'an enclosing with_timeout still interrupts a driver read' => sub {
my ( $a, $b ) = socket_pair();
Acme::Parataxis->attach_loop( Mojo::IOLoop->new );
my ( $err, $after );
Acme::Parataxis::run(
sub {
$err = dies {
with_timeout( 30, sub { await_read( $b, 2000 ) } );
};
$after = 'ran-on';
}
);
Acme::Parataxis->detach_loop;
ok ref($err) && $err->isa('Acme::Parataxis::Error::Timeout'), 'enclosing ::Timeout propagated';
is $err->kind, 'timeout', 'kind is timeout';
is $after, 'ran-on', 'the fiber continued after catching it';
};
subtest 'a driver sleep mixes with a concurrent pool job' => sub {
Acme::Parataxis->attach_loop( Mojo::IOLoop->new );
my ( $pool, $ms );
my $t0 = time;
Acme::Parataxis::run(
sub {
my $pw = fiber { await_sleep(20); 'pool-writer' };
await_sleep(40);
$pool = $pw->await;
}
);
$ms = ( time - $t0 ) * 1000;
Acme::Parataxis->detach_loop;
is $pool, 'pool-writer', 'the driver sleep ran the run while the pool fiber completed';
ok $ms >= 30 && $ms < 600, 'the sleep actually slept ~40ms (ms)';
};
# event-loop acceptance: high-volume readiness on the loop, and proof that the worker pool does none of it.
# Count every pool submission. _submit_job is the only gate onto submit_c_job (await_sleep, await_core_id,
# await_read and await_write all route through it) and run() never submits on its own, so a zero count while a
# loop is attached means no worker thread was ever asked to provide readiness or a sleep.
my $submits = 0;
my $orig_submit_job = \&Acme::Parataxis::_submit_job;
{
no warnings 'redefine';
*Acme::Parataxis::_submit_job = sub ( $type, $arg, $timeout ) { $submits++; $orig_submit_job->( $type, $arg, $timeout ) };
}
# Windows: stock Strawberry perl builds without d_poll, so Mojo::Reactor::Poll drives IO::Poll::_poll through its
# select fallback. The limit there is the descriptor NUMBER, not the watch count: winsock FD_SETSIZE truncates the
# fd_set at 64, so the reactor goes silent as soon as any watched fd reaches 64 (5 watches woke 5/5 at maxfd 13,
# 5 watches after 70 dummy fds woke 0/5), IO::Select never crashes but reports at most 64 ready handles, and the
# pure-Mojo stack crashes the interpreter above 128 pairs on perl 5.42.3. 24 pairs keep every fd under 64, which
# is enough to exercise the whole driver path on Windows; other platforms ask for the full 300, less whatever their
# own two ceilings cannot hold - see pairs_that_fit.
my $N = pairs_that_fit( $^O eq 'MSWin32' ? 24 : 300 );
# The cap is only as good as the number behind it, and a broken accessor would shrink this workload quietly rather
# than fail: 0 gives a negative cap, pairs_that_fit's halving loop never runs on a negative, and $N would just come
# back small with every assertion below still green. Ask for the number directly so that shows up as a failure.
ok Acme::Parataxis::fd_setsize() >= 64, 'the platform reports a usable FD_SETSIZE (' . Acme::Parataxis::fd_setsize() . ')';
ok $N >= 8, "the high-volume workload kept a workable $N pairs";
# A lost wakeup in the high-volume batch is intermittent and platform-shaped, so a bare count is close to useless
# when one does happen: it cannot say whether the stragglers were a contiguous tail or scattered singles, whether
# their descriptors clustered past a ceiling, or whether await_read hit its timeout (undef) or returned something
# else. Those three answers point at different bugs, so the failure diagnostic carries all of them along with the
# reactor actually in play. Only ever runs after the count assertion has already failed.
sub lost_diagnosis ( $got, $fd, $loop, $n, $ms, $snap, $arms_end, $awaiting ) {
my @lost = grep { !( defined $got->[$_] && $got->[$_] == 1 ) } 0 .. $#$got;
my @kept = grep { defined $got->[$_] && $got->[$_] == 1 } 0 .. $#$got;
my @out = ( 'lost ' . @lost . " of $n parked reads" );
return join "\n", @out if !@lost;
# A run of consecutive indices is one lost batch; scattered singles are a different failure.
my @runs;
for my $i (@lost) {
( run in 0.842 second using v1.01-cache-2.11-cpan-234fa220139 )