Commit ac24eca7 authored by Willi Wehmeier's avatar Willi Wehmeier

Merge branch...

Merge branch '6-acquire-non-blocking-lock-should-thrown-lockacquirewouldblockexception-if-lock-could-not-be-acquired' into 'master'

[FEATURE] Acquire a non-blocking lock should throw LockAcquireWouldBlockException

Closes #6

See merge request !1
parents 31d29739 ea703258
......@@ -68,11 +68,20 @@ class MutexAdapterStrategy implements LockingStrategyInterface
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
{
$timeout = null;
if ($mode & static::LOCK_CAPABILITY_NOBLOCK) {
$isNonBlockingMode = $mode & static::LOCK_CAPABILITY_NOBLOCK;
if ($isNonBlockingMode) {
$timeout = 0;
}
return $this->mutex->acquireLock($timeout);
$isAcquired = $this->mutex->acquireLock($timeout);
if ($isNonBlockingMode && ! $isAcquired) {
throw new LockAcquireWouldBlockException(
'Failed to acquire lock because the request would block.',
1428700748
);
}
return $isAcquired;
}
/**
......
......@@ -98,6 +98,25 @@ class MutexAdapterStrategyTest extends UnitTestCase
$this->assertTrue($sut->acquire($mode));
}
/**
* @test
* @expectedException \TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
* @expectedExceptionMessage Failed to acquire lock because the request would block.
* @expectedExceptionCode 1428700748
*/
public function itThrowsALockAcquireWouldBlockExceptionIfNonBlockingLockWhouldBlock()
{
$mode = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK;
$mutex = $this->prophesize(Mutex::class);
$mutex
->acquireLock(0)
->shouldBeCalled()
->willReturn(false);
$sut = new MutexAdapterStrategy($mutex->reveal());
$sut->acquire($mode);
}
/**
* @test
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment