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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use Future;
use Future::AsyncAwait;
# foreach(ARRAY) await
{
my @F = map { Future->new } 1 .. 3;
async sub with_foreach_array
{
foreach my $f ( @F ) {
defined $f or die "ARGH: expected a Future";
await $f;
}
return "end foreach";
}
my $fret = with_foreach_array();
$F[0]->done;
$F[1]->done;
$F[2]->done;
is( scalar $fret->get, "end foreach", '$fret now ready after foreach(ARRAY) loop' );
}
# foreach(LIST) await
{
my @F = map { Future->new } 1 .. 3;
async sub with_foreach_list
{
foreach my $f ( $F[0], $F[1], $F[2] ) {
defined $f or die "ARGH: expected a Future";
await $f;
}
return "end foreach";
}
my $fret = with_foreach_list();
$F[0]->done;
$F[1]->done;
$F[2]->done;
is( scalar $fret->get, "end foreach", '$fret now ready after foreach(LIST) loop' );
}
# foreach(LAZY IV) await
{
my @F = map { Future->new } 1 .. 3;
async sub with_foreach_lazy_iv
{
foreach my $idx ( 0 .. 2 ) {
defined $idx or die "ARGH: Expected an integer index";
await $F[$idx];
}
return "end foreach";
}
my $fret = with_foreach_lazy_iv();
$F[0]->done;
$F[1]->done;
$F[2]->done;
is( scalar $fret->get, "end foreach", '$fret now ready after foreach(LAZY IV) loop' );
}
# foreach(LAZY SV) await
{
my %F = map { $_ => Future->new } 'a' .. 'c';
async sub with_foreach_lazy_sv
{
foreach my $key ( 'a' .. 'c' ) {
defined $key or die "ARGH: Expected a string key";
await $F{$key};
}
return "end foreach";
}
my $fret = with_foreach_lazy_sv();
$F{a}->done;
$F{b}->done;
$F{c}->done;
is( scalar $fret->get, "end foreach", '$fret now ready after foreach(LAZY SV) loop' );
}
# RT#124144
{
my $f1 = Future->new;
my $f2 = Future->new;
async sub with_foreach_await_twice
{
foreach my $x ( 0 ) {
await $f1;
await $f2;
}
return "awaited twice";
}
my $fret = with_foreach_await_twice();
$f1->done;
$f2->done;
is( scalar $fret->get, "awaited twice", '$fret now ready after foreach with two awaits' );
}
# RT#129215
{
my @F = map { Future->new } 0, 1, 2;
my $fret = (async sub {
foreach my $idx ( 0, 1, 2 ) {
await $F[$idx];
}
return "OK";
})->();
# Arrange for the stack to be at different heights on each resume
my $tmp = do { 1 + ( $F[0]->done // 0 ) };
$tmp = [ 2, 3, [ $F[1]->done ] ];
$tmp = 4 * ( 6 + ( $F[2]->done // 0 ) );
is( scalar $fret->get, "OK", '$fret now ready after differing stack resumes' );
}
# RT#129319 - foreach(LIST) with extra values
{
my @F = map { Future->new } 0, 1, 2;
my $fret = (async sub {
my $ret = "";
foreach my $idx ( 0, 1, 2 ) {
# $ret will appear on the stack after the foreach-LIST items
$ret .= await $F[$idx];
}
return $ret;
})->();
$F[0]->done( "A" );
$F[1]->done( "B" );
$F[2]->done( "C" );
is( scalar $fret->get, "ABC", '$fret now ready after await with stack items before LIST' );
}
# RT#129319 - foreach(LIST) with extra marks
{
my @F = map { Future->new } 0, 1, 2;
my $fret = (async sub {
my @values;
foreach my $idx ( 0, 1, 2 ) {
# push list creates an extra mark
push @values, "(", await $F[$idx], ")";
}
return join "", @values;
})->();
$F[0]->done( "A" );
$F[1]->done( "B" );
$F[2]->done( "C" );
is( scalar $fret->get, "(A)(B)(C)", '$fret now ready after await with stack marks before LIST' );
}
SKIP: {
skip "IO::Async::Loop not available", 1 unless eval { require IO::Async::Loop; };
my $loop = IO::Async::Loop->new;
my $out = "";
(async sub {
foreach my $k (qw( one two three four )) {
$out .= "$k\n";
await $loop->delay_future(after => 0.01);
$out .= "$k\n";
}
})->()->get;
is( $out, "one\none\ntwo\ntwo\nthree\nthree\nfour\nfour\n",
'Output from sleepy foreach(LIST)'
);
}
{
our $VAR;
my $f1;
my $ok = !eval q{
async sub foreach_pkgvar
{
foreach $VAR ( 1 .. 3 ) {
await $f1;
}
}
};
my $e = $@;
ok( $ok, 'await in non-lexical foreach loop fails to compile' );
$ok and like( $e, qr/^await is not allowed inside foreach on non-lexical iterator variable /, '' );
}
done_testing;
|