Skip to content
Snippets Groups Projects
Commit ea703258 authored by Daniel Hürtgen's avatar Daniel Hürtgen
Browse files

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

LockAcquireWouldBlockException should be thrown if non-blocking
lock would block according to the TYPO3 locking api.
parent 31d29739
1 merge request!1[FEATURE] Acquire a non-blocking lock should throw LockAcquireWouldBlockException
...@@ -68,11 +68,20 @@ class MutexAdapterStrategy implements LockingStrategyInterface ...@@ -68,11 +68,20 @@ class MutexAdapterStrategy implements LockingStrategyInterface
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
{ {
$timeout = null; $timeout = null;
if ($mode & static::LOCK_CAPABILITY_NOBLOCK) { $isNonBlockingMode = $mode & static::LOCK_CAPABILITY_NOBLOCK;
if ($isNonBlockingMode) {
$timeout = 0; $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 ...@@ -98,6 +98,25 @@ class MutexAdapterStrategyTest extends UnitTestCase
$this->assertTrue($sut->acquire($mode)); $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 * @test
*/ */
......
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