File: 70await%2Bfeature-try.t

package info (click to toggle)
libfuture-asyncawait-perl 0.70-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 528 kB
  • sloc: perl: 2,647; ansic: 118; pascal: 34; makefile: 3
file content (118 lines) | stat: -rw-r--r-- 2,429 bytes parent folder | download
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
#!/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.10 is not available"
      unless eval { require Future::AsyncAwait;
                    Future::AsyncAwait->VERSION( '0.10' ) };
   plan skip_all => "feature 'try' is not available"
      unless $] >= 5.033007;

   diag( "Future::AsyncAwait $Future::AsyncAwait::VERSION, " .
         "core perl version $^V" );
}

use Future::AsyncAwait;

use feature 'try';
no warnings 'experimental::try';

# 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 toplevel try
{
   try {
      is( await Future->done( "success" ), "success",
         'await in toplevel try' );
   }
   catch ($e) {
      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;