diff --git a/src/Util/PcntlTimeout.php b/src/Util/PcntlTimeout.php index c066ee9..4fd1ca6 100644 --- a/src/Util/PcntlTimeout.php +++ b/src/Util/PcntlTimeout.php @@ -53,6 +53,7 @@ public function __construct(int $timeout) * * @return T * + * @throws \Throwable * @throws DeadlineException Running the code hit the deadline * @throws LockAcquireException Installing the timeout failed */ @@ -67,7 +68,7 @@ public function timeBoxed(callable $code) )); }); if (!$signal) { - throw new LockAcquireException('Could not install signal'); + throw new LockAcquireException('Could not install signal handler'); } $oldAlarm = pcntl_alarm($this->timeout); @@ -79,8 +80,11 @@ public function timeBoxed(callable $code) return $code(); } finally { pcntl_alarm(0); - pcntl_signal_dispatch(); - pcntl_signal(\SIGALRM, $existingHandler); + try { + pcntl_signal_dispatch(); + } finally { + pcntl_signal(\SIGALRM, $existingHandler); + } } } diff --git a/tests/Util/LoopTest.php b/tests/Util/LoopTest.php index afeff11..df08c90 100644 --- a/tests/Util/LoopTest.php +++ b/tests/Util/LoopTest.php @@ -46,7 +46,6 @@ public function testInvalidAcquireTimeout(float $acquireTimeout): void $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The lock acquire timeout must be greater than or equal to 0.0 (' . LockUtil::getInstance()->formatTimeout($acquireTimeout) . ' was given)'); - $loop->execute(static function () { self::fail(); }, $acquireTimeout); @@ -78,10 +77,10 @@ public function testExecutionWithinAcquireTimeout(): void public function testExecutionWithinAcquireTimeoutWithoutCallingEnd(): void { + $loop = new Loop(); + $this->expectException(LockAcquireTimeoutException::class); $this->expectExceptionMessage('Lock acquire timeout of 0.5 seconds has been exceeded'); - - $loop = new Loop(); $loop->execute(static function () { usleep(10 * 1000); }, 0.5); @@ -102,10 +101,10 @@ public function testExceedAcquireTimeoutIsAcceptableIfEndWasCalled(): void public function testExceedAcquireTimeoutWithoutCallingEnd(): void { + $loop = new Loop(); + $this->expectException(LockAcquireTimeoutException::class); $this->expectExceptionMessage('Lock acquire timeout of 0.5 seconds has been exceeded'); - - $loop = new Loop(); $loop->execute(static function () { usleep(501 * 1000); }, 0.5); @@ -113,9 +112,9 @@ public function testExceedAcquireTimeoutWithoutCallingEnd(): void public function testExceptionStopsIteration(): void { - $this->expectException(\DomainException::class); - $loop = new Loop(); + + $this->expectException(\DomainException::class); $loop->execute(static function () { throw new \DomainException(); }, 1); diff --git a/tests/Util/PcntlTimeoutTest.php b/tests/Util/PcntlTimeoutTest.php index 775c037..da6d690 100644 --- a/tests/Util/PcntlTimeoutTest.php +++ b/tests/Util/PcntlTimeoutTest.php @@ -21,10 +21,9 @@ class PcntlTimeoutTest extends TestCase */ public function testShouldTimeout(): void { - $this->expectException(DeadlineException::class); - $timeout = new PcntlTimeout(1); + $this->expectException(DeadlineException::class); $timeout->timeBoxed(static function () { sleep(2); }); @@ -49,12 +48,11 @@ public function testShouldNotTimeout(): void */ public function testShouldFailOnExistingAlarm(): void { - $this->expectException(LockAcquireException::class); - try { pcntl_alarm(1); $timeout = new PcntlTimeout(1); + $this->expectException(LockAcquireException::class); $timeout->timeBoxed(static function () { sleep(1); });