mirror of
https://github.com/danog/amp.git
synced 2024-12-11 17:09:40 +01:00
Rename CancellationToken to Cancellation
This commit is contained in:
parent
5bd21bed06
commit
112e813914
@ -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.
|
@ -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
|
||||
{
|
||||
|
@ -4,40 +4,39 @@ namespace Amp;
|
||||
|
||||
use Revolt\EventLoop;
|
||||
|
||||
final class CombinedCancellationToken implements CancellationToken
|
||||
final class CompositeCancellation implements Cancellation
|
||||
{
|
||||
/** @var array<int, array{CancellationToken, string}> */
|
||||
private array $tokens = [];
|
||||
/** @var array<int, array{Cancellation, string}> */
|
||||
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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -18,13 +18,13 @@ final class Future
|
||||
* @template Tv
|
||||
*
|
||||
* @param iterable<Tk, Future<Tv>> $futures
|
||||
* @param CancellationToken|null $token Optional cancellation token.
|
||||
* @param Cancellation|null $cancellation Optional cancellation token.
|
||||
*
|
||||
* @return iterable<Tk, Future<Tv>>
|
||||
*/
|
||||
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<Tv>
|
||||
*/
|
||||
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<never-return>
|
||||
* @return Future<never>
|
||||
*/
|
||||
public static function error(\Throwable $throwable): self
|
||||
{
|
||||
/** @var FutureState<never-return> $state */
|
||||
/** @var FutureState<never> $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();
|
||||
|
||||
|
@ -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<Future<T>> $futures
|
||||
* @param CancellationToken|null $cancellation Optional cancellation token.
|
||||
* @param iterable<Future<T>> $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<Tk, Future<Tv>> $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<Tk, Future<Tv>> $futures
|
||||
* @param CancellationToken|null $cancellation Optional cancellation token.
|
||||
* @param Cancellation|null $cancellation Optional cancellation token.
|
||||
*
|
||||
* @return non-empty-array<Tk, Tv>
|
||||
*
|
||||
* @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<Tk, Future<Tv>> $futures
|
||||
* @param CancellationToken|null $cancellation Optional cancellation token.
|
||||
* @param Cancellation|null $cancellation Optional cancellation token.
|
||||
*
|
||||
* @return array{array<Tk, \Throwable>, array<Tk, Tv>}
|
||||
*/
|
||||
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<Tk, Future<Tv>> $futures
|
||||
* @param CancellationToken|null $cancellation Optional cancellation token.
|
||||
* @param Cancellation|null $cancellation Optional cancellation token.
|
||||
*
|
||||
* @return array<Tk, Tv> Unwrapped values with the order preserved.
|
||||
*/
|
||||
function all(iterable $futures, CancellationToken $cancellation = null): array
|
||||
function all(iterable $futures, Cancellation $cancellation = null): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
) {
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ namespace Amp;
|
||||
*
|
||||
* instead.
|
||||
*/
|
||||
final class NullCancellationToken implements CancellationToken
|
||||
final class NullCancellation implements Cancellation
|
||||
{
|
||||
public function subscribe(\Closure $callback): string
|
||||
{
|
@ -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)));
|
@ -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
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\CancellationToken;
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
use Amp\CancellationTokenSource;
|
||||
use Amp\DeferredCancellation;
|
||||
use Amp\PHPUnit\AsyncTestCase;
|
||||
use Amp\PHPUnit\TestException;
|
||||
use Revolt\EventLoop;
|
||||
@ -12,19 +12,19 @@ class CancellationTest extends AsyncTestCase
|
||||
{
|
||||
public function testUnsubscribeWorks(): void
|
||||
{
|
||||
$cancellationSource = new CancellationTokenSource;
|
||||
$deferredCancellation = new DeferredCancellation;
|
||||
|
||||
$id = $cancellationSource->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)));
|
||||
}
|
||||
}
|
@ -1,20 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\CancellationToken;
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
use Amp\CancelledException;
|
||||
use Amp\PHPUnit\AsyncTestCase;
|
||||
use Amp\TimeoutCancellationToken;
|
||||
use Amp\TimeoutCancellation;
|
||||
use Amp\TimeoutException;
|
||||
use Revolt\EventLoop;
|
||||
use function Amp\delay;
|
||||
|
||||
class TimeoutCancellationTokenTest extends AsyncTestCase
|
||||
class TimeoutCancellationTest extends AsyncTestCase
|
||||
{
|
||||
public function testTimeout(): void
|
||||
{
|
||||
$line = __LINE__ + 1;
|
||||
$token = new TimeoutCancellationToken(0.01);
|
||||
$token = new TimeoutCancellation(0.01);
|
||||
|
||||
self::assertFalse($token->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"]);
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
@ -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<T>
|
||||
*/
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user