2015-04-29 11:19:02 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-04-29 11:19:02 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Lock;
|
|
|
|
|
|
|
|
|
|
use OCP\Lock\ILockingProvider;
|
2015-04-30 08:16:09 -04:00
|
|
|
use OCP\Lock\LockedException;
|
2015-04-29 11:19:02 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
abstract class LockingProvider extends TestCase {
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @var ILockingProvider
|
2015-04-29 11:19:02 -04:00
|
|
|
*/
|
|
|
|
|
protected $instance;
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @return ILockingProvider
|
2015-04-29 11:19:02 -04:00
|
|
|
*/
|
|
|
|
|
abstract protected function getInstance();
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-04-29 11:19:02 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->instance = $this->getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExclusiveLock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSharedLock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDoubleSharedLock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReleaseSharedLock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
public function testDoubleExclusiveLock(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReleaseExclusiveLock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
public function testExclusiveLockAfterShared(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExclusiveLockAfterSharedReleased(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-21 09:57:33 -04:00
|
|
|
public function testReleaseAll(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
|
2016-04-05 10:13:28 -04:00
|
|
|
$this->instance->acquireLock('fizz#A=23', ILockingProvider::LOCK_EXCLUSIVE);
|
2015-05-21 09:57:33 -04:00
|
|
|
|
|
|
|
|
$this->instance->releaseAll();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
|
2016-04-05 10:13:28 -04:00
|
|
|
$this->assertFalse($this->instance->isLocked('fizz#A=23', ILockingProvider::LOCK_EXCLUSIVE));
|
2015-05-21 09:57:33 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-15 09:44:51 -04:00
|
|
|
public function testReleaseAllAfterChange(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
|
|
$this->instance->changeLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
|
|
$this->instance->releaseAll();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReleaseAllAfterUnlock(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
|
|
$this->instance->releaseLock('bar', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
|
|
$this->instance->releaseAll();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-21 09:57:33 -04:00
|
|
|
public function testReleaseAfterReleaseAll(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
|
|
$this->instance->releaseAll();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
public function testSharedLockAfterExclusive(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-04-29 11:19:02 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
}
|
2015-04-30 08:16:09 -04:00
|
|
|
|
|
|
|
|
public function testLockedExceptionHasPathForShared(): void {
|
|
|
|
|
try {
|
2019-11-23 07:13:54 -05:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
2015-04-30 08:16:09 -04:00
|
|
|
$this->fail('Expected locked exception');
|
|
|
|
|
} catch (LockedException $e) {
|
|
|
|
|
$this->assertEquals('foo', $e->getPath());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLockedExceptionHasPathForExclusive(): void {
|
|
|
|
|
try {
|
2019-11-23 07:13:54 -05:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
2015-04-30 08:16:09 -04:00
|
|
|
$this->fail('Expected locked exception');
|
|
|
|
|
} catch (LockedException $e) {
|
|
|
|
|
$this->assertEquals('foo', $e->getPath());
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-29 08:34:21 -04:00
|
|
|
|
|
|
|
|
public function testChangeLockToExclusive(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeLockToShared(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
public function testChangeLockToExclusiveDoubleShared(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
public function testChangeLockToExclusiveNoShared(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
public function testChangeLockToExclusiveFromExclusive(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
public function testChangeLockToSharedNoExclusive(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
public function testChangeLockToSharedFromShared(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(LockedException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-05-29 08:34:21 -04:00
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
}
|
2018-07-05 08:39:10 -04:00
|
|
|
|
|
|
|
|
public function testReleaseNonExistingShared(): void {
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
|
|
// releasing a lock once to many should not result in a locked state
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
|
|
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
|
|
|
|
|
$this->instance->releaseLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
}
|
2015-04-29 11:19:02 -04:00
|
|
|
}
|