Skip to content

Commit df19397

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #20 from TheLevti/feature/execution-outside-lock-exception
#19: Added a new exception
2 parents 5522f44 + 9fb6c3a commit df19397

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace malkusch\lock\exception;
4+
5+
/**
6+
* This exception should be thrown when for example the lock is released or
7+
* times out before the synchronized code finished execution.
8+
*
9+
* @see \malkusch\lock\mutex\SpinlockMutex::unlock()
10+
*
11+
* @author Petr Levtonov <[email protected]>
12+
* @license WTFPL
13+
*/
14+
class ExecutionOutsideLockException extends LockReleaseException
15+
{
16+
}

classes/mutex/SpinlockMutex.php

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

33
namespace malkusch\lock\mutex;
44

5-
use malkusch\lock\util\Loop;
6-
use malkusch\lock\exception\LockReleaseException;
5+
use malkusch\lock\exception\ExecutionOutsideLockException;
76
use malkusch\lock\exception\LockAcquireException;
7+
use malkusch\lock\exception\LockReleaseException;
8+
use malkusch\lock\util\Loop;
89

910
/**
1011
* Spinlock implementation.
@@ -79,11 +80,13 @@ protected function unlock()
7980
$elapsed = microtime(true) - $this->acquired;
8081
if ($elapsed >= $this->timeout) {
8182
$message = sprintf(
82-
"The code executed for %d seconds. But the timeout is %d seconds.",
83+
"The code executed for %.2F seconds. But the timeout is %d " .
84+
"seconds. The last %.2F seconds were executed outside the lock.",
8385
$elapsed,
84-
$this->timeout
86+
$this->timeout,
87+
$elapsed - $this->timeout
8588
);
86-
throw new LockReleaseException($message);
89+
throw new ExecutionOutsideLockException($message);
8790
}
8891

8992
/*

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\LockReleaseException
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)