Skip to content

Commit fde7289

Browse files
committed
minor PcntlTimeout improvements
1 parent 2ab079f commit fde7289

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/Util/PcntlTimeout.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function __construct(int $timeout)
5353
*
5454
* @return T
5555
*
56+
* @throws \Throwable
5657
* @throws DeadlineException Running the code hit the deadline
5758
* @throws LockAcquireException Installing the timeout failed
5859
*/
@@ -67,7 +68,7 @@ public function timeBoxed(callable $code)
6768
));
6869
});
6970
if (!$signal) {
70-
throw new LockAcquireException('Could not install signal');
71+
throw new LockAcquireException('Could not install signal handler');
7172
}
7273

7374
$oldAlarm = pcntl_alarm($this->timeout);
@@ -79,8 +80,11 @@ public function timeBoxed(callable $code)
7980
return $code();
8081
} finally {
8182
pcntl_alarm(0);
82-
pcntl_signal_dispatch();
83-
pcntl_signal(\SIGALRM, $existingHandler);
83+
try {
84+
pcntl_signal_dispatch();
85+
} finally {
86+
pcntl_signal(\SIGALRM, $existingHandler);
87+
}
8488
}
8589
}
8690

tests/Util/LoopTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function testInvalidAcquireTimeout(float $acquireTimeout): void
4646

4747
$this->expectException(\InvalidArgumentException::class);
4848
$this->expectExceptionMessage('The lock acquire timeout must be greater than or equal to 0.0 (' . LockUtil::getInstance()->formatTimeout($acquireTimeout) . ' was given)');
49-
5049
$loop->execute(static function () {
5150
self::fail();
5251
}, $acquireTimeout);
@@ -78,10 +77,10 @@ public function testExecutionWithinAcquireTimeout(): void
7877

7978
public function testExecutionWithinAcquireTimeoutWithoutCallingEnd(): void
8079
{
80+
$loop = new Loop();
81+
8182
$this->expectException(LockAcquireTimeoutException::class);
8283
$this->expectExceptionMessage('Lock acquire timeout of 0.5 seconds has been exceeded');
83-
84-
$loop = new Loop();
8584
$loop->execute(static function () {
8685
usleep(10 * 1000);
8786
}, 0.5);
@@ -102,20 +101,20 @@ public function testExceedAcquireTimeoutIsAcceptableIfEndWasCalled(): void
102101

103102
public function testExceedAcquireTimeoutWithoutCallingEnd(): void
104103
{
104+
$loop = new Loop();
105+
105106
$this->expectException(LockAcquireTimeoutException::class);
106107
$this->expectExceptionMessage('Lock acquire timeout of 0.5 seconds has been exceeded');
107-
108-
$loop = new Loop();
109108
$loop->execute(static function () {
110109
usleep(501 * 1000);
111110
}, 0.5);
112111
}
113112

114113
public function testExceptionStopsIteration(): void
115114
{
116-
$this->expectException(\DomainException::class);
117-
118115
$loop = new Loop();
116+
117+
$this->expectException(\DomainException::class);
119118
$loop->execute(static function () {
120119
throw new \DomainException();
121120
}, 1);

tests/Util/PcntlTimeoutTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ class PcntlTimeoutTest extends TestCase
2121
*/
2222
public function testShouldTimeout(): void
2323
{
24-
$this->expectException(DeadlineException::class);
25-
2624
$timeout = new PcntlTimeout(1);
2725

26+
$this->expectException(DeadlineException::class);
2827
$timeout->timeBoxed(static function () {
2928
sleep(2);
3029
});
@@ -49,12 +48,11 @@ public function testShouldNotTimeout(): void
4948
*/
5049
public function testShouldFailOnExistingAlarm(): void
5150
{
52-
$this->expectException(LockAcquireException::class);
53-
5451
try {
5552
pcntl_alarm(1);
5653
$timeout = new PcntlTimeout(1);
5754

55+
$this->expectException(LockAcquireException::class);
5856
$timeout->timeBoxed(static function () {
5957
sleep(1);
6058
});

0 commit comments

Comments
 (0)