Skip to content

Commit

Permalink
tests pass, but script fails?
Browse files Browse the repository at this point in the history
  • Loading branch information
FGasper committed Dec 2, 2020
1 parent eb04cb9 commit 1bd2144
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
3 changes: 3 additions & 0 deletions MANIFEST
Expand Up @@ -3,6 +3,8 @@ LICENSE
MANIFEST
Makefile.PL
README.md
await.pl
await_simple.pl
lib/Promise/ES6.pm
lib/Promise/ES6/AnyEvent.pm
lib/Promise/ES6/Backend/PP.pm
Expand All @@ -17,6 +19,7 @@ t/all.t
t/all_async.t
t/allsettled.t
t/anyevent.t
t/async_await.t
t/await.t
t/await_then_with_async.t
t/await_with_async.t
Expand Down
68 changes: 68 additions & 0 deletions await.pl
@@ -0,0 +1,68 @@
#!/usr/bin/env perl

use strict;
use warnings;

use experimental 'signatures';

use FindBin;
use lib "$FindBin::Bin/lib";
use Promise::ES6;

use blib "$FindBin::Bin/Future-AsyncAwait-0.47";

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

use IO::Async::Loop;
use IO::Async::Timer::Countdown;

sub delay ($loop, $secs) {
return Promise::ES6->new( sub ($res, @) {
my $timer; $timer = IO::Async::Timer::Countdown->new(
delay => $secs,
on_expire => sub {
undef $timer;
$res->($secs);
},
);

$timer->start();
$loop->add($timer);
} );
}

async sub thethings ($loop) {
print "waiting …\n";

my $waited_p = delay($loop, 0.2);

my $waited = await $waited_p;

print "waited $waited\n";

return 5;
}

#sub thethings_plain {
# print "waiting …\n";
# return delay(3.2)->then( sub ($val) {
# print "waited $val\n";
# 5;
# } );
#}

my $loop = IO::Async::Loop->new();
Promise::ES6::use_event('IO::Async', $loop);

# It works thus:
#my $promise1 = thethings($loop);
#my $promise = $promise1->then( sub ($val) {

my $promise = thethings($loop)->then( sub ($val) {
print "async gave $val\n";
$loop->stop();
} );

$loop->run();

1;
30 changes: 30 additions & 0 deletions await_simple.pl
@@ -0,0 +1,30 @@
#!/usr/bin/env perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";
use Promise::ES6;

use Time::HiRes;

#use blib "$FindBin::Bin/Future-AsyncAwait-0.47";

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

my $resolver_cr;
my $p = Promise::ES6->new( sub { $resolver_cr = shift } );

async sub do_await {
my $p = shift;
return await $p;
}

do_await($p)->then( sub { print "got " . shift . $/ } );

Time::HiRes::sleep(0.1);

$resolver_cr->(5);

1;
49 changes: 49 additions & 0 deletions lib/Promise/ES6/Backend/PP.pm
Expand Up @@ -334,4 +334,53 @@ sub DESTROY {
}
}

#----------------------------------------------------------------------

# Future::AsyncAwait::Awaitable interface:

sub AWAIT_NEW_DONE {
(ref($_[0]) || $_[0])->resolve( $_[1] );
}

sub AWAIT_NEW_FAIL {
(ref($_[0]) || $_[0])->reject( $_[1] );
}

sub AWAIT_CLONE {
(ref $_[0])->new(\&_noop);
}

sub AWAIT_DONE {
my $copy = $_[1];

$_[0]->_settle(bless \$copy, _RESOLUTION_CLASS);
}

sub AWAIT_FAIL {
my $copy = $_[1];

$_[0]->_settle(bless \$copy, _REJECTION_CLASS);
}

sub AWAIT_IS_READY {
!UNIVERSAL::isa( $_[0]->[_VALUE_SR_IDX], _PENDING_CLASS );
}

sub AWAIT_IS_CANCELLED { 0 }

sub AWAIT_GET {
return ${ $_[0]->[_VALUE_SR_IDX] } if UNIVERSAL::isa( $_[0]->[_VALUE_SR_IDX], _RESOLUTION_CLASS );

die ${ $_[0]->[_VALUE_SR_IDX] };
}

sub _noop {}

sub AWAIT_ON_READY {
$_[0]->finally($_[1])->catch(\&noop);
}

sub AWAIT_CHAIN_CANCEL { }
sub AWAIT_ON_CANCEL { }

1;
27 changes: 27 additions & 0 deletions t/async_await.t
@@ -0,0 +1,27 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

# 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 = $@;
}

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

use Promise::ES6;

Test::Future::AsyncAwait::Awaitable::test_awaitable(
'Promise::ES6 conforms to Awaitable API',
class => 'Promise::ES6',
new => sub { Promise::ES6->new( sub {} ) },
);

done_testing;

0 comments on commit 1bd2144

Please sign in to comment.