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

146 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
2020-09-28 05:19:52 +02:00
use Amp\AsyncGenerator;
use Amp\CancellationToken;
use Amp\CancellationTokenSource;
2020-09-28 05:19:52 +02:00
use Amp\Deferred;
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;
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 {
$running = true;
2020-09-28 05:19:52 +02:00
$cancellationToken->subscribe(function () use (&$running): void {
$running = false;
});
$i = 0;
while ($running) {
2020-09-28 05:19:52 +02:00
yield $i++;
}
});
}
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;
2020-09-28 05:19:52 +02:00
$pipeline = $this->createAsyncIterator($cancellationSource->getToken());
2020-09-28 05:19:52 +02:00
$count = 0;
2020-09-28 05:19:52 +02:00
while (null !== $current = $pipeline->continue()) {
$count++;
2020-09-28 05:19:52 +02:00
$this->assertIsInt($current);
2020-09-28 05:19:52 +02:00
if ($current === 3) {
$cancellationSource->cancel();
}
2020-09-28 05:19:52 +02:00
}
2020-09-28 05:19:52 +02:00
$this->assertSame(4, $count);
}
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;
2020-09-28 05:19:52 +02:00
$id = $cancellationSource->getToken()->subscribe(function () {
$this->fail("Callback has been called");
});
2020-09-28 05:19:52 +02:00
$cancellationSource->getToken()->subscribe(function () {
$this->assertTrue(true);
});
2020-09-28 05:19:52 +02:00
$cancellationSource->getToken()->unsubscribe($id);
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
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
}
}