From ea7032583aea6ab49fddbdbfc1314b82d1095338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hu=CC=88rtgen?= <daniel@higidi.de> Date: Fri, 17 Nov 2017 09:47:01 +0100 Subject: [PATCH] [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. --- Classes/Strategy/MutexAdapterStrategy.php | 13 +++++++++++-- .../Strategy/MutexAdapterStrategyTest.php | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Classes/Strategy/MutexAdapterStrategy.php b/Classes/Strategy/MutexAdapterStrategy.php index 216c14f..9b56b3d 100644 --- a/Classes/Strategy/MutexAdapterStrategy.php +++ b/Classes/Strategy/MutexAdapterStrategy.php @@ -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; } /** diff --git a/Tests/Unit/Strategy/MutexAdapterStrategyTest.php b/Tests/Unit/Strategy/MutexAdapterStrategyTest.php index 3709620..ecdb780 100644 --- a/Tests/Unit/Strategy/MutexAdapterStrategyTest.php +++ b/Tests/Unit/Strategy/MutexAdapterStrategyTest.php @@ -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 */ -- GitLab