2017-05-14 22:46:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\AsyncGenerator;
|
2017-05-14 22:46:33 +02:00
|
|
|
use Amp\CancellationToken;
|
|
|
|
use Amp\CancellationTokenSource;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-05-16 21:56:52 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\Pipeline;
|
2021-03-26 22:34:32 +01:00
|
|
|
use Revolt\EventLoop\Loop;
|
|
|
|
use function Revolt\EventLoop\delay;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class CancellationTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testCancellationCancelsIterator(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$pipeline = $this->createAsyncIterator($cancellationSource->getToken());
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$count = 0;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
while (null !== $current = $pipeline->continue()) {
|
|
|
|
$count++;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertIsInt($current);
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
if ($current === 3) {
|
|
|
|
$cancellationSource->cancel();
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|
2020-09-28 05:19:52 +02:00
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame(4, $count);
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testUnsubscribeWorks(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$id = $cancellationSource->getToken()->subscribe(function () {
|
|
|
|
$this->fail("Callback has been called");
|
|
|
|
});
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
$this->assertTrue(true);
|
|
|
|
});
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->getToken()->unsubscribe($id);
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->cancel();
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testThrowingCallbacksEndUpInLoop(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
throw new TestException;
|
2017-05-16 21:56:52 +02:00
|
|
|
});
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->cancel();
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
delay(10); // Tick event loop to invoke callbacks.
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertInstanceOf(TestException::class, $reason);
|
2020-09-28 05:19:52 +02:00
|
|
|
}
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testDoubleCancelOnlyInvokesOnce(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->cancel();
|
|
|
|
$cancellationSource->cancel();
|
2017-05-16 21:56:52 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testCalledIfSubscribingAfterCancel(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
2017-05-16 21:56:52 +02:00
|
|
|
}
|
2021-03-26 22:34:32 +01:00
|
|
|
|
|
|
|
private function createAsyncIterator(CancellationToken $cancellationToken): Pipeline
|
|
|
|
{
|
|
|
|
return new AsyncGenerator(function () use ($cancellationToken): \Generator {
|
|
|
|
$running = true;
|
|
|
|
$cancellationToken->subscribe(function () use (&$running): void {
|
|
|
|
$running = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
while ($running) {
|
|
|
|
yield $i++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|