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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
BEGIN {
plan skip_all => "Future is not available"
unless eval { require Future };
plan skip_all => "Future::AsyncAwait >= 0.50 is not available"
unless eval { require Future::AsyncAwait;
Future::AsyncAwait->VERSION( '0.50' ) };
plan skip_all => "Syntax::Keyword::Try >= 0.22 is not available"
unless eval { require Syntax::Keyword::Try;
Syntax::Keyword::Try->VERSION( '0.22' ) };
Future::AsyncAwait->import;
Syntax::Keyword::Try->import;
diag( "Future::AsyncAwait $Future::AsyncAwait::VERSION, " .
"Syntax::Keyword::Try $Syntax::Keyword::Try::VERSION" );
}
# await in try/catch
{
async sub with_trycatch
{
my $f = shift;
my $ret;
try {
await $f;
$ret = "result";
}
catch ($e) {
$ret = "oopsie";
}
return $ret;
}
my $f1 = Future->new;
my $fdone = with_trycatch( $f1 );
$f1->done;
is( scalar $fdone->get, "result", '$fdone for successful await in try/catch' );
my $f2 = Future->new;
my $ffail = with_trycatch( $f2 );
$f2->fail( "fail" );
is( scalar $ffail->get, "oopsie", '$ffail for failed await in try/catch' );
}
# await in try/catch with return
{
my $fellthrough;
async sub with_trycatch_return
{
my $f = shift;
try {
await $f;
return "result";
}
catch ($e) {}
$fellthrough++;
return "fallthrough";
}
my $f1 = Future->new;
my $fdone = with_trycatch_return( $f1 );
$f1->done;
is( scalar $fdone->get, "result", '$fdone for successful await in try/catch with return' );
ok( !$fellthrough, 'fallthrough after try{return} did not happen' );
}
# await in try/catch list context (RT134790)
{
async sub return_list { return ( "first", "second" ); }
async sub await_return_list
{
try {
return await return_list();
}
catch ($e) { die $e; }
}
my ( $r1, $r2 ) = await await_return_list();
is( $r1, "first", 'first result from try/return list' );
is( $r2, "second", 'second result from try/return list' );
}
# await in try/finally
{
async sub with_tryfinally
{
my $f = shift;
my $ret = "";
try {
await $f;
$ret .= "T";
}
finally {
$ret .= "F";
}
return $ret;
}
my $f1 = Future->new;
my $fret = with_tryfinally( $f1 );
$f1->done;
is( scalar $fret->get, "TF", '$fret for await in try/finally' );
}
# finally still runs for cancel (RT135351)
{
my $ok;
my $f1 = Future->new;
my $fret = (async sub {
try {
await $f1;
}
finally {
$ok++;
}
})->();
ok( !$ok, 'defer {} not run before ->cancel' );
$fret->cancel;
ok( $ok, 'defer {} was run after ->cancel' );
}
# await in toplevel try
{
try {
is( await Future->done( "success" ), "success",
'await in toplevel try' );
}
catch {
fail( 'await in toplevel try' );
}
try {
await Future->fail( "failure\n" );
}
catch ( $e ) {
is( $e, "failure\n", 'await in toplevel try/catch failure' );
}
}
done_testing;
|