Acme-Parataxis
view release on metacpan or search on metacpan
t/153_nursery.t view on Meta::CPAN
await_sleep(100);
$tok_done = 1;
$tok->unregister;
}
);
$n->spawn( sub { die 'boom' } );
}
);
};
};
ok !$wt_done, 'a child parked inside a with_timeout was cancelled (propagated to the nested deadline token)';
ok !$tok_done, 'a child parked on its own registered token was cancelled';
};
subtest 'the nursery token is public: cancelling it cancels the children' => sub {
my $done = 0;
async {
eval {
nursery(
sub ($n) {
is ref( $n->token ), 'Acme::Parataxis::CancellationToken', 'the block sees the nursery token';
$n->token->cancel;
$n->spawn( sub { await_sleep(30); $done = 1 } );
}
);
};
};
ok !$done, 'a user-cancelled token cancels the children like a failed sibling';
};
subtest 'a block error cancels its children, drains them, and rethrows the block error' => sub {
my ( $block_err, $done ) = ( undef, 0 );
async {
$block_err = dies {
nursery(
sub ($n) {
$n->spawn( sub { await_sleep(50); $done = 1 } );
die 'block died';
}
);
};
};
like "$block_err", qr/^block died/, 'the block error propagates unchanged';
ok !$done, 'the child was cancelled, not left running';
};
subtest 'a nursery inside with_timeout propagates the timeout and drains its children' => sub {
my ( $t_err, $done ) = ( undef, 0 );
async {
$t_err = dies {
with_timeout(
10,
sub { # deadline shorter than the join, so the enclosing timeout must win
nursery(
sub ($n) {
$n->spawn( sub { await_sleep(200); $done = 1 } );
$n->spawn( sub { await_sleep(200) } );
}
);
}
);
};
};
ok ref($t_err) && $t_err->isa('Acme::Parataxis::Error::Timeout'), 'the enclosing timeout wins over the join';
ok !$done, 'the children were cancelled and drained, not left running';
};
subtest 'destructors of in-flight children run during cancellation' => sub {
$DtorProbe::n = 0;
async {
eval {
nursery(
sub ($n) {
$n->spawn( sub { my $d = DtorProbe->new; await_sleep(50) } );
$n->spawn( sub { die 'boom' } );
}
);
};
};
is $DtorProbe::n, 1, 'the cancelled child gave back its in-flight object while unwinding';
};
subtest 'a bare fiber inside the block is not adopted by the nursery' => sub {
my $plain = 0;
async {
nursery(
sub ($n) {
fiber { await_sleep(2); $plain = 1 };
}
);
ok !$plain, 'the bare fiber is not joined by the nursery and is still running when it returns';
};
ok $plain, 'the independent fiber completed on its own after the nursery returned';
};
subtest 'no orphan fibers on any exit path' => sub {
my $base = Acme::Parataxis::get_live_fiber_count();
async {
eval {
nursery(
sub ($n) {
$n->spawn( sub { await_sleep(20) } );
$n->spawn( sub { die 'x' } );
}
);
};
nursery(
sub ($n) {
$n->spawn( sub {1} );
$n->spawn( sub { await_sleep(1); 'ok' } );
}
);
};
is Acme::Parataxis::get_live_fiber_count(), $base, 'success and failure paths both leave zero live fibers';
};
subtest 'nursery() and ->spawn croak outside a scheduled fiber' => sub {
like dies {
nursery( sub {1} )
}, qr/scheduled fiber/, 'nursery() croaks at the top level';
my $n = Acme::Parataxis::Nursery->new;
like dies {
$n->spawn( sub {1} )
}, qr/scheduled fiber/, '->spawn croaks at the top level';
};
#
done_testing;
( run in 1.233 second using v1.01-cache-2.11-cpan-234fa220139 )