mirror of
https://github.com/danog/amp.git
synced 2024-12-12 09:29:45 +01:00
c98c9a40d2
Added delay() and trap() functions.
46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Amp\CancellationToken;
|
|
|
|
use Amp\CancelledException;
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
use Amp\TimeoutCancellationToken;
|
|
use Amp\TimeoutException;
|
|
use Revolt\EventLoop\Loop;
|
|
use function Amp\delay;
|
|
|
|
class TimeoutCancellationTokenTest extends AsyncTestCase
|
|
{
|
|
public function testTimeout(): void
|
|
{
|
|
$line = __LINE__ + 1;
|
|
$token = new TimeoutCancellationToken(0.01);
|
|
|
|
self::assertFalse($token->isRequested());
|
|
delay(0.02);
|
|
self::assertTrue($token->isRequested());
|
|
|
|
try {
|
|
$token->throwIfRequested();
|
|
} catch (CancelledException $exception) {
|
|
self::assertInstanceOf(TimeoutException::class, $exception->getPrevious());
|
|
|
|
$message = $exception->getPrevious()->getMessage();
|
|
|
|
if ((int)ini_get('zend.assertions') > 0) {
|
|
self::assertStringContainsString('TimeoutCancellationToken was created here', $message);
|
|
self::assertStringContainsString('TimeoutCancellationTokenTest.php:' . $line, $message);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function testWatcherCancellation(): void
|
|
{
|
|
$enabled = Loop::getInfo()["delay"]["enabled"];
|
|
$token = new TimeoutCancellationToken(0.001);
|
|
self::assertSame($enabled + 1, Loop::getInfo()["delay"]["enabled"]);
|
|
unset($token);
|
|
self::assertSame($enabled, Loop::getInfo()["delay"]["enabled"]);
|
|
}
|
|
}
|