Skip to content

Commit

Permalink
add float timeout support to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 2, 2024
1 parent c1af1f9 commit fbefa48
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
32 changes: 24 additions & 8 deletions src/exception/ExecutionOutsideLockException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,34 @@ class ExecutionOutsideLockException extends LockReleaseException
* Creates a new instance of the ExecutionOutsideLockException class.
*
* @param float $elapsedTime Total elapsed time of the synchronized code
* callback execution.
* @param int $timeout The lock timeout in seconds.
* callback execution.
* @param float $timeout The lock timeout in seconds.
* @return self Execution outside lock exception.
*/
public static function create(float $elapsedTime, int $timeout): self
public static function create(float $elapsedTime, float $timeout): self
{
$elapsedTimeStr = (string) round($elapsedTime, 6);
if (\is_finite($elapsedTime) && strpos($elapsedTimeStr, '.') === false) {
$elapsedTimeStr .= '.0';
}

$timeoutStr = (string) round($timeout, 6);
if (\is_finite($timeout) && strpos($timeoutStr, '.') === false) {
$timeoutStr .= '.0';
}

$overTime = round($elapsedTime, 6) - round($timeout, 6);
$overTimeStr = (string) round($overTime, 6);
if (\is_finite($timeout) && strpos($overTimeStr, '.') === false) {
$overTimeStr .= '.0';
}

return new self(\sprintf(
'The code executed for %.2F seconds. But the timeout is %d ' .
'seconds. The last %.2F seconds were executed outside of the lock.',
$elapsedTime,
$timeout,
$elapsedTime - $timeout
'The code executed for %s seconds. But the timeout is %s ' .
'seconds. The last %s seconds were executed outside of the lock.',
$elapsedTimeStr,
$timeoutStr,
$overTimeStr
));
}
}
11 changes: 8 additions & 3 deletions src/exception/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ class TimeoutException extends LockAcquireException
/**
* Creates a new instance of the TimeoutException class.
*
* @param int $timeout The timeout in seconds.
* @param float $timeout The timeout in seconds.
* @return self A timeout has been exceeded exception.
*/
public static function create(int $timeout): self
public static function create(float $timeout): self
{
return new self(\sprintf('Timeout of %d seconds exceeded.', $timeout));
$timeoutStr = (string) round($timeout, 6);
if (\is_finite($timeout) && strpos($timeoutStr, '.') === false) {
$timeoutStr .= '.0';
}

return new self(\sprintf('Timeout of %s seconds exceeded.', $timeoutStr));
}
}
2 changes: 1 addition & 1 deletion tests/mutex/FlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testCodeExecutedOutsideLockIsNotThrown(int $strategy)
public function testTimeoutOccurs(int $strategy)
{
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');

$another_resource = fopen($this->file, 'r');
flock($another_resource, LOCK_EX);
Expand Down
9 changes: 7 additions & 2 deletions tests/mutex/RedisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function () use (&$i, $available): bool {
public function testAcquireTooFewKeys($count, $available)
{
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');

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

Expand Down Expand Up @@ -164,8 +164,13 @@ function () use (&$i, $available): bool {
*/
public function testTimingOut(int $count, int $timeout, int $delay)
{
$timeoutStr = (string) round($timeout, 6);
if (strpos($timeoutStr, '.') === false) {
$timeoutStr .= '.0';
}

$this->expectException(TimeoutException::class);
$this->expectExceptionMessage("Timeout of {$timeout} seconds exceeded.");
$this->expectExceptionMessage("Timeout of {$timeoutStr} seconds exceeded.");

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

Expand Down
2 changes: 1 addition & 1 deletion tests/mutex/SpinlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testFailAcquireLock()
public function testAcquireTimesOut()
{
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 3 seconds exceeded.');
$this->expectExceptionMessage('Timeout of 3.0 seconds exceeded.');

$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
$mutex->expects($this->atLeastOnce())
Expand Down
2 changes: 1 addition & 1 deletion tests/util/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testExceedTimeoutIsAcceptableIfEndWasCalled()
public function testExceedTimeoutWithoutExplicitEnd()
{
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');

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

0 comments on commit fbefa48

Please sign in to comment.