1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/test/Cancellation/TimeoutCancellationTest.php
2021-12-03 01:02:05 +01:00

46 lines
1.4 KiB
PHP

<?php
namespace Amp\Cancellation;
use Amp\CancelledException;
use Amp\PHPUnit\AsyncTestCase;
use Amp\TimeoutCancellation;
use Amp\TimeoutException;
use Revolt\EventLoop;
use function Amp\delay;
class TimeoutCancellationTest extends AsyncTestCase
{
public function testTimeout(): void
{
$line = __LINE__ + 1;
$cancellation = new TimeoutCancellation(0.01);
self::assertFalse($cancellation->isRequested());
delay(0.02);
self::assertTrue($cancellation->isRequested());
try {
$cancellation->throwIfRequested();
} catch (CancelledException $exception) {
self::assertInstanceOf(TimeoutException::class, $exception->getPrevious());
$message = $exception->getPrevious()->getMessage();
if ((int) \ini_get('zend.assertions') > 0) {
self::assertStringContainsString('TimeoutCancellation was created here', $message);
self::assertStringContainsString('TimeoutCancellationTest.php:' . $line, $message);
}
}
}
public function testWatcherCancellation(): void
{
$enabled = EventLoop::getInfo()["delay"]["enabled"];
$cancellation = new TimeoutCancellation(0.001);
self::assertSame($enabled + 1, EventLoop::getInfo()["delay"]["enabled"]);
unset($cancellation);
self::assertSame($enabled, EventLoop::getInfo()["delay"]["enabled"]);
}
}