From 112e8139140d38dc8d94047167a366a87718cac7 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 2 Dec 2021 22:24:56 +0100 Subject: [PATCH] Rename CancellationToken to Cancellation --- ...CancellationToken.php => Cancellation.php} | 8 +-- src/CancelledException.php | 4 +- ...ionToken.php => CompositeCancellation.php} | 31 ++++++------ ...kenSource.php => DeferredCancellation.php} | 28 +++++------ src/Future.php | 18 +++---- src/Future/functions.php | 24 ++++----- .../{CancellableToken.php => Cancellable.php} | 4 +- src/Internal/FutureIterator.php | 10 ++-- ...ationToken.php => WrappedCancellation.php} | 6 +-- ...ellationToken.php => NullCancellation.php} | 2 +- ...ationToken.php => TimeoutCancellation.php} | 6 +-- src/TimeoutException.php | 2 +- src/functions.php | 50 ++++++++++--------- .../CancellationTest.php | 26 +++++----- .../TimeoutCancellationTest.php} | 14 +++--- test/Future/AllTest.php | 6 +-- test/Future/AnyTest.php | 6 +-- test/Future/FutureTest.php | 14 +++--- test/Future/RaceTest.php | 6 +-- test/Future/SettleTest.php | 6 +-- test/Future/SomeTest.php | 9 ++-- 21 files changed, 142 insertions(+), 138 deletions(-) rename src/{CancellationToken.php => Cancellation.php} (79%) rename src/{CombinedCancellationToken.php => CompositeCancellation.php} (62%) rename src/{CancellationTokenSource.php => DeferredCancellation.php} (57%) rename src/Internal/{CancellableToken.php => Cancellable.php} (94%) rename src/Internal/{WrappedCancellationToken.php => WrappedCancellation.php} (79%) rename src/{NullCancellationToken.php => NullCancellation.php} (93%) rename src/{TimeoutCancellationToken.php => TimeoutCancellation.php} (91%) rename test/{CancellationToken => Cancellation}/CancellationTest.php (55%) rename test/{CancellationToken/TimeoutCancellationTokenTest.php => Cancellation/TimeoutCancellationTest.php} (78%) diff --git a/src/CancellationToken.php b/src/Cancellation.php similarity index 79% rename from src/CancellationToken.php rename to src/Cancellation.php index d38cf3f..d2f5521 100644 --- a/src/CancellationToken.php +++ b/src/Cancellation.php @@ -3,15 +3,15 @@ namespace Amp; /** - * Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests. + * Cancellations are simple objects that allow registering handlers to subscribe to cancellation requests. */ -interface CancellationToken +interface Cancellation { /** * Subscribes a new handler to be invoked on a cancellation request. * - * This handler might be invoked immediately in case the token has already been cancelled. Any unhandled exceptions - * will be thrown into the event loop. + * This handler might be invoked immediately in case the cancellation has already been requested. Any unhandled + * exceptions will be thrown into the event loop. * * @param \Closure(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a * `CancelledException` as first argument that may be used to fail the operation. diff --git a/src/CancelledException.php b/src/CancelledException.php index 7cc1da4..c5b5251 100644 --- a/src/CancelledException.php +++ b/src/CancelledException.php @@ -5,8 +5,8 @@ namespace Amp; /** * Will be thrown in case an operation is cancelled. * - * @see CancellationToken - * @see CancellationTokenSource + * @see Cancellation + * @see DeferredCancellation */ class CancelledException extends \Exception { diff --git a/src/CombinedCancellationToken.php b/src/CompositeCancellation.php similarity index 62% rename from src/CombinedCancellationToken.php rename to src/CompositeCancellation.php index 1f0f4cd..0934860 100644 --- a/src/CombinedCancellationToken.php +++ b/src/CompositeCancellation.php @@ -4,40 +4,39 @@ namespace Amp; use Revolt\EventLoop; -final class CombinedCancellationToken implements CancellationToken +final class CompositeCancellation implements Cancellation { - /** @var array */ - private array $tokens = []; + /** @var array */ + private array $cancellations = []; private string $nextId = "a"; - /** @var callable(CancelledException)[] */ + /** @var \Closure(CancelledException)[] */ private array $callbacks = []; private ?CancelledException $exception = null; - public function __construct(CancellationToken ...$tokens) + public function __construct(Cancellation ...$cancellations) { - foreach ($tokens as $token) { - $id = $token->subscribe(function (CancelledException $exception): void { + foreach ($cancellations as $cancellation) { + $id = $cancellation->subscribe(function (CancelledException $exception): void { $this->exception = $exception; - $callbacks = $this->callbacks; - $this->callbacks = []; - - foreach ($callbacks as $callback) { + foreach ($this->callbacks as $callback) { EventLoop::queue($callback, $exception); } + + $this->callbacks = []; }); - $this->tokens[] = [$token, $id]; + $this->cancellations[] = [$cancellation, $id]; } } public function __destruct() { - foreach ($this->tokens as [$token, $id]) { - /** @var CancellationToken $token */ + foreach ($this->cancellations as [$token, $id]) { + /** @var Cancellation $token */ $token->unsubscribe($id); } } @@ -64,7 +63,7 @@ final class CombinedCancellationToken implements CancellationToken /** @inheritdoc */ public function isRequested(): bool { - foreach ($this->tokens as [$token]) { + foreach ($this->cancellations as [$token]) { if ($token->isRequested()) { return true; } @@ -76,7 +75,7 @@ final class CombinedCancellationToken implements CancellationToken /** @inheritdoc */ public function throwIfRequested(): void { - foreach ($this->tokens as [$token]) { + foreach ($this->cancellations as [$token]) { $token->throwIfRequested(); } } diff --git a/src/CancellationTokenSource.php b/src/DeferredCancellation.php similarity index 57% rename from src/CancellationTokenSource.php rename to src/DeferredCancellation.php index 9027ef7..dac3847 100644 --- a/src/CancellationTokenSource.php +++ b/src/DeferredCancellation.php @@ -3,10 +3,10 @@ namespace Amp; /** - * A cancellation token source provides a mechanism to cancel operations. + * A deferred cancellation provides a mechanism to cancel operations dynamically. * - * Cancellation of operation works by creating a cancellation token source and passing the corresponding token when - * starting the operation. To cancel the operation, invoke `CancellationTokenSource::cancel()`. + * Cancellation of operation works by creating a deferred cancellation and passing the corresponding cancellation when + * starting the operation. To cancel the operation, invoke `DeferredCancellation::cancel()`. * * Any operation can decide what to do on a cancellation request, it has "don't care" semantics. An operation SHOULD be * aborted, but MAY continue. Example: A DNS client might continue to receive and cache the response, as the query has @@ -16,37 +16,37 @@ namespace Amp; * **Example** * * ```php - * $cancellationTokenSource = new CancellationTokenSource; - * $cancellationToken = $cancellationTokenSource->getToken(); + * $deferredCancellation = new DeferredCancellation; + * $cancellation = $deferredCancellation->getCancellation(); * - * $response = $httpClient->request("https://example.com/pipeline", $cancellationToken); + * $response = $httpClient->request("https://example.com/pipeline", $cancellation); * $responseBody = $response->getBody(); * * while (null !== $chunk = $response->read()) { * // consume $chunk * * if ($noLongerInterested) { - * $cancellationTokenSource->cancel(); + * $deferredCancellation->cancel(); * break; * } * } * ``` * - * @see CancellationToken + * @see Cancellation * @see CancelledException */ -final class CancellationTokenSource +final class DeferredCancellation { - private Internal\CancellableToken $source; - private CancellationToken $token; + private Internal\Cancellable $source; + private Cancellation $token; public function __construct() { - $this->source = new Internal\CancellableToken; - $this->token = new Internal\WrappedCancellationToken($this->source); + $this->source = new Internal\Cancellable; + $this->token = new Internal\WrappedCancellation($this->source); } - public function getToken(): CancellationToken + public function getCancellation(): Cancellation { return $this->token; } diff --git a/src/Future.php b/src/Future.php index a1264c8..5082a25 100644 --- a/src/Future.php +++ b/src/Future.php @@ -18,13 +18,13 @@ final class Future * @template Tv * * @param iterable> $futures - * @param CancellationToken|null $token Optional cancellation token. + * @param Cancellation|null $cancellation Optional cancellation token. * * @return iterable> */ - public static function iterate(iterable $futures, ?CancellationToken $token = null): iterable + public static function iterate(iterable $futures, ?Cancellation $cancellation = null): iterable { - $iterator = new FutureIterator($token); + $iterator = new FutureIterator($cancellation); // Directly iterate in case of an array, because there can't be suspensions during iteration if (\is_array($futures)) { @@ -61,24 +61,24 @@ final class Future /** * @template Tv * - * @param Tv $result + * @param Tv $value * * @return Future */ - public static function complete(mixed $result = null): self + public static function complete(mixed $value = null): self { $state = new FutureState(); - $state->complete($result); + $state->complete($value); return new self($state); } /** - * @return Future + * @return Future */ public static function error(\Throwable $throwable): self { - /** @var FutureState $state */ + /** @var FutureState $state */ $state = new FutureState(); $state->error($throwable); @@ -211,7 +211,7 @@ final class Future * * @return T */ - public function await(?CancellationToken $cancellation = null): mixed + public function await(?Cancellation $cancellation = null): mixed { $suspension = EventLoop::createSuspension(); diff --git a/src/Future/functions.php b/src/Future/functions.php index bcdc26a..62bf723 100644 --- a/src/Future/functions.php +++ b/src/Future/functions.php @@ -2,7 +2,7 @@ namespace Amp\Future; -use Amp\CancellationToken; +use Amp\Cancellation; use Amp\CompositeException; use Amp\Future; @@ -13,14 +13,14 @@ use Amp\Future; * * @template T * - * @param iterable> $futures - * @param CancellationToken|null $cancellation Optional cancellation token. + * @param iterable> $futures + * @param Cancellation|null $cancellation Optional cancellation token. * * @return T * * @throws \Error If $futures is empty. */ -function race(iterable $futures, ?CancellationToken $cancellation = null): mixed +function race(iterable $futures, ?Cancellation $cancellation = null): mixed { foreach (Future::iterate($futures, $cancellation) as $first) { return $first->await(); @@ -38,13 +38,13 @@ function race(iterable $futures, ?CancellationToken $cancellation = null): mixed * @template Tv * * @param iterable> $futures - * @param CancellationToken|null $cancellation Optional cancellation token. + * @param Cancellation|null $cancellation Optional cancellation token. * * @return Tv * * @throws CompositeException If all futures errored. */ -function any(iterable $futures, ?CancellationToken $cancellation = null): mixed +function any(iterable $futures, ?Cancellation $cancellation = null): mixed { $result = some($futures, 1, $cancellation); return $result[\array_key_first($result)]; @@ -55,13 +55,13 @@ function any(iterable $futures, ?CancellationToken $cancellation = null): mixed * @template Tv * * @param iterable> $futures - * @param CancellationToken|null $cancellation Optional cancellation token. + * @param Cancellation|null $cancellation Optional cancellation token. * * @return non-empty-array * * @throws CompositeException If all futures errored. */ -function some(iterable $futures, int $count, ?CancellationToken $cancellation = null): array +function some(iterable $futures, int $count, ?Cancellation $cancellation = null): array { if ($count <= 0) { throw new \ValueError('The count must be greater than 0, got ' . $count); @@ -96,11 +96,11 @@ function some(iterable $futures, int $count, ?CancellationToken $cancellation = * @template Tv * * @param iterable> $futures - * @param CancellationToken|null $cancellation Optional cancellation token. + * @param Cancellation|null $cancellation Optional cancellation token. * * @return array{array, array} */ -function settle(iterable $futures, ?CancellationToken $cancellation = null): array +function settle(iterable $futures, ?Cancellation $cancellation = null): array { $values = []; $errors = []; @@ -124,11 +124,11 @@ function settle(iterable $futures, ?CancellationToken $cancellation = null): arr * @template Tv * * @param iterable> $futures - * @param CancellationToken|null $cancellation Optional cancellation token. + * @param Cancellation|null $cancellation Optional cancellation token. * * @return array Unwrapped values with the order preserved. */ -function all(iterable $futures, CancellationToken $cancellation = null): array +function all(iterable $futures, Cancellation $cancellation = null): array { $values = []; diff --git a/src/Internal/CancellableToken.php b/src/Internal/Cancellable.php similarity index 94% rename from src/Internal/CancellableToken.php rename to src/Internal/Cancellable.php index 8a38996..9eb92fb 100644 --- a/src/Internal/CancellableToken.php +++ b/src/Internal/Cancellable.php @@ -2,7 +2,7 @@ namespace Amp\Internal; -use Amp\CancellationToken; +use Amp\Cancellation; use Amp\CancelledException; use Revolt\EventLoop; @@ -11,7 +11,7 @@ use Revolt\EventLoop; * * @internal */ -final class CancellableToken implements CancellationToken +final class Cancellable implements Cancellation { private string $nextId = "a"; diff --git a/src/Internal/FutureIterator.php b/src/Internal/FutureIterator.php index d0c84be..80ad3db 100644 --- a/src/Internal/FutureIterator.php +++ b/src/Internal/FutureIterator.php @@ -2,9 +2,9 @@ namespace Amp\Internal; -use Amp\CancellationToken; +use Amp\Cancellation; use Amp\Future; -use Amp\NullCancellationToken; +use Amp\NullCancellation; use Revolt\EventLoop; /** @@ -20,7 +20,7 @@ final class FutureIterator */ private FutureIteratorQueue $queue; - private CancellationToken $token; + private Cancellation $token; private string $cancellationId; @@ -29,10 +29,10 @@ final class FutureIterator */ private ?Future $complete = null; - public function __construct(?CancellationToken $token = null) + public function __construct(?Cancellation $token = null) { $this->queue = $queue = new FutureIteratorQueue(); - $this->token = $token ?? new NullCancellationToken(); + $this->token = $token ?? new NullCancellation(); $this->cancellationId = $this->token->subscribe(static function (\Throwable $reason) use ($queue): void { if ($queue->suspension) { diff --git a/src/Internal/WrappedCancellationToken.php b/src/Internal/WrappedCancellation.php similarity index 79% rename from src/Internal/WrappedCancellationToken.php rename to src/Internal/WrappedCancellation.php index 66f9a99..063d2a7 100644 --- a/src/Internal/WrappedCancellationToken.php +++ b/src/Internal/WrappedCancellation.php @@ -2,15 +2,15 @@ namespace Amp\Internal; -use Amp\CancellationToken; +use Amp\Cancellation; /** * @internal */ -final class WrappedCancellationToken implements CancellationToken +final class WrappedCancellation implements Cancellation { public function __construct( - private CancellationToken $token + private Cancellation $token ) { } diff --git a/src/NullCancellationToken.php b/src/NullCancellation.php similarity index 93% rename from src/NullCancellationToken.php rename to src/NullCancellation.php index 94114c7..3d9c597 100644 --- a/src/NullCancellationToken.php +++ b/src/NullCancellation.php @@ -25,7 +25,7 @@ namespace Amp; * * instead. */ -final class NullCancellationToken implements CancellationToken +final class NullCancellation implements Cancellation { public function subscribe(\Closure $callback): string { diff --git a/src/TimeoutCancellationToken.php b/src/TimeoutCancellation.php similarity index 91% rename from src/TimeoutCancellationToken.php rename to src/TimeoutCancellation.php index 44df5a6..361fbfc 100644 --- a/src/TimeoutCancellationToken.php +++ b/src/TimeoutCancellation.php @@ -7,11 +7,11 @@ use Revolt\EventLoop; /** * A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed. */ -final class TimeoutCancellationToken implements CancellationToken +final class TimeoutCancellation implements Cancellation { private string $watcher; - private CancellationToken $token; + private Cancellation $token; /** * @param float $timeout Seconds until cancellation is requested. @@ -19,7 +19,7 @@ final class TimeoutCancellationToken implements CancellationToken */ public function __construct(float $timeout, string $message = "Operation timed out") { - $this->token = $source = new Internal\CancellableToken; + $this->token = $source = new Internal\Cancellable; $trace = null; // Defined in case assertions are disabled. \assert((bool) ($trace = \debug_backtrace(0))); diff --git a/src/TimeoutException.php b/src/TimeoutException.php index dc7fba9..d2a7230 100644 --- a/src/TimeoutException.php +++ b/src/TimeoutException.php @@ -5,7 +5,7 @@ namespace Amp; /** * Thrown if a promise doesn't resolve within a specified timeout. * - * @see \Amp\Promise\timeout() + * @see TimeoutCancellation */ class TimeoutException extends \Exception { diff --git a/src/functions.php b/src/functions.php index c2cfe72..c15fb0a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -48,66 +48,70 @@ function now(): float /** * Non-blocking sleep for the specified number of seconds. * - * @param float $timeout Number of seconds to wait. - * @param bool $reference If false, unreference the underlying watcher. - * @param CancellationToken|null $token Cancel waiting if cancellation is requested. + * @param float $timeout Number of seconds to wait. + * @param bool $reference If false, unreference the underlying watcher. + * @param Cancellation|null $cancellation Cancel waiting if cancellation is requested. */ -function delay(float $timeout, bool $reference = true, ?CancellationToken $token = null): void +function delay(float $timeout, bool $reference = true, ?Cancellation $cancellation = null): void { $suspension = EventLoop::createSuspension(); - $watcher = EventLoop::delay($timeout, static fn () => $suspension->resume(null)); - $id = $token?->subscribe(static fn (CancelledException $exception) => $suspension->throw($exception)); + $callbackId = EventLoop::delay($timeout, static fn () => $suspension->resume()); + $cancellationId = $cancellation?->subscribe( + static fn (CancelledException $exception) => $suspension->throw($exception) + ); if (!$reference) { - EventLoop::unreference($watcher); + EventLoop::unreference($callbackId); } try { $suspension->suspend(); } finally { - /** @psalm-suppress PossiblyNullArgument $id will not be null if $token is not null. */ - $token?->unsubscribe($id); - EventLoop::cancel($watcher); + EventLoop::cancel($callbackId); + + /** @psalm-suppress PossiblyNullArgument $cancellationId will not be null if $token is not null. */ + $cancellation?->unsubscribe($cancellationId); } } /** * Wait for signal(s) in a non-blocking way. * - * @param int|array $signals Signal number or array of signal numbers. - * @param bool $reference If false, unreference the underlying watcher. - * @param CancellationToken|null $token Cancel waiting if cancellation is requested. + * @param int|array $signals Signal number or array of signal numbers. + * @param bool $reference If false, unreference the underlying watcher. + * @param Cancellation|null $cancellation Cancel waiting if cancellation is requested. * * @return int Caught signal number. * @throws UnsupportedFeatureException */ -function trapSignal(int|array $signals, bool $reference = true, ?CancellationToken $token = null): int +function trapSignal(int|array $signals, bool $reference = true, ?Cancellation $cancellation = null): int { $suspension = EventLoop::createSuspension(); - $callback = static fn (string $watcher, int $signo) => $suspension->resume($signo); - $id = $token?->subscribe(static fn (CancelledException $exception) => $suspension->throw($exception)); + $callback = static fn (string $watcher, int $signal) => $suspension->resume($signal); + $id = $cancellation?->subscribe(static fn (CancelledException $exception) => $suspension->throw($exception)); - $watchers = []; + $callbackIds = []; if (\is_int($signals)) { $signals = [$signals]; } foreach ($signals as $signo) { - $watchers[] = $watcher = EventLoop::onSignal($signo, $callback); + $callbackIds[] = $callbackId = EventLoop::onSignal($signo, $callback); if (!$reference) { - EventLoop::unreference($watcher); + EventLoop::unreference($callbackId); } } try { return $suspension->suspend(); } finally { - /** @psalm-suppress PossiblyNullArgument $id will not be null if $token is not null. */ - $token?->unsubscribe($id); - foreach ($watchers as $watcher) { - EventLoop::cancel($watcher); + foreach ($callbackIds as $callbackId) { + EventLoop::cancel($callbackId); } + + /** @psalm-suppress PossiblyNullArgument $id will not be null if $token is not null. */ + $cancellation?->unsubscribe($id); } } diff --git a/test/CancellationToken/CancellationTest.php b/test/Cancellation/CancellationTest.php similarity index 55% rename from test/CancellationToken/CancellationTest.php rename to test/Cancellation/CancellationTest.php index 4a2ff31..e85a899 100644 --- a/test/CancellationToken/CancellationTest.php +++ b/test/Cancellation/CancellationTest.php @@ -1,8 +1,8 @@ getToken()->subscribe(function () { + $id = $deferredCancellation->getCancellation()->subscribe(function () { $this->fail("Callback has been called"); }); - $cancellationSource->getToken()->subscribe(function () { + $deferredCancellation->getCancellation()->subscribe(function () { $this->assertTrue(true); }); - $cancellationSource->getToken()->unsubscribe($id); + $deferredCancellation->getCancellation()->unsubscribe($id); - $cancellationSource->cancel(); + $deferredCancellation->cancel(); } public function testThrowingCallbacksEndUpInLoop(): void @@ -33,8 +33,8 @@ class CancellationTest extends AsyncTestCase $reason = $exception; }); - $cancellationSource = new CancellationTokenSource; - $cancellationSource->getToken()->subscribe(function () { + $cancellationSource = new DeferredCancellation; + $cancellationSource->getCancellation()->subscribe(function () { throw new TestException; }); @@ -47,8 +47,8 @@ class CancellationTest extends AsyncTestCase public function testDoubleCancelOnlyInvokesOnce(): void { - $cancellationSource = new CancellationTokenSource; - $cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1))); + $cancellationSource = new DeferredCancellation; + $cancellationSource->getCancellation()->subscribe(\Closure::fromCallable($this->createCallback(1))); $cancellationSource->cancel(); $cancellationSource->cancel(); @@ -56,8 +56,8 @@ class CancellationTest extends AsyncTestCase public function testCalledIfSubscribingAfterCancel(): void { - $cancellationSource = new CancellationTokenSource; + $cancellationSource = new DeferredCancellation; $cancellationSource->cancel(); - $cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1))); + $cancellationSource->getCancellation()->subscribe(\Closure::fromCallable($this->createCallback(1))); } } diff --git a/test/CancellationToken/TimeoutCancellationTokenTest.php b/test/Cancellation/TimeoutCancellationTest.php similarity index 78% rename from test/CancellationToken/TimeoutCancellationTokenTest.php rename to test/Cancellation/TimeoutCancellationTest.php index 814f49b..143ebb1 100644 --- a/test/CancellationToken/TimeoutCancellationTokenTest.php +++ b/test/Cancellation/TimeoutCancellationTest.php @@ -1,20 +1,20 @@ isRequested()); delay(0.02); @@ -28,8 +28,8 @@ class TimeoutCancellationTokenTest extends AsyncTestCase $message = $exception->getPrevious()->getMessage(); if ((int) \ini_get('zend.assertions') > 0) { - self::assertStringContainsString('TimeoutCancellationToken was created here', $message); - self::assertStringContainsString('TimeoutCancellationTokenTest.php:' . $line, $message); + self::assertStringContainsString('TimeoutCancellation was created here', $message); + self::assertStringContainsString('TimeoutCancellationTest.php:' . $line, $message); } } } @@ -37,7 +37,7 @@ class TimeoutCancellationTokenTest extends AsyncTestCase public function testWatcherCancellation(): void { $enabled = EventLoop::getInfo()["delay"]["enabled"]; - $token = new TimeoutCancellationToken(0.001); + $token = new TimeoutCancellation(0.001); self::assertSame($enabled + 1, EventLoop::getInfo()["delay"]["enabled"]); unset($token); self::assertSame($enabled, EventLoop::getInfo()["delay"]["enabled"]); diff --git a/test/Future/AllTest.php b/test/Future/AllTest.php index 5d9e455..8de8bf6 100644 --- a/test/Future/AllTest.php +++ b/test/Future/AllTest.php @@ -5,7 +5,7 @@ namespace Amp\Future; use Amp\CancelledException; use Amp\Deferred; use Amp\Future; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use PHPUnit\Framework\TestCase; use Revolt\EventLoop; @@ -84,7 +84,7 @@ class AllTest extends TestCase all(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.2)); + ), new TimeoutCancellation(0.2)); } public function testCompleteBeforeCancellation(): void @@ -98,6 +98,6 @@ class AllTest extends TestCase self::assertSame([1, 2, 3], all(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.5))); + ), new TimeoutCancellation(0.5))); } } diff --git a/test/Future/AnyTest.php b/test/Future/AnyTest.php index d5dbe90..c7179b8 100644 --- a/test/Future/AnyTest.php +++ b/test/Future/AnyTest.php @@ -6,7 +6,7 @@ use Amp\CancelledException; use Amp\CompositeException; use Amp\Deferred; use Amp\Future; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use PHPUnit\Framework\TestCase; use Revolt\EventLoop; @@ -62,7 +62,7 @@ class AnyTest extends TestCase any(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.05)); + ), new TimeoutCancellation(0.05)); } public function testCompleteBeforeCancellation(): void @@ -81,6 +81,6 @@ class AnyTest extends TestCase self::assertSame(1, any(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.2))); + ), new TimeoutCancellation(0.2))); } } diff --git a/test/Future/FutureTest.php b/test/Future/FutureTest.php index 692baab..1466b97 100644 --- a/test/Future/FutureTest.php +++ b/test/Future/FutureTest.php @@ -2,14 +2,14 @@ namespace Amp\Future; -use Amp\CancellationTokenSource; use Amp\CancelledException; use Amp\Deferred; +use Amp\DeferredCancellation; use Amp\Future; use Amp\PHPUnit\AsyncTestCase; use Amp\PHPUnit\LoopCaughtException; use Amp\PHPUnit\TestException; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use Revolt\EventLoop; use function Amp\async; use function Amp\delay; @@ -127,7 +127,7 @@ class FutureTest extends AsyncTestCase { $future = $this->delay(0.02, true); - $token = new TimeoutCancellationToken(0.01); + $token = new TimeoutCancellation(0.01); $this->expectException(CancelledException::class); @@ -138,7 +138,7 @@ class FutureTest extends AsyncTestCase { $future = $this->delay(0.01, true); - $token = new TimeoutCancellationToken(0.02); + $token = new TimeoutCancellation(0.02); self::assertTrue($future->await($token)); } @@ -146,11 +146,11 @@ class FutureTest extends AsyncTestCase public function testCompleteThenCancelJoin(): void { $deferred = new Deferred; - $source = new CancellationTokenSource; + $source = new DeferredCancellation; $future = $deferred->getFuture(); EventLoop::queue(function () use ($future, $source): void { - self::assertSame(1, $future->await($source->getToken())); + self::assertSame(1, $future->await($source->getCancellation())); }); $deferred->complete(1); @@ -319,7 +319,7 @@ class FutureTest extends AsyncTestCase * @template T * * @param float $seconds - * @param T $value + * @param T $value * * @return Future */ diff --git a/test/Future/RaceTest.php b/test/Future/RaceTest.php index 5282b4c..053ad26 100644 --- a/test/Future/RaceTest.php +++ b/test/Future/RaceTest.php @@ -5,7 +5,7 @@ namespace Amp\Future; use Amp\CancelledException; use Amp\Deferred; use Amp\Future; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use PHPUnit\Framework\TestCase; use Revolt\EventLoop; @@ -60,7 +60,7 @@ class RaceTest extends TestCase race(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.05)); + ), new TimeoutCancellation(0.05)); } public function testCompleteBeforeCancellation(): void @@ -74,6 +74,6 @@ class RaceTest extends TestCase self::assertSame(1, race(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.2))); + ), new TimeoutCancellation(0.2))); } } diff --git a/test/Future/SettleTest.php b/test/Future/SettleTest.php index 54661eb..67f1220 100644 --- a/test/Future/SettleTest.php +++ b/test/Future/SettleTest.php @@ -5,7 +5,7 @@ namespace Amp\Future; use Amp\CancelledException; use Amp\Deferred; use Amp\Future; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use PHPUnit\Framework\TestCase; use Revolt\EventLoop; @@ -58,7 +58,7 @@ class SettleTest extends TestCase settle(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.05)); + ), new TimeoutCancellation(0.05)); } public function testCompleteBeforeCancellation(): void @@ -72,6 +72,6 @@ class SettleTest extends TestCase self::assertSame([[], \range(1, 3)], settle(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), new TimeoutCancellationToken(0.5))); + ), new TimeoutCancellation(0.5))); } } diff --git a/test/Future/SomeTest.php b/test/Future/SomeTest.php index 1ac2f1e..8dfc684 100644 --- a/test/Future/SomeTest.php +++ b/test/Future/SomeTest.php @@ -6,7 +6,7 @@ use Amp\CancelledException; use Amp\CompositeException; use Amp\Deferred; use Amp\Future; -use Amp\TimeoutCancellationToken; +use Amp\TimeoutCancellation; use PHPUnit\Framework\TestCase; use Revolt\EventLoop; @@ -31,7 +31,8 @@ class SomeTest extends TestCase public function testTwoFirstThrowing(): void { - self::assertSame(['two' => 2], some(['one' => Future::error(new \Exception('foo')), 'two' => Future::complete(2)], 1)); + self::assertSame(['two' => 2], + some(['one' => Future::error(new \Exception('foo')), 'two' => Future::complete(2)], 1)); } public function testTwoBothThrowing(): void @@ -62,7 +63,7 @@ class SomeTest extends TestCase some(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), 3, new TimeoutCancellationToken(0.05)); + ), 3, new TimeoutCancellation(0.05)); } public function testCompleteBeforeCancellation(): void @@ -76,7 +77,7 @@ class SomeTest extends TestCase self::assertSame(\range(1, 3), some(\array_map( fn (Deferred $deferred) => $deferred->getFuture(), $deferreds - ), 3, new TimeoutCancellationToken(0.5))); + ), 3, new TimeoutCancellation(0.5))); } public function testZero(): void