Skip to content

Commit

Permalink
Merge pull request #20 from TheLevti/feature/execution-outside-lock-e…
Browse files Browse the repository at this point in the history
…xception

#19: Added a new exception
  • Loading branch information
Willem Stuursma-Ruwen authored May 25, 2018
2 parents 5522f44 + 9fb6c3a commit df19397
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 16 additions & 0 deletions classes/exception/ExecutionOutsideLockException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace malkusch\lock\exception;

/**
* This exception should be thrown when for example the lock is released or
* times out before the synchronized code finished execution.
*
* @see \malkusch\lock\mutex\SpinlockMutex::unlock()
*
* @author Petr Levtonov <[email protected]>
* @license WTFPL
*/
class ExecutionOutsideLockException extends LockReleaseException
{
}
13 changes: 8 additions & 5 deletions classes/mutex/SpinlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/*
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\LockReleaseException
*/
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 df19397

Please sign in to comment.