From c37f9e9e32c62037ccd871662dae5af7955ffd14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Fri, 21 Sep 2018 15:30:36 +0200 Subject: [PATCH] Wrap release exception --- src/Symfony/Component/Lock/Lock.php | 18 +++++++++++---- src/Symfony/Component/Lock/Tests/LockTest.php | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Lock/Lock.php b/src/Symfony/Component/Lock/Lock.php index bef58fbec277f..03ac44cd12f3c 100644 --- a/src/Symfony/Component/Lock/Lock.php +++ b/src/Symfony/Component/Lock/Lock.php @@ -147,12 +147,22 @@ public function isAcquired() */ public function release() { - $this->store->delete($this->key); - $this->dirty = false; + try { + try { + $this->store->delete($this->key); + $this->dirty = false; + } catch (LockReleasingException $e) { + throw $e; + } catch (\Exception $e) { + throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key), 0, $e); + } - if ($this->store->exists($this->key)) { + if ($this->store->exists($this->key)) { + throw new LockReleasingException(sprintf('Failed to release the "%s" lock, the resource is still locked.', $this->key)); + } + } catch (LockReleasingException $e) { $this->logger->notice('Failed to release the "{resource}" lock.', array('resource' => $this->key)); - throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key)); + throw $e; } } diff --git a/src/Symfony/Component/Lock/Tests/LockTest.php b/src/Symfony/Component/Lock/Tests/LockTest.php index 654d58d51c4b6..270b71dfb4e16 100644 --- a/src/Symfony/Component/Lock/Tests/LockTest.php +++ b/src/Symfony/Component/Lock/Tests/LockTest.php @@ -184,6 +184,29 @@ public function testNoAutoReleaseWhenNotConfigured() unset($lock); } + /** + * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException + */ + public function testReleaseThrowsExceptionWhenDeletionFail() + { + $key = new Key(uniqid(__METHOD__, true)); + $store = $this->getMockBuilder(StoreInterface::class)->getMock(); + $lock = new Lock($key, $store, 10); + + $store + ->expects($this->once()) + ->method('delete') + ->with($key) + ->willThrowException(new \RuntimeException('Boom')); + + $store + ->expects($this->never()) + ->method('exists') + ->with($key); + + $lock->release(); + } + /** * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException */