Skip to content

Commit

Permalink
minor PcntlTimeout improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 15, 2024
1 parent 2ab079f commit fde7289
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/Util/PcntlTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand All @@ -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);
}
}
}

Expand Down
13 changes: 6 additions & 7 deletions tests/Util/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -102,20 +101,20 @@ 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);
}

public function testExceptionStopsIteration(): void
{
$this->expectException(\DomainException::class);

$loop = new Loop();

$this->expectException(\DomainException::class);
$loop->execute(static function () {
throw new \DomainException();
}, 1);
Expand Down
6 changes: 2 additions & 4 deletions tests/Util/PcntlTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit fde7289

Please sign in to comment.