2017-06-13 19:41:47 +02:00
|
|
|
<?php
|
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
namespace Amp\Cancellation;
|
2017-06-13 19:41:47 +02:00
|
|
|
|
|
|
|
use Amp\CancelledException;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2021-12-02 22:24:56 +01:00
|
|
|
use Amp\TimeoutCancellation;
|
2017-06-13 19:41:47 +02:00
|
|
|
use Amp\TimeoutException;
|
2021-10-15 00:50:40 +02:00
|
|
|
use Revolt\EventLoop;
|
2021-09-19 06:04:20 +02:00
|
|
|
use function Amp\delay;
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
class TimeoutCancellationTest 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;
|
2021-12-03 01:02:05 +01:00
|
|
|
$cancellation = new TimeoutCancellation(0.01);
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2021-12-03 01:02:05 +01:00
|
|
|
self::assertFalse($cancellation->isRequested());
|
2021-08-29 19:18:24 +02:00
|
|
|
delay(0.02);
|
2021-12-03 01:02:05 +01:00
|
|
|
self::assertTrue($cancellation->isRequested());
|
2017-06-13 19:41:47 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
2021-12-03 01:02:05 +01:00
|
|
|
$cancellation->throwIfRequested();
|
2020-09-28 05:19:52 +02:00
|
|
|
} catch (CancelledException $exception) {
|
2021-03-26 22:34:32 +01:00
|
|
|
self::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();
|
2021-08-30 07:07:10 +02:00
|
|
|
|
2021-11-14 18:35:07 +01:00
|
|
|
if ((int) \ini_get('zend.assertions') > 0) {
|
2021-12-02 22:24:56 +01:00
|
|
|
self::assertStringContainsString('TimeoutCancellation was created here', $message);
|
|
|
|
self::assertStringContainsString('TimeoutCancellationTest.php:' . $line, $message);
|
2021-08-30 07:07:10 +02:00
|
|
|
}
|
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
|
|
|
{
|
2021-10-15 00:50:40 +02:00
|
|
|
$enabled = EventLoop::getInfo()["delay"]["enabled"];
|
2021-12-03 01:02:05 +01:00
|
|
|
$cancellation = new TimeoutCancellation(0.001);
|
2021-10-15 00:50:40 +02:00
|
|
|
self::assertSame($enabled + 1, EventLoop::getInfo()["delay"]["enabled"]);
|
2021-12-03 01:02:05 +01:00
|
|
|
unset($cancellation);
|
2021-10-15 00:50:40 +02:00
|
|
|
self::assertSame($enabled, EventLoop::getInfo()["delay"]["enabled"]);
|
2017-06-13 19:41:47 +02:00
|
|
|
}
|
|
|
|
}
|