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\Deferred;
|
2017-05-14 22:46:33 +02:00
|
|
|
use Amp\Loop;
|
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;
|
2017-05-16 21:56:52 +02:00
|
|
|
use Amp\Success;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\sleep;
|
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
|
|
|
private function createAsyncIterator(CancellationToken $cancellationToken): Pipeline
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
return new AsyncGenerator(function () use ($cancellationToken): \Generator {
|
2017-05-14 22:46:33 +02:00
|
|
|
$running = true;
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationToken->subscribe(function () use (&$running): void {
|
2017-05-14 22:46:33 +02:00
|
|
|
$running = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
while ($running) {
|
2020-09-28 05:19:52 +02:00
|
|
|
yield $i++;
|
2017-05-14 22:46:33 +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
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->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
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->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 testSubscriptionsRunAsCoroutine(): \Generator
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$deferred = new Deferred;
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectOutputString("abc");
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () use ($deferred) {
|
|
|
|
print yield new Success("a");
|
|
|
|
print yield new Success("b");
|
|
|
|
print yield new Success("c");
|
|
|
|
$deferred->resolve();
|
2017-05-16 21:56:52 +02:00
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
|
|
|
|
yield $deferred->promise();
|
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
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
sleep(0); // Tick event loop to invoke callbacks.
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertInstanceOf(TestException::class, $reason);
|
|
|
|
}
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testThrowingCallbacksEndUpInLoopIfCoroutine(): void
|
|
|
|
{
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
if (false) {
|
|
|
|
yield;
|
2017-05-16 21:56:52 +02:00
|
|
|
}
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
throw new TestException;
|
2017-05-16 21:56:52 +02:00
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
|
|
|
|
sleep(1); // Tick event loop a couple of times to invoke callbacks.
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TestException::class, $reason);
|
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
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|