-
-
Save zaucker/f9cb13381383a93bbe6aa879cb8307d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Promise; | |
use Test::More; | |
sub firstSuccessfulSequential { | |
my @promises = @_; | |
note "* " . scalar @promises . " to test"; | |
unless (@promises) { | |
return Mojo::Promise->new->reject('No input promises'); | |
} | |
my $out = Mojo::Promise->new; | |
my $p = shift @promises; | |
$p | |
->then(sub { | |
note "* Success"; | |
$out->resolve(shift); | |
}) | |
->catch(sub { | |
note "* Failure"; | |
unless (@promises) { | |
note "* No more promises"; | |
return $out->reject(shift); | |
} | |
note "* " .scalar @promises . " left"; | |
firstSuccessfulSequential(@promises); | |
}); | |
return $out; | |
} | |
subtest "firstSuccessfulSequential" => sub { | |
plan tests => 5; | |
{ | |
firstSuccessfulSequential() | |
->then(sub { fail 'No promises makes it fail' }) | |
->catch(sub { pass 'No promises makes it fail' }); | |
Mojo::IOLoop->start; | |
} | |
{ | |
my $p1 = Mojo::Promise->new; | |
my $p2 = Mojo::Promise->new; | |
firstSuccessfulSequential($p1, $p2) | |
->then(sub { is shift(), 'first completed', 'Correct outcome when first and second complete' }) | |
->catch(sub { fail "Unexpected failure" }); | |
Mojo::IOLoop->next_tick(sub { | |
$p1->resolve('first completed'); | |
$p2->resolve('second completed'); | |
}); | |
Mojo::IOLoop->start; | |
} | |
{ | |
my $p1 = Mojo::Promise->new; | |
my $p2 = Mojo::Promise->new; | |
firstSuccessfulSequential($p1, $p2) | |
->then(sub { is shift(), 'first completed', 'Correct outcome when first completes, second fails' }) | |
->catch(sub { fail "Unexpected failure" }); | |
Mojo::IOLoop->next_tick(sub { | |
$p1->resolve('first completed'); | |
$p2->reject('second rejected'); | |
}); | |
Mojo::IOLoop->start; | |
} | |
{ | |
my $p1 = Mojo::Promise->new; | |
my $p2 = Mojo::Promise->new; | |
firstSuccessfulSequential($p1, $p2) | |
->then(sub { is shift(), 'second completed', 'Correct outcome when first fails, second completes' }) | |
->catch(sub { fail "Unexpected failure" }); | |
Mojo::IOLoop->next_tick(sub { | |
$p1->reject('first rejected'); | |
$p2->resolve('second completed'); | |
}); | |
Mojo::IOLoop->start; | |
} | |
{ | |
my $p1 = Mojo::Promise->new; | |
my $p2 = Mojo::Promise->new; | |
firstSuccessfulSequential($p1, $p2) | |
->then(sub { fail 'If all fail, result fails' }) | |
->catch(sub { pass 'If all fails, result fails' }); | |
Mojo::IOLoop->next_tick(sub { | |
$p1->reject('first failed'); | |
$p2->reject('second failed'); | |
}); | |
Mojo::IOLoop->start; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment