2017-06-13 19:41:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\CancelledException;
|
|
|
|
use Amp\Loop;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-06-13 19:41:47 +02:00
|
|
|
use Amp\TimeoutCancellationToken;
|
|
|
|
use Amp\TimeoutException;
|
2020-10-07 06:40:14 +02:00
|
|
|
use function Amp\delay;
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class TimeoutCancellationTokenTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testTimeout(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$line = __LINE__ + 1;
|
|
|
|
$token = new TimeoutCancellationToken(10);
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertFalse($token->isRequested());
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(20);
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertTrue($token->isRequested());
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
|
|
|
$token->throwIfRequested();
|
|
|
|
} catch (CancelledException $exception) {
|
|
|
|
$this->assertInstanceOf(TimeoutException::class, $exception->getPrevious());
|
2020-04-04 17:05:26 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$message = $exception->getPrevious()->getMessage();
|
|
|
|
$this->assertStringContainsString('TimeoutCancellationToken was created here', $message);
|
|
|
|
$this->assertStringContainsString('TimeoutCancellationTokenTest.php:' . $line, $message);
|
|
|
|
}
|
2017-06-13 19:41:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testWatcherCancellation(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$token = new TimeoutCancellationToken(1);
|
|
|
|
$this->assertSame(1, Loop::getInfo()["delay"]["enabled"]);
|
|
|
|
unset($token);
|
|
|
|
$this->assertSame(0, Loop::getInfo()["delay"]["enabled"]);
|
2017-06-13 19:41:47 +02:00
|
|
|
}
|
|
|
|
}
|