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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
use v5.10;
use strict;
use warnings;
use Test2::V0 0.000148; # is_refcount
use Future;
use constant FUTURE_HAS_CONVERGENT_ALSO => ( !defined $Future::XS::VERSION or $Future::XS::VERSION >= 0.13 );
# One done
{
my $f1 = Future->new;
my $f2 = Future->new;
my $c2;
$f2->on_cancel( sub { $c2++ } );
my $future = Future->needs_any( $f1, $f2 );
is_oneref( $future, '$future has refcount 1 initially' );
# Two refs; one lexical here, one in $future
is_refcount( $f1, 2, '$f1 has refcount 2 after adding to ->needs_any' );
is_refcount( $f2, 2, '$f2 has refcount 2 after adding to ->needs_any' );
my $ready;
$future->on_ready( sub { $ready++ } );
ok( !$future->is_ready, '$future not yet ready' );
$f1->done( one => 1 );
is( $ready, 1, '$future is now ready' );
ok( $future->is_ready, '$future now ready after f1 ready' );
is( [ $future->result ], [ one => 1 ], 'results from $future->result' );
is( [ $future->pending_futures ],
[],
'$future->pending_futures after $f1 done' );
is( [ $future->ready_futures ],
[ $f1, $f2 ],
'$future->ready_futures after $f1 done' );
is( [ $future->done_futures ],
[ $f1 ],
'$future->done_futures after $f1 done' );
is( [ $future->failed_futures ],
[],
'$future->failed_futures after $f1 done' );
is( [ $future->cancelled_futures ],
[ $f2 ],
'$future->cancelled_futures after $f1 done' );
is_refcount( $future, 1, '$future has refcount 1 at end of test' );
undef $future;
is_refcount( $f1, 1, '$f1 has refcount 1 at end of test' );
is_refcount( $f2, 1, '$f2 has refcount 1 at end of test' );
is( $c2, 1, 'Unfinished child future cancelled on failure' );
}
# One fails
{
my $f1 = Future->new;
my $f2 = Future->new;
my $future = Future->needs_any( $f1, $f2 );
my $ready;
$future->on_ready( sub { $ready++ } );
ok( !$future->is_ready, '$future not yet ready' );
$f1->fail( "Partly fails" );
ok( !$future->is_ready, '$future not yet ready after $f1 fails' );
$f2->done( two => 2 );
ok( $future->is_ready, '$future now ready after $f2 done' );
is( [ $future->result ], [ two => 2 ], '$future->result after $f2 done' );
is( [ $future->done_futures ],
[ $f2 ],
'$future->done_futures after $f2 done' );
is( [ $future->failed_futures ],
[ $f1 ],
'$future->failed_futures after $f2 done' );
}
# All fail
{
my $f1 = Future->new;
my $f2 = Future->new;
my $future = Future->needs_any( $f1, $f2 );
my $ready;
$future->on_ready( sub { $ready++ } );
ok( !$future->is_ready, '$future not yet ready' );
$f1->fail( "Partly fails" );
$f2->fail( "It fails" );
is( $ready, 1, '$future is now ready' );
ok( $future->is_ready, '$future now ready after f2 fails' );
is( $future->failure, "It fails", '$future->failure yields exception' );
my $file = __FILE__;
my $line = __LINE__ + 1;
like( dies { $future->result }, qr/^It fails at \Q$file line $line\E\.?\n$/, '$future->result throws exception' );
is( [ $future->failed_futures ],
[ $f1, $f2 ],
'$future->failed_futures after all fail' );
}
# immediately done
{
my $future = Future->needs_any( Future->fail("F1"), Future->done );
ok( $future->is_ready, '$future of already-done sub already ready' );
}
# immediately fails
{
my $future = Future->needs_any( Future->fail("F1") );
ok( $future->is_ready, '$future of already-failed sub already ready' );
$future->failure;
}
# cancel propagation
{
my $f1 = Future->new;
my $c1;
$f1->on_cancel( sub { $c1++ } );
my $f2 = Future->new;
my $c2;
$f2->on_cancel( sub { $c2++ } );
my $future = Future->needs_all( $f1, $f2 );
$f2->fail( "booo" );
$future->cancel;
is( $c1, 1, '$future->cancel marks subs cancelled' );
is( $c2, undef, '$future->cancel ignores ready subs' );
}
# cancelled convergent
{
my $f1 = Future->new;
my $f2 = Future->new;
my $future = Future->needs_any( $f1, $f2 );
$f1->cancel;
ok( !$future->is_ready, '$future not yet ready after first cancellation' );
$f2->done( "result" );
is( [ $future->done_futures ],
[ $f2 ],
'->done_futures with cancellation' );
is( [ $future->cancelled_futures ],
[ $f1 ],
'->cancelled_futures with cancellation' );
my $f3 = Future->new;
$future = Future->needs_any( $f3 );
$f3->cancel;
ok( $future->is_ready, '$future is ready after final cancellation' );
like( scalar $future->failure, qr/ cancelled/, 'Failure mentions cancelled' );
}
# 'also' subs don't get cancelled
if( FUTURE_HAS_CONVERGENT_ALSO ) {
my $falso = Future->new;
my $calso;
$falso->on_cancel( sub { $calso++ } );
my $future = Future->needs_any( also => $falso );
$future->cancel;
is( $calso, undef, '$future->cancel does not cancel $falso' );
my $f1 = Future->new;
$future = Future->needs_any( $f1, also => $falso );
$f1->done;
is( $calso, undef, '$f1->done does not cancel $falso' );
}
# needs_any on none
{
my $f = Future->needs_any( () );
ok( $f->is_ready, 'needs_any on no futures already done' );
is( scalar $f->failure, "Cannot ->needs_any with no subfutures",
'->result on empty needs_any is empty' );
}
# weakself retention (RT120468)
{
my $f = Future->new;
my $wait;
$wait = Future->needs_any(
$f,
my $cancelled = Future->new->on_cancel( sub {
undef $wait;
}),
);
ok( lives { $f->done(1) },
'no problems cancelling a Future which clears the original ->needs_any ref' );
ok( $cancelled->is_cancelled, 'cancellation occured as expected' );
ok( $f->is_done, '->needs_any is marked as done' );
}
# With a cancelled subfuture (RT144459)
foreach ([ precancelled => 0 ], [ postcancelled => 1 ])
{
my ( $title, $cancel_after ) = @$_;
my $f1 = Future->new;
my $f2 = Future->new;
$f2->cancel if !$cancel_after;
my $f = Future->needs_any( $f1, $f2 );
$f2->cancel if $cancel_after;
ok( !$f->is_ready, "needs_any is pending before f1 done for $title" );
$f1->done;
ok( $f->is_done, "needs_any now done after f1 done for $title" );
}
done_testing;
|