1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/TimeoutCancellationTokenTest.php

42 lines
1.2 KiB
PHP
Raw Normal View History

2017-06-13 19:41:47 +02:00
<?php
namespace Amp\Test;
use Amp\CancelledException;
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;
2021-03-26 22:34:32 +01:00
use Revolt\EventLoop\Loop;
use function Revolt\EventLoop\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
2021-03-26 22:34:32 +01:00
self::assertFalse($token->isRequested());
delay(20);
2021-03-26 22:34:32 +01:00
self::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) {
2021-03-26 22:34:32 +01:00
self::assertInstanceOf(TimeoutException::class, $exception->getPrevious());
2020-09-28 05:19:52 +02:00
$message = $exception->getPrevious()->getMessage();
2021-03-26 22:34:32 +01:00
self::assertStringContainsString('TimeoutCancellationToken was created here', $message);
self::assertStringContainsString('TimeoutCancellationTokenTest.php:' . $line, $message);
2020-09-28 05:19:52 +02:00
}
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);
2021-03-26 22:34:32 +01:00
self::assertSame(1, Loop::getInfo()["delay"]["enabled"]);
2020-09-28 05:19:52 +02:00
unset($token);
2021-03-26 22:34:32 +01:00
self::assertSame(0, Loop::getInfo()["delay"]["enabled"]);
2017-06-13 19:41:47 +02:00
}
}