diff --git a/classes/exception/ExecutionOutsideLockException.php b/classes/exception/ExecutionOutsideLockException.php new file mode 100644 index 00000000..6075b955 --- /dev/null +++ b/classes/exception/ExecutionOutsideLockException.php @@ -0,0 +1,16 @@ + + * @license WTFPL + */ +class ExecutionOutsideLockException extends LockReleaseException +{ +} diff --git a/classes/mutex/SpinlockMutex.php b/classes/mutex/SpinlockMutex.php index fd713b2f..f921687d 100644 --- a/classes/mutex/SpinlockMutex.php +++ b/classes/mutex/SpinlockMutex.php @@ -2,9 +2,10 @@ namespace malkusch\lock\mutex; -use malkusch\lock\util\Loop; -use malkusch\lock\exception\LockReleaseException; +use malkusch\lock\exception\ExecutionOutsideLockException; use malkusch\lock\exception\LockAcquireException; +use malkusch\lock\exception\LockReleaseException; +use malkusch\lock\util\Loop; /** * Spinlock implementation. @@ -79,11 +80,13 @@ protected function unlock() $elapsed = microtime(true) - $this->acquired; if ($elapsed >= $this->timeout) { $message = sprintf( - "The code executed for %d seconds. But the timeout is %d seconds.", + "The code executed for %.2F seconds. But the timeout is %d " . + "seconds. The last %.2F seconds were executed outside the lock.", $elapsed, - $this->timeout + $this->timeout, + $elapsed - $this->timeout ); - throw new LockReleaseException($message); + throw new ExecutionOutsideLockException($message); } /* diff --git a/tests/mutex/SpinlockMutexTest.php b/tests/mutex/SpinlockMutexTest.php index f1038855..3e8607cf 100644 --- a/tests/mutex/SpinlockMutexTest.php +++ b/tests/mutex/SpinlockMutexTest.php @@ -2,9 +2,10 @@ namespace malkusch\lock\mutex; +use malkusch\lock\exception\ExecutionOutsideLockException; use malkusch\lock\exception\LockAcquireException; -use phpmock\phpunit\PHPMock; use phpmock\environment\SleepEnvironmentBuilder; +use phpmock\phpunit\PHPMock; /** * Tests for SpinlockMutex. @@ -68,7 +69,6 @@ public function testAcquireTimesOut() * Tests executing code which exceeds the timeout fails. * * @test - * @expectedException malkusch\lock\exception\LockReleaseException */ public function testExecuteTooLong() { @@ -76,6 +76,12 @@ public function testExecuteTooLong() $mutex->expects($this->any())->method("acquire")->willReturn(true); $mutex->expects($this->any())->method("release")->willReturn(true); + $this->expectException(ExecutionOutsideLockException::class); + $this->expectExceptionMessageRegExp( + '/The code executed for \d+\.\d+ seconds. But the timeout is 1 ' . + 'seconds. The last \d+\.\d+ seconds were executed outside the lock./' + ); + $mutex->synchronized(function () { sleep(1); });