Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FGasper committed Feb 21, 2021
1 parent 1589639 commit 2b46287
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
47 changes: 47 additions & 0 deletions t/async_await.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use Promise::XS;

# This test throws unhandled-rejection warnings … do they matter?
#use Test::FailWarnings;

my $failed_why;

BEGIN {
eval 'use Test::Future::AsyncAwait::Awaitable; 1' or $failed_why = $@;
}

if (!$failed_why) {
my $backend;

my @backends = ('AnyEvent', 'IO::Async', 'Mojolicious');

for my $try (@backends) {
if (eval "require $try") {
$backend = $try;
last;
}
}

if ($backend) {
Promise::XS::use_event($backend);
}
else {
$failed_why = "No event interface (@backends) is available.";
}
}

plan skip_all => "Can’t run test: $failed_why" if $failed_why;

Test::Future::AsyncAwait::Awaitable::test_awaitable(
'Conforms to Awaitable API',
class => 'Promise::XS::Promise',
new => sub { Promise::XS::deferred()->promise() },
);

done_testing;
49 changes: 49 additions & 0 deletions t/async_await_hold_ref.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::FailWarnings;

BEGIN {
for my $req ( qw( Future::AsyncAwait AnyEvent ) ) {
eval "require $req" or plan skip_all => 'No Future::AsyncAwait';
}
}

use Promise::XS;

use Future::AsyncAwait future_class => 'Promise::XS::Promise';

sub delay {
my $secs = shift;

my $d = Promise::XS::deferred();

my $timer; $timer = AnyEvent->timer(
after => $secs,
cb => sub {
undef $timer;
$d->resolve($secs);
},
);

return $d->promise();
}

async sub thethings {
await delay(0.1);

return 5;
}

my $cv = AnyEvent->condvar();

thethings()->then($cv);

my ($got) = $cv->recv();

is $got, 5, 'expected resolution';

done_testing;

0 comments on commit 2b46287

Please sign in to comment.