8000 feature #28538 [Lock] Wrap release exception (jderusse) · symfony/symfony@7ae5722 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ae5722

Browse files
committed
feature #28538 [Lock] Wrap release exception (jderusse)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Lock] Wrap release exception | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27920 (comment) | License | MIT | Doc PR | NA Commits ------- c37f9e9 Wrap release exception
2 parents 85d335a + c37f9e9 commit 7ae5722

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,22 @@ public function isAcquired()
147147
*/
148148
public function release()
149149
{
150-
$this->store->delete($this->key);
151-
$this->dirty = false;
150+
try {
151+
try {
152+
$this->store->delete($this->key);
153+
$this->dirty = false;
154+
} catch (LockReleasingException $e) {
155+
throw $e;
156+
} catch (\Exception $e) {
157+
throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key), 0, $e);
158+
}
152159

153-
if ($this->store->exists($this->key)) {
160+
if ($this->store->exists($this->key)) {
161+
throw new LockReleasingException(sprintf('Failed to release the "%s" lock, the resource is still locked.', $this->key));
162+
}
163+
} catch (LockReleasingException $e) {
154164
$this->logger->notice('Failed to release the "{resource}" lock.', array('resource' => $this->key));
155-
throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key));
165+
throw $e;
156166
}
157167
}
158168

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,29 @@ public function testNoAutoReleaseWhenNotConfigured()
184184
unset($lock);
185185
}
186186

187+
/**
188+
* @expectedException \Symfony\Component\Lock\Exception\LockReleasingException
189+
*/
190+
public function testReleaseThrowsExceptionWhenDeletionFail()
191+
{
192+
$key = new Key(uniqid(__METHOD__, true));
193+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
194+
$lock = new Lock($key, $store, 10);
195+
196+
$store
197+
->expects($this->once())
198+
->method('delete')
199+
->with($key)
200+
->willThrowException(new \RuntimeException('Boom'));
201+
202+
$store
203+
->expects($this->never())
204+
->method('exists')
205+
->with($key);
206+
207+
$lock->release();
208+
}
209+
187210
/**
188211
* @expectedException \Symfony\Component\Lock\Exception\LockReleasingException
189212
*/

0 commit comments

Comments
 (0)
0