3 namespace Friendica\Test\src\Core\Lock;
5 use Friendica\Test\MockedTest;
6 use Friendica\Test\Util\AppMockTrait;
7 use Friendica\Test\Util\VFSTrait;
9 abstract class LockTest extends MockedTest
15 * @var int Start time of the mock (used for time operations)
17 protected $startTime = 1417011228;
20 * @var \Friendica\Core\Lock\ILockDriver
24 abstract protected function getInstance();
26 protected function setUp()
29 $this->instance = $this->getInstance();
30 $this->instance->releaseAll();
32 // Reusable App object
34 $this->mockApp($this->root);
36 ->shouldReceive('getHostname')
37 ->andReturn('friendica.local');
40 $this->mockConfigGet('config', 'hostname', 'localhost');
41 $this->mockConfigGet('system', 'throttle_limit_day', 100);
42 $this->mockConfigGet('system', 'throttle_limit_week', 100);
43 $this->mockConfigGet('system', 'throttle_limit_month', 100);
44 $this->mockConfigGet('system', 'theme', 'system_theme');
47 protected function tearDown()
49 $this->instance->releaseAll();
56 public function testLock() {
57 $this->assertFalse($this->instance->isLocked('foo'));
58 $this->assertTrue($this->instance->acquireLock('foo', 1));
59 $this->assertTrue($this->instance->isLocked('foo'));
60 $this->assertFalse($this->instance->isLocked('bar'));
66 public function testDoubleLock() {
67 $this->assertFalse($this->instance->isLocked('foo'));
68 $this->assertTrue($this->instance->acquireLock('foo', 1));
69 $this->assertTrue($this->instance->isLocked('foo'));
70 // We already locked it
71 $this->assertTrue($this->instance->acquireLock('foo', 1));
77 public function testReleaseLock() {
78 $this->assertFalse($this->instance->isLocked('foo'));
79 $this->assertTrue($this->instance->acquireLock('foo', 1));
80 $this->assertTrue($this->instance->isLocked('foo'));
81 $this->instance->releaseLock('foo');
82 $this->assertFalse($this->instance->isLocked('foo'));
88 public function testReleaseAll() {
89 $this->assertTrue($this->instance->acquireLock('foo', 1));
90 $this->assertTrue($this->instance->acquireLock('bar', 1));
91 $this->assertTrue($this->instance->acquireLock('nice', 1));
93 $this->assertTrue($this->instance->isLocked('foo'));
94 $this->assertTrue($this->instance->isLocked('bar'));
95 $this->assertTrue($this->instance->isLocked('nice'));
97 $this->instance->releaseAll();
99 $this->assertFalse($this->instance->isLocked('foo'));
100 $this->assertFalse($this->instance->isLocked('bar'));
101 $this->assertFalse($this->instance->isLocked('nice'));
107 public function testReleaseAfterUnlock() {
108 $this->assertFalse($this->instance->isLocked('foo'));
109 $this->assertFalse($this->instance->isLocked('bar'));
110 $this->assertFalse($this->instance->isLocked('nice'));
111 $this->assertTrue($this->instance->acquireLock('foo', 1));
112 $this->assertTrue($this->instance->acquireLock('bar', 1));
113 $this->assertTrue($this->instance->acquireLock('nice', 1));
115 $this->instance->releaseLock('foo');
117 $this->assertFalse($this->instance->isLocked('foo'));
118 $this->assertTrue($this->instance->isLocked('bar'));
119 $this->assertTrue($this->instance->isLocked('nice'));
121 $this->instance->releaseAll();
123 $this->assertFalse($this->instance->isLocked('bar'));
124 $this->assertFalse($this->instance->isLocked('nice'));
130 function testLockTTL() {
131 $this->markTestSkipped('taking too much time without mocking');
133 $this->assertFalse($this->instance->isLocked('foo'));
134 $this->assertFalse($this->instance->isLocked('bar'));
136 // 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
137 $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
138 $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
140 $this->assertTrue($this->instance->isLocked('foo'));
141 $this->assertTrue($this->instance->isLocked('bar'));
145 $this->assertFalse($this->instance->isLocked('foo'));
146 $this->assertTrue($this->instance->isLocked('bar'));
150 $this->assertFalse($this->instance->isLocked('foo'));
151 $this->assertFalse($this->instance->isLocked('bar'));