1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
use v5.10;
use strict;
use warnings;
use Test2::V0;
use Future;
# ->get on immediate done
{
my $f = Future->done( result => "here" );
is( [ $f->get ], [ result => "here" ], 'Result of ->get on done future' );
}
# ->get on immediate fail
{
my $f = Future->fail( "Something broke" );
like( dies { $f->get }, qr/^Something broke at /, 'Exception from ->get on failed future' );
}
# ->get on cancelled
{
my $f = Future->new;
$f->cancel;
like( dies { $f->get }, qr/cancelled/, 'Exception from ->get on cancelled future' );
}
# ->get while pending without await
{
my $f = Future->new;
like( dies { $f->get }, qr/ is not yet complete /, 'Exception from ->get on pending future' );
}
# ->get invokes ->await
{
no strict 'refs';
no warnings 'redefine';
local *{"Future::await"} = sub {
shift->done( "result of await" );
};
my $f = Future->new;
is( scalar $f->get, "result of await", 'Result of ->get with overloaded ->await' );
}
# ->failure invokes ->await
{
no strict 'refs';
no warnings 'redefine';
local *{"Future::await"} = sub {
shift->fail( "Oopsie\n" );
};
my $f = Future->new;
is( scalar $f->failure, "Oopsie\n", 'Result of ->failure with overloaded ->await' );
}
# ->await on already-complete future succeeds
{
my $e;
ok( !( $e = dies { Future->done( "Result" )->await } ),
'->await on done does not throw' ) or
diag( "Exception was: $e" );
ok( !( $e = dies { Future->fail( "Oops\n" )->await } ),
'->await on done does not throw' ) or
diag( "Exception was: $e" );
}
done_testing;
|