2017-05-14 22:46:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\CancellationToken;
|
|
|
|
use Amp\CancellationTokenSource;
|
|
|
|
use Amp\Emitter;
|
|
|
|
use Amp\Loop;
|
2017-05-16 21:56:52 +02:00
|
|
|
use Amp\PHPUnit\TestCase;
|
|
|
|
use Amp\PHPUnit\TestException;
|
|
|
|
use Amp\Success;
|
2017-05-14 22:46:33 +02:00
|
|
|
use function Amp\asyncCall;
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class CancellationTest extends TestCase
|
|
|
|
{
|
|
|
|
private function createAsyncIterator(CancellationToken $cancellationToken)
|
|
|
|
{
|
2017-05-14 22:46:33 +02:00
|
|
|
$emitter = new Emitter;
|
|
|
|
|
|
|
|
asyncCall(function () use ($emitter, $cancellationToken) {
|
|
|
|
$running = true;
|
|
|
|
|
|
|
|
$cancellationToken->subscribe(function () use (&$running) {
|
|
|
|
$running = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
while ($running) {
|
|
|
|
yield $emitter->emit($i++);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $emitter->iterate();
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCancellationCancelsIterator()
|
|
|
|
{
|
2017-05-14 22:46:33 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
|
|
|
|
$iterator = $this->createAsyncIterator($cancellationSource->getToken());
|
|
|
|
|
|
|
|
$current = null;
|
|
|
|
|
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$current = $iterator->getCurrent();
|
|
|
|
|
|
|
|
$this->assertInternalType("int", $current);
|
|
|
|
|
|
|
|
if ($current === 3) {
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(3, $current);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testUnsubscribeWorks()
|
|
|
|
{
|
2017-05-14 22:46:33 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
|
|
|
|
$id = $cancellationSource->getToken()->subscribe(function () {
|
|
|
|
$this->fail("Callback has been called");
|
|
|
|
});
|
|
|
|
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
$this->assertTrue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$cancellationSource->getToken()->unsubscribe($id);
|
|
|
|
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
});
|
|
|
|
}
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testSubscriptionsRunAsCoroutine()
|
|
|
|
{
|
2017-05-16 21:56:52 +02:00
|
|
|
$this->expectOutputString("abc");
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
print yield new Success("a");
|
|
|
|
print yield new Success("b");
|
|
|
|
print yield new Success("c");
|
|
|
|
});
|
|
|
|
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testThrowingCallbacksEndUpInLoop()
|
|
|
|
{
|
2017-05-16 21:56:52 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$this->expectException(TestException::class);
|
|
|
|
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
throw new TestException;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
} catch (TestException $e) {
|
|
|
|
$this->fail("Exception thrown from cancel instead of being thrown into the loop.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testThrowingCallbacksEndUpInLoopIfCoroutine()
|
|
|
|
{
|
2017-05-16 21:56:52 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$this->expectException(TestException::class);
|
|
|
|
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe(function () {
|
|
|
|
if (false) {
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new TestException;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
} catch (TestException $e) {
|
|
|
|
$this->fail("Exception thrown from cancel instead of being thrown into the loop.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testDoubleCancelOnlyInvokesOnce()
|
|
|
|
{
|
2017-05-16 21:56:52 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
|
|
|
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCalledIfSubscribingAfterCancel()
|
|
|
|
{
|
2017-05-16 21:56:52 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$cancellationSource = new CancellationTokenSource;
|
|
|
|
$cancellationSource->cancel();
|
|
|
|
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
|
|
|
});
|
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|