1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 01:19:46 +01:00
amp/test/CancellationToken/CancellationTest.php

64 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2021-09-06 06:47:06 +02:00
namespace Amp\CancellationToken;
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;
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;
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 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
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
}
}