From c6bf4c502389574b45887f789d6d69c2ae0a2524 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 25 Jul 2023 14:23:00 +0200 Subject: [PATCH 1/3] Allow referencing a TimeoutCancellation --- src/TimeoutCancellation.php | 11 +++++++---- test/Cancellation/TimeoutCancellationTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/TimeoutCancellation.php b/src/TimeoutCancellation.php index a73f737c..bc34c857 100644 --- a/src/TimeoutCancellation.php +++ b/src/TimeoutCancellation.php @@ -17,10 +17,11 @@ final class TimeoutCancellation implements Cancellation private readonly Cancellation $cancellation; /** - * @param float $timeout Seconds until cancellation is requested. - * @param string $message Message for TimeoutException. Default is "Operation timed out". + * @param float $timeout Seconds until cancellation is requested. + * @param string $message Message for TimeoutException. Default is "Operation timed out". + * @param bool $unreference Whether to unreference the timer. */ - public function __construct(float $timeout, string $message = "Operation timed out") + public function __construct(float $timeout, string $message = "Operation timed out", bool $unreference = true) { $this->cancellation = $source = new Internal\Cancellable; @@ -37,7 +38,9 @@ public function __construct(float $timeout, string $message = "Operation timed o $source->cancel(new TimeoutException($message)); }); - EventLoop::unreference($this->watcher); + if ($unreference) { + EventLoop::unreference($this->watcher); + } } /** diff --git a/test/Cancellation/TimeoutCancellationTest.php b/test/Cancellation/TimeoutCancellationTest.php index 3b8e229f..a540d77a 100644 --- a/test/Cancellation/TimeoutCancellationTest.php +++ b/test/Cancellation/TimeoutCancellationTest.php @@ -3,6 +3,7 @@ namespace Amp\Cancellation; use Amp\CancelledException; +use Amp\DeferredFuture; use Amp\TestCase; use Amp\TimeoutCancellation; use Amp\TimeoutException; @@ -42,4 +43,20 @@ public function testWatcherCancellation(): void unset($cancellation); self::assertSame($identifiers, EventLoop::getIdentifiers()); } + + public function testWatcherUnreference(): void + { + $this->expectExceptionMessageMatches("/Event loop terminated without resuming the current suspension/"); + $deferred = new DeferredFuture; + $cancellation = new TimeoutCancellation(0.001); + $deferred->getFuture()->await($cancellation); + } + + public function testWatcherNoUnreference(): void + { + $this->expectException(CancelledException::class); + $cancellation = new TimeoutCancellation(0.001, unreference: false); + $deferred = new DeferredFuture; + $deferred->getFuture()->await($cancellation); + } } From ee6e57f2e53ab8651298cdb893ecdad3ef151ffd Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 7 Aug 2023 18:56:14 +0200 Subject: [PATCH 2/3] Fix --- src/TimeoutCancellation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TimeoutCancellation.php b/src/TimeoutCancellation.php index bc34c857..21434686 100644 --- a/src/TimeoutCancellation.php +++ b/src/TimeoutCancellation.php @@ -19,9 +19,9 @@ final class TimeoutCancellation implements Cancellation /** * @param float $timeout Seconds until cancellation is requested. * @param string $message Message for TimeoutException. Default is "Operation timed out". - * @param bool $unreference Whether to unreference the timer. + * @param bool $reference Whether to reference the timer. */ - public function __construct(float $timeout, string $message = "Operation timed out", bool $unreference = true) + public function __construct(float $timeout, string $message = "Operation timed out", bool $reference = false) { $this->cancellation = $source = new Internal\Cancellable; @@ -38,7 +38,7 @@ public function __construct(float $timeout, string $message = "Operation timed o $source->cancel(new TimeoutException($message)); }); - if ($unreference) { + if (!$reference) { EventLoop::unreference($this->watcher); } } From f9d9f70435e7f37d1254401c4ee97e71d6e623c0 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 7 Aug 2023 18:59:40 +0200 Subject: [PATCH 3/3] fix --- test/Cancellation/TimeoutCancellationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Cancellation/TimeoutCancellationTest.php b/test/Cancellation/TimeoutCancellationTest.php index a540d77a..628fbe44 100644 --- a/test/Cancellation/TimeoutCancellationTest.php +++ b/test/Cancellation/TimeoutCancellationTest.php @@ -55,7 +55,7 @@ public function testWatcherUnreference(): void public function testWatcherNoUnreference(): void { $this->expectException(CancelledException::class); - $cancellation = new TimeoutCancellation(0.001, unreference: false); + $cancellation = new TimeoutCancellation(0.001, reference: true); $deferred = new DeferredFuture; $deferred->getFuture()->await($cancellation); }