Subject: | $f1->on_ready($f2) and $f1->cancel() throws an exception |
With the attached test script, I examined Future's behavior when it is
cancelled. Then I found that $f1->on_ready($f2) and $f1->cancel()
throws an exception.
Example:
my $f1 = Future->new;
my $f2 = Future->new;
$f1->on_ready($f2);
$f1->cancel();
Result: $f1->cancel() throws an exception
Future=HASH(0x9a76fd8) ->fail requires an exception that is true at eg.pl line 10.
It throws an excetion if on_ready() is called after cancel(), too.
The document does not clearly explain what will happen to $f2 when $f1
is cancelled. I suppose $f2 should be cancelled, too.
Subject: | test_callbacks_on_cancel.t |
use strict;
use warnings;
use Future 0.11;
use Test::More;
use Test::Fatal qw(lives_ok);
note("Test whether various callbacks are executed or not on cancelling.");
{
my $f = Future->new;
$f->cancel();
ok($f->is_ready, "If the Future is cancelled, is_ready should be true");
}
note("--- on_ready(CODE), cancel later");
lives_ok {
my $f = Future->new;
my $executed = 0;
$f->on_ready(sub { $executed = 1 });
$f->cancel();
ok($executed, "on_ready CODE should be executed on cancel");
} 'on_ready(CODE) should live';
note("--- on_ready(CODE), already canceled");
lives_ok {
my $f = Future->new;
my $executed = 0;
$f->cancel();
$f->on_ready(sub { $executed = 1 });
ok($executed, "on_ready CODE should be executed on cancel");
} 'on_ready(CODE) should live';
note("--- on_ready(FUTURE), cancel later");
lives_ok {
my $f = Future->new;
my $cf = Future->new;
$f->on_ready($cf);
$f->cancel();
ok($cf->is_ready, "child future should be ready"); ## really?
ok($cf->is_cancelled, "child future should be cancelled"); ## really?
} 'on_ready(FUTURE) should live';
note("--- on_ready(FUTURE), already canceled");
lives_ok {
my $f = Future->new;
my $cf = Future->new;
$f->cancel();
$f->on_ready($cf);
ok($cf->is_ready, "child future should be ready"); ## really?
ok($cf->is_cancelled, "child future should be cancelled"); ## really?
} 'on_ready(FUTURE) should live';
foreach my $method (qw(on_done on_fail)) {
note("--- $method(CODE), cancel later");
lives_ok {
my $f = Future->new;
my $executed = 0;
$f->$method(sub { $executed = 1 });
$f->cancel();
ok(!$executed, "$method CODE should not be executed on cancel");
} "$method(CODE) should live";
note("--- $method(CODE), already canceled");
lives_ok {
my $f = Future->new;
my $executed = 0;
$f->cancel();
$f->$method(sub { $executed = 1 });
ok(!$executed, "$method CODE should not be executed on cancel");
} "$method(CODE) should live";
note("--- $method(FUTURE), cancel later");
lives_ok {
my $f = Future->new;
my $cf = Future->new;
$f->$method($cf);
$f->cancel();
ok(!$cf->is_ready, "child future should not be ready");
} "$method(FUTURE) should live";
note("--- $method(FUTURE), already cancelled");
lives_ok {
my $f = Future->new;
my $cf = Future->new;
$f->cancel();
$f->$method($cf);
ok(!$cf->is_ready, "child future should not be ready");
} "$method(FUTURE) should live";
}
foreach my $method (qw(and_then or_else followed_by)) {
note("--- $method(CODE)");
lives_ok {
my $f1 = Future->new;
my $executed = 0;
my $f = $f1->$method(sub {
$executed = 1;
return Future->done;
});
$f->cancel();
ok(!$executed, "$method CODE should not be executed on cancel");
ok($f1->is_cancelled, "$method f1 should be cancelled");
} "$method(CODE) should live";
}
done_testing();