1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 18:07:57 +01:00
amp/test/TimeoutCancellationTokenTest.php

45 lines
1.3 KiB
PHP
Raw Normal View History

2017-06-13 19:41:47 +02:00
<?php
namespace Amp\Test;
use Amp\CancelledException;
use Amp\Loop;
use Amp\TimeoutCancellationToken;
use Amp\TimeoutException;
use function Amp\delay;
2017-06-13 19:41:47 +02:00
class TimeoutCancellationTokenTest extends BaseTest
2018-06-18 20:00:01 +02:00
{
public function testTimeout()
{
2017-06-13 19:41:47 +02:00
Loop::run(function () {
$line = __LINE__ + 1;
2017-06-13 19:41:47 +02:00
$token = new TimeoutCancellationToken(10);
$this->assertFalse($token->isRequested());
yield delay(20);
2017-06-13 19:41:47 +02:00
$this->assertTrue($token->isRequested());
try {
$token->throwIfRequested();
} catch (CancelledException $exception) {
$this->assertInstanceOf(TimeoutException::class, $exception->getPrevious());
$message = $exception->getPrevious()->getMessage();
$this->assertContains('TimeoutCancellationToken was created here', $message);
$this->assertContains('TimeoutCancellationTokenTest.php:' . $line, $message);
2017-06-13 19:41:47 +02:00
}
});
}
2018-06-18 20:00:01 +02:00
public function testWatcherCancellation()
{
2017-06-13 19:41:47 +02:00
Loop::run(function () {
$token = new TimeoutCancellationToken(1);
$this->assertSame(1, Loop::getInfo()["delay"]["enabled"]);
unset($token);
$this->assertSame(0, Loop::getInfo()["delay"]["enabled"]);
});
}
}