Skip to content

Commit

Permalink
Updated exception message and added a testcase to check the message.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLevti committed May 25, 2018
1 parent 7e15db9 commit 9fb6c3a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions classes/mutex/SpinlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ protected function unlock()
$elapsed = microtime(true) - $this->acquired;
if ($elapsed >= $this->timeout) {
$message = sprintf(
"The code executed for %.2f seconds. But the timeout is %d " .
"seconds. The last %.2f seconds were executed outside the lock.",
"The code executed for %.2F seconds. But the timeout is %d " .
"seconds. The last %.2F seconds were executed outside the lock.",
$elapsed,
$this->timeout,
$elapsed - $this->timeout
Expand Down
10 changes: 8 additions & 2 deletions tests/mutex/SpinlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -68,14 +69,19 @@ public function testAcquireTimesOut()
* Tests executing code which exceeds the timeout fails.
*
* @test
* @expectedException malkusch\lock\exception\ExecutionOutsideLockException
*/
public function testExecuteTooLong()
{
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ["test", 1]);
$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);
});
Expand Down

0 comments on commit 9fb6c3a

Please sign in to comment.