Wrap release exception

This commit is contained in:
Jérémy Derussé 2018-09-21 15:30:36 +02:00
parent 1fc66ff080
commit c37f9e9e32
No known key found for this signature in database
GPG Key ID: 2083FA5758C473D2
2 changed files with 37 additions and 4 deletions

View File

@ -147,12 +147,22 @@ final class Lock implements LockInterface, LoggerAwareInterface
*/
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;
}
}

View File

@ -184,6 +184,29 @@ class LockTest extends TestCase
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
*/