allow getting the path from the lockedexception

This commit is contained in:
Robin Appelman 2015-04-30 14:16:09 +02:00
parent 8119b8b040
commit ba7d221cff
3 changed files with 42 additions and 2 deletions

View File

@ -62,12 +62,12 @@ class MemcacheLockingProvider implements ILockingProvider {
public function acquireLock($path, $type) {
if ($type === self::LOCK_SHARED) {
if (!$this->memcache->inc($path)) {
throw new LockedException($path . ' is locked');
throw new LockedException($path);
}
} else {
$this->memcache->add($path, 0);
if (!$this->memcache->cas($path, 0, 'exclusive')) {
throw new LockedException($path . ' is locked');
throw new LockedException($path);
}
}
}

View File

@ -22,4 +22,25 @@
namespace OCP\Lock;
class LockedException extends \Exception {
/**
* @var string
*/
private $path;
/**
* LockedException constructor.
*
* @param string $path
*/
public function __construct($path) {
parent::__construct($path . ' is locked');
$this->path = $path;
}
/**
* @return string
*/
public function getPath() {
return $this->path;
}
}

View File

@ -22,6 +22,7 @@
namespace Test\Lock;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Test\TestCase;
abstract class LockingProvider extends TestCase {
@ -115,4 +116,22 @@ abstract class LockingProvider extends TestCase {
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
}
public function testLockedExceptionHasPathForShared() {
try {
$this->testSharedLockAfterExclusive();
$this->fail('Expected locked exception');
} catch (LockedException $e) {
$this->assertEquals('foo', $e->getPath());
}
}
public function testLockedExceptionHasPathForExclusive() {
try {
$this->testExclusiveLockAfterShared();
$this->fail('Expected locked exception');
} catch (LockedException $e) {
$this->assertEquals('foo', $e->getPath());
}
}
}