2017-05-14 22:46:33 +02:00
|
|
|
<?php
|
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
namespace Amp\Cancellation;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
use Amp\DeferredCancellation;
|
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-10-15 00:50:40 +02:00
|
|
|
use Revolt\EventLoop;
|
2021-09-19 06:04:20 +02:00
|
|
|
use function Amp\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
|
|
|
{
|
2021-12-02 22:24:56 +01:00
|
|
|
$deferredCancellation = new DeferredCancellation;
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
$id = $deferredCancellation->getCancellation()->subscribe(function () {
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->fail("Callback has been called");
|
|
|
|
});
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
$deferredCancellation->getCancellation()->subscribe(function () {
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertTrue(true);
|
|
|
|
});
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
$deferredCancellation->getCancellation()->unsubscribe($id);
|
2017-05-14 22:46:33 +02:00
|
|
|
|
2022-01-16 16:21:21 +01:00
|
|
|
self::assertFalse($deferredCancellation->isCancelled());
|
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
$deferredCancellation->cancel();
|
2022-01-16 16:21:21 +01:00
|
|
|
|
|
|
|
self::assertTrue($deferredCancellation->isCancelled());
|
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
|
|
|
{
|
2021-10-15 00:50:40 +02:00
|
|
|
EventLoop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
|
2020-09-28 05:19:52 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-05-16 21:56:52 +02:00
|
|
|
|
2021-12-02 22:24:56 +01:00
|
|
|
$cancellationSource = new DeferredCancellation;
|
|
|
|
$cancellationSource->getCancellation()->subscribe(function () {
|
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();
|
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
|
|
|
{
|
2021-12-02 22:24:56 +01:00
|
|
|
$cancellationSource = new DeferredCancellation;
|
2021-12-03 00:47:30 +01:00
|
|
|
$cancellationSource->getCancellation()->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
|
|
|
{
|
2021-12-02 22:24:56 +01:00
|
|
|
$cancellationSource = new DeferredCancellation;
|
2020-09-28 05:19:52 +02:00
|
|
|
$cancellationSource->cancel();
|
2021-12-03 00:47:30 +01:00
|
|
|
$cancellationSource->getCancellation()->subscribe($this->createCallback(1));
|
2017-05-16 21:56:52 +02:00
|
|
|
}
|
2022-01-16 16:21:21 +01:00
|
|
|
|
|
|
|
public function testCancelOnDestruct(): void
|
|
|
|
{
|
|
|
|
$cancellationSource = new DeferredCancellation;
|
|
|
|
$cancellationSource->getCancellation()->subscribe($this->createCallback(1));
|
|
|
|
unset($cancellationSource);
|
|
|
|
}
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|