Last active
October 30, 2022 10:41
-
-
Save Marcool04/cfeca8df967c832a0560fcf6f123f10d 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 Test; | |
class X::Timeout is Exception { | |
has $.payload; | |
method message() { | |
"Timeout error occurred: {self.payload}"; | |
} | |
} | |
throws-like { | |
await timeout_promise( | |
start { | |
sleep 5; | |
42; | |
}, | |
timeout => 2, | |
description => "5 sec promise to 42" | |
); | |
}, | |
X::Timeout, | |
"promise timeout yields correct error", | |
message => /Timeout.*/; | |
my Str $actual_result = "HERE IS MY RESULT"; | |
my $two_sec_result; | |
lives-ok { | |
$two_sec_result = await timeout_promise( | |
Promise.in(2).then({ $actual_result; }), | |
timeout => 4, | |
); | |
}, | |
"2 second timed out promise didn't time out under 4 seconds"; | |
is $two_sec_result, $actual_result, "2 second promise result is correct"; | |
sub timeout_promise( | |
Promise $promise, | |
Int :$timeout!, | |
Str :$description = "", | |
--> Promise | |
) { | |
say "Requested a $timeout second timed out promise for $description"; | |
await Promise.anyof( | |
$promise, | |
Promise.in($timeout); | |
).then({ | |
if ($promise.status != Kept) { | |
X::Timeout.new(payload => "$description").throw; | |
} | |
$promise; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now gives: