Skip to content

Commit fbefa48

Browse files
committed
add float timeout support to exceptions
1 parent c1af1f9 commit fbefa48

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

src/exception/ExecutionOutsideLockException.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,34 @@ class ExecutionOutsideLockException extends LockReleaseException
2222
* Creates a new instance of the ExecutionOutsideLockException class.
2323
*
2424
* @param float $elapsedTime Total elapsed time of the synchronized code
25-
* callback execution.
26-
* @param int $timeout The lock timeout in seconds.
25+
* callback execution.
26+
* @param float $timeout The lock timeout in seconds.
2727
* @return self Execution outside lock exception.
2828
*/
29-
public static function create(float $elapsedTime, int $timeout): self
29+
public static function create(float $elapsedTime, float $timeout): self
3030
{
31+
$elapsedTimeStr = (string) round($elapsedTime, 6);
32+
if (\is_finite($elapsedTime) && strpos($elapsedTimeStr, '.') === false) {
33+
$elapsedTimeStr .= '.0';
34+
}
35+
36+
$timeoutStr = (string) round($timeout, 6);
37+
if (\is_finite($timeout) && strpos($timeoutStr, '.') === false) {
38+
$timeoutStr .= '.0';
39+
}
40+
41+
$overTime = round($elapsedTime, 6) - round($timeout, 6);
42+
$overTimeStr = (string) round($overTime, 6);
43+
if (\is_finite($timeout) && strpos($overTimeStr, '.') === false) {
44+
$overTimeStr .= '.0';
45+
}
46+
3147
return new self(\sprintf(
32-
'The code executed for %.2F seconds. But the timeout is %d ' .
33-
'seconds. The last %.2F seconds were executed outside of the lock.',
34-
$elapsedTime,
35-
$timeout,
36-
$elapsedTime - $timeout
48+
'The code executed for %s seconds. But the timeout is %s ' .
49+
'seconds. The last %s seconds were executed outside of the lock.',
50+
$elapsedTimeStr,
51+
$timeoutStr,
52+
$overTimeStr
3753
));
3854
}
3955
}

src/exception/TimeoutException.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ class TimeoutException extends LockAcquireException
1515
/**
1616
* Creates a new instance of the TimeoutException class.
1717
*
18-
* @param int $timeout The timeout in seconds.
18+
* @param float $timeout The timeout in seconds.
1919
* @return self A timeout has been exceeded exception.
2020
*/
21-
public static function create(int $timeout): self
21+
public static function create(float $timeout): self
2222
{
23-
return new self(\sprintf('Timeout of %d seconds exceeded.', $timeout));
23+
$timeoutStr = (string) round($timeout, 6);
24+
if (\is_finite($timeout) && strpos($timeoutStr, '.') === false) {
25+
$timeoutStr .= '.0';
26+
}
27+
28+
return new self(\sprintf('Timeout of %s seconds exceeded.', $timeoutStr));
2429
}
2530
}

tests/mutex/FlockMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testCodeExecutedOutsideLockIsNotThrown(int $strategy)
5555
public function testTimeoutOccurs(int $strategy)
5656
{
5757
$this->expectException(TimeoutException::class);
58-
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
58+
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');
5959

6060
$another_resource = fopen($this->file, 'r');
6161
flock($another_resource, LOCK_EX);

tests/mutex/RedisMutexTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function () use (&$i, $available): bool {
133133
public function testAcquireTooFewKeys($count, $available)
134134
{
135135
$this->expectException(TimeoutException::class);
136-
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
136+
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');
137137

138138
$mutex = $this->buildRedisMutex($count);
139139

@@ -164,8 +164,13 @@ function () use (&$i, $available): bool {
164164
*/
165165
public function testTimingOut(int $count, int $timeout, int $delay)
166166
{
167+
$timeoutStr = (string) round($timeout, 6);
168+
if (strpos($timeoutStr, '.') === false) {
169+
$timeoutStr .= '.0';
170+
}
171+
167172
$this->expectException(TimeoutException::class);
168-
$this->expectExceptionMessage("Timeout of {$timeout} seconds exceeded.");
173+
$this->expectExceptionMessage("Timeout of {$timeoutStr} seconds exceeded.");
169174

170175
$mutex = $this->buildRedisMutex($count, $timeout);
171176

tests/mutex/SpinlockMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testFailAcquireLock()
5353
public function testAcquireTimesOut()
5454
{
5555
$this->expectException(TimeoutException::class);
56-
$this->expectExceptionMessage('Timeout of 3 seconds exceeded.');
56+
$this->expectExceptionMessage('Timeout of 3.0 seconds exceeded.');
5757

5858
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
5959
$mutex->expects($this->atLeastOnce())

tests/util/LoopTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testExceedTimeoutIsAcceptableIfEndWasCalled()
7070
public function testExceedTimeoutWithoutExplicitEnd()
7171
{
7272
$this->expectException(TimeoutException::class);
73-
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
73+
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');
7474

7575
$loop = new Loop(1);
7676
$loop->execute(function (): void {

0 commit comments

Comments
 (0)