Async-Trampoline

 view release on metacpan or  search on metacpan

t/basic.t  view on Meta::CPAN

describe q(is_resolved()) => sub {
    my $async = async { return async_value; };

    ok !$async->is_resolved, "async thunk is not resolved";

    $async->run_until_completion;

    ok $async->is_resolved, "completed async is resolved";

    ok !(async_cancel)->is_resolved, "cancel is not resolved";
    ok +(async_error "error message")->is_resolved, "error is resolved";
    ok +(async_value)->is_resolved, "value is resolved";
};

describe q(is_cancelled()) => sub {
    my $async = async { return async_cancel; };

    ok !$async->is_cancelled, "async thunk is not cancelled";

    eval { $async->run_until_completion };  # throws due to cancellation

    ok $async->is_cancelled, "completed async is cancelled";

    ok +(async_cancel)->is_cancelled, "cancel is cancelled";
    ok !(async_error "error message")->is_cancelled, "error is not cancelled";
    ok !(async_value)->is_cancelled, "value is not cancelled";
};

describe q(is_error()) => sub {
    my $async = async { return async_error "error message" };

    ok !$async->is_error, "async thunk is not error";

    eval { $async->run_until_completion };  # throws errors

    ok $async->is_error, "completed async is error";

    ok !(async_cancel)->is_error, "cancel is not error";
    ok +(async_error "error message")->is_error, "error is error";
    ok !(async_value)->is_error, "value is not error";
};

describe q(is_value()) => sub {
    my $async = async { return async_value };

    ok !$async->is_value, "async thunk is not value";

    $async->run_until_completion;

    ok $async->is_value, "completed async is value";

    ok !(async_cancel)->is_value, "cancel is not value";
    ok !(async_error "error message")->is_value, "error is not value";
    ok +(async_value)->is_value, "value is value";
};

describe q(errors) => sub {
    # TODO perhaps retain async_error call location,
    # instead of throwing from run_until_completion()?

    it q(can be thrown from callbacks) => sub {
        my $file = __FILE__;

        my ($l, $async) = (__LINE__, async { die "my little error" });

        throws_ok { $async->run_until_completion }
            qr/\Amy little error at \Q$file\E line $l\.$/;

        ok $async->is_error;
    };

    it q(can be returned explicitly) => sub {
        my $file = __FILE__;

        my $async = async { async_error "explicit error message" };

        my $l = __LINE__; throws_ok { $async->run_until_completion }
            qr/\Aexplicit error message at \Q$file\E line $l\.$/;

        ok $async->is_error;
    };
};

describe q(concat()) => sub {
    it q(combines two values) => sub {
        my $x = async_value qw( 1 2 3 );
        my $y = async_value qw( a b );
        my @result = $x->concat($y)->run_until_completion;
        is "@result", "1 2 3 a b";
    };
};

done_testing;



( run in 0.463 second using v1.01-cache-2.11-cpan-39bf76dae61 )