Skip to content

Commit 9fb6c3a

Browse files
committed
Updated exception message and added a testcase to check the message.
1 parent 7e15db9 commit 9fb6c3a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

classes/mutex/SpinlockMutex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ protected function unlock()
8080
$elapsed = microtime(true) - $this->acquired;
8181
if ($elapsed >= $this->timeout) {
8282
$message = sprintf(
83-
"The code executed for %.2f seconds. But the timeout is %d " .
84-
"seconds. The last %.2f seconds were executed outside the lock.",
83+
"The code executed for %.2F seconds. But the timeout is %d " .
84+
"seconds. The last %.2F seconds were executed outside the lock.",
8585
$elapsed,
8686
$this->timeout,
8787
$elapsed - $this->timeout

tests/mutex/SpinlockMutexTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace malkusch\lock\mutex;
44

5+
use malkusch\lock\exception\ExecutionOutsideLockException;
56
use malkusch\lock\exception\LockAcquireException;
6-
use phpmock\phpunit\PHPMock;
77
use phpmock\environment\SleepEnvironmentBuilder;
8+
use phpmock\phpunit\PHPMock;
89

910
/**
1011
* Tests for SpinlockMutex.
@@ -68,14 +69,19 @@ public function testAcquireTimesOut()
6869
* Tests executing code which exceeds the timeout fails.
6970
*
7071
* @test
71-
* @expectedException malkusch\lock\exception\ExecutionOutsideLockException
7272
*/
7373
public function testExecuteTooLong()
7474
{
7575
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ["test", 1]);
7676
$mutex->expects($this->any())->method("acquire")->willReturn(true);
7777
$mutex->expects($this->any())->method("release")->willReturn(true);
7878

79+
$this->expectException(ExecutionOutsideLockException::class);
80+
$this->expectExceptionMessageRegExp(
81+
'/The code executed for \d+\.\d+ seconds. But the timeout is 1 ' .
82+
'seconds. The last \d+\.\d+ seconds were executed outside the lock./'
83+
);
84+
7985
$mutex->synchronized(function () {
8086
sleep(1);
8187
});

0 commit comments

Comments
 (0)