2017-05-14 22:46:33 +02:00
|
|
|
<?php
|
|
|
|
|
2021-09-06 06:47:06 +02:00
|
|
|
namespace Amp\CancellationToken;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
|
|
|
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;
|
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 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-08-29 19:18:24 +02:00
|
|
|
delay(0.01); // 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
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|