Skip to content
Snippets Groups Projects
Commit ac24eca7 authored by Willi Wehmeier's avatar Willi Wehmeier
Browse files

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
Branches
Tags 1.0.0
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