File: async_await__several_awaits.t

package info (click to toggle)
libpromise-xs-perl 0.20-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: perl: 1,097; ansic: 355; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;

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

use Promise::XS;

my $failed_why;

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

    if (!$failed_why) {
        eval 'require Future::AsyncAwait' or $failed_why = $@;
    }

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

use Future::AsyncAwait;

SKIP: {
    eval 'require AnyEvent' or skip $@, 1;

    Promise::XS::use_event('AnyEvent');

    my $timer_cr = sub {
        my $d = Promise::XS::deferred();

        my $t;
        $t = AnyEvent->timer(
            after => 0.01,
            cb => sub {
                $d->resolve(42, 53);
                undef $t;
            },
        );

        return $d->promise();
    };

    my $label = 'AnyEvent';

    my @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (redux)");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (yet again)");
}

SKIP: {
    eval 'require IO::Async::Loop' or skip $@, 1;

    my $loop = IO::Async::Loop->new();

    Promise::XS::use_event('IO::Async', $loop);

    my $timer_cr = sub {
        my $d = Promise::XS::deferred();

        $loop->watch_time(
            after => 0.01,
            code => sub {
                $d->resolve(42, 53);
            },
        );

        return $d->promise();
    };

    my $label = 'IO::Async';

    my @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (redux)");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (yet again)");
}

SKIP: {
    eval 'require Mojo::IOLoop' or skip $@, 1;

    Promise::XS::use_event('Mojo::IOLoop');

    my $timer_cr = sub {
        my $d = Promise::XS::deferred();

        Mojo::IOLoop->timer( 0.01, sub { $d->resolve(42, 53) } );

        return $d->promise();
    };

    my $label = 'Mojo';

    my @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (redux)");

    @vals = await $timer_cr->();
    is_deeply(\@vals, [42, 53], "$label: await() return (yet again)");
}

done_testing();