2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\TimeoutException;
|
2021-03-26 22:34:32 +01:00
|
|
|
use function Amp\asyncValue;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\await;
|
2021-03-26 22:34:32 +01:00
|
|
|
use function Revolt\EventLoop\delay;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class TimeoutTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testSuccessfulPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$value = 1;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise = new Success($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise = Promise\timeout($promise, 10);
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame($value, await($promise));
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testFailedPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$exception = new \Exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise = new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
|
|
|
await(Promise\timeout($promise, 10));
|
|
|
|
} catch (\Throwable $reason) {
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame($exception, $reason);
|
2020-09-28 05:19:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::fail("Promise should have failed");
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testFastPending(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
$promise = asyncValue(10, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame($value, await(Promise\timeout($promise, 20)));
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke onResolve callback to remove watcher.
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testSlowPending(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2021-03-26 22:34:32 +01:00
|
|
|
$promise = asyncValue(20);
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await(Promise\timeout($promise, 10));
|
2021-03-26 22:34:32 +01:00
|
|
|
self::fail("Promise did not fail");
|
2020-09-28 05:19:52 +02:00
|
|
|
} catch (TimeoutException $reason) {
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertNull(await($promise));
|
2020-09-28 05:19:52 +02:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-03-13 05:27:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSuccessfulPromise
|
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testSlowPendingWithDefault(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$value = 0;
|
|
|
|
$default = 1;
|
2017-03-13 05:27:43 +01:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
$promise = asyncValue(20, $value);
|
2017-03-13 05:27:43 +01:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame($default, await(Promise\timeoutWithDefault($promise, 10, $default)));
|
2017-03-13 05:27:43 +01:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame($value, await($promise));
|
2017-03-13 05:27:43 +01:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|