File: 06await-nested.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 (116 lines) | stat: -rw-r--r-- 1,871 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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Future;

use Future::AsyncAwait;

my $orig_cxstack_ix = Future::AsyncAwait::__cxstack_ix;

my $f;
my $failure;

async sub inner
{
   my $ret = await $f;
   die $failure if defined $failure;
   return $ret;
}

async sub outer
{
   await inner();
}

# await through two nested async sub calls
# See also RT123062
{
   $f = Future->new;

   my $fret = outer();
   $f->done( "value" );

   is( scalar $fret->get, "value", '$fret->get through two nested async subs' );
}

# die after double await
# See also RT126037
{
   $f = Future->new;

   my $fret = outer();
   $failure = "Oopsie\n";

   $f->done( "result" );

   is( scalar $fret->failure, "Oopsie\n", '$fret->failure through two nested async subs' );
}

# await through two nested async method calls
{
   my $f = Future->new;

   package TestObj {
      async sub inner {
         await $f;
      }

      async sub outer {
         my $mth = "inner";
         await shift->$mth;
      }
   }

   my $fret = TestObj->outer();
   $f->done( "value" );

   is( scalar $fret->get, "value", '$fret->get through two nested async methods' );
}

# await twice nested
{
   my @f;

   async sub f2
   {
      await $f[0];
   }

   async sub f1
   {
      await f2();
      await f2();
   }

   @f = map { Future->new } 1 .. 2;
   my $fret = f1();
   ( shift @f )->done( "result" ) while @f;

   is( scalar $fret->get, "result", '$fret->get through nested double wait' );
}

# nested failure
{
    my $f = Future->new;

    async sub func_fail {
        await $f;
    }
    async sub func_fail_wrap {
        await func_fail();
    }

    my $fret = func_fail_wrap();
    $f->fail("aiee\n");

    is( $fret->failure, "aiee\n", "nested fail" );
}

is( Future::AsyncAwait::__cxstack_ix, $orig_cxstack_ix,
   'cxstack_ix did not grow during the test' );

done_testing;