3 namespace Friendica\Test\src\Core\Lock;
5 use Friendica\BaseObject;
6 use Friendica\Core\Config;
7 use Friendica\Test\DatabaseTest;
9 abstract class LockTest extends DatabaseTest
12 * @var \Friendica\Core\Lock\ILockDriver
16 abstract protected function getInstance();
18 protected function setUp()
21 $this->instance = $this->getInstance();
22 $this->instance->releaseAll();
24 // Reusable App object
25 $this->app = BaseObject::getApp();
28 Config::set('config', 'hostname', 'localhost');
29 Config::set('system', 'throttle_limit_day', 100);
30 Config::set('system', 'throttle_limit_week', 100);
31 Config::set('system', 'throttle_limit_month', 100);
32 Config::set('system', 'theme', 'system_theme');
35 protected function tearDown()
38 $this->instance->releaseAll();
44 public function testLock() {
45 $this->assertFalse($this->instance->isLocked('foo'));
46 $this->assertTrue($this->instance->acquireLock('foo', 1));
47 $this->assertTrue($this->instance->isLocked('foo'));
48 $this->assertFalse($this->instance->isLocked('bar'));
54 public function testDoubleLock() {
55 $this->assertFalse($this->instance->isLocked('foo'));
56 $this->assertTrue($this->instance->acquireLock('foo', 1));
57 $this->assertTrue($this->instance->isLocked('foo'));
58 // We already locked it
59 $this->assertTrue($this->instance->acquireLock('foo', 1));
65 public function testReleaseLock() {
66 $this->assertFalse($this->instance->isLocked('foo'));
67 $this->assertTrue($this->instance->acquireLock('foo', 1));
68 $this->assertTrue($this->instance->isLocked('foo'));
69 $this->instance->releaseLock('foo');
70 $this->assertFalse($this->instance->isLocked('foo'));
76 public function testReleaseAll() {
77 $this->assertTrue($this->instance->acquireLock('foo', 1));
78 $this->assertTrue($this->instance->acquireLock('bar', 1));
79 $this->assertTrue($this->instance->acquireLock('nice', 1));
81 $this->assertTrue($this->instance->isLocked('foo'));
82 $this->assertTrue($this->instance->isLocked('bar'));
83 $this->assertTrue($this->instance->isLocked('nice'));
85 $this->instance->releaseAll();
87 $this->assertFalse($this->instance->isLocked('foo'));
88 $this->assertFalse($this->instance->isLocked('bar'));
89 $this->assertFalse($this->instance->isLocked('nice'));
95 public function testReleaseAfterUnlock() {
96 $this->assertFalse($this->instance->isLocked('foo'));
97 $this->assertFalse($this->instance->isLocked('bar'));
98 $this->assertFalse($this->instance->isLocked('nice'));
99 $this->assertTrue($this->instance->acquireLock('foo', 1));
100 $this->assertTrue($this->instance->acquireLock('bar', 1));
101 $this->assertTrue($this->instance->acquireLock('nice', 1));
103 $this->instance->releaseLock('foo');
105 $this->assertFalse($this->instance->isLocked('foo'));
106 $this->assertTrue($this->instance->isLocked('bar'));
107 $this->assertTrue($this->instance->isLocked('nice'));
109 $this->instance->releaseAll();
111 $this->assertFalse($this->instance->isLocked('bar'));
112 $this->assertFalse($this->instance->isLocked('nice'));
118 function testLockTTL() {
119 $this->assertFalse($this->instance->isLocked('foo'));
120 $this->assertFalse($this->instance->isLocked('bar'));
122 // TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
123 $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
124 $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
126 $this->assertTrue($this->instance->isLocked('foo'));
127 $this->assertTrue($this->instance->isLocked('bar'));
131 $this->assertFalse($this->instance->isLocked('foo'));
132 $this->assertTrue($this->instance->isLocked('bar'));
136 $this->assertFalse($this->instance->isLocked('foo'));
137 $this->assertFalse($this->instance->isLocked('bar'));