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 $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
35 $this->mockApp($this->root, $configMock);
37 ->shouldReceive('getHostname')
38 ->andReturn('friendica.local');
41 $this->mockConfigGet('config', 'hostname', 'localhost');
42 $this->mockConfigGet('system', 'throttle_limit_day', 100);
43 $this->mockConfigGet('system', 'throttle_limit_week', 100);
44 $this->mockConfigGet('system', 'throttle_limit_month', 100);
45 $this->mockConfigGet('system', 'theme', 'system_theme');
48 protected function tearDown()
50 $this->instance->releaseAll();
57 public function testLock() {
58 $this->assertFalse($this->instance->isLocked('foo'));
59 $this->assertTrue($this->instance->acquireLock('foo', 1));
60 $this->assertTrue($this->instance->isLocked('foo'));
61 $this->assertFalse($this->instance->isLocked('bar'));
67 public function testDoubleLock() {
68 $this->assertFalse($this->instance->isLocked('foo'));
69 $this->assertTrue($this->instance->acquireLock('foo', 1));
70 $this->assertTrue($this->instance->isLocked('foo'));
71 // We already locked it
72 $this->assertTrue($this->instance->acquireLock('foo', 1));
78 public function testReleaseLock() {
79 $this->assertFalse($this->instance->isLocked('foo'));
80 $this->assertTrue($this->instance->acquireLock('foo', 1));
81 $this->assertTrue($this->instance->isLocked('foo'));
82 $this->instance->releaseLock('foo');
83 $this->assertFalse($this->instance->isLocked('foo'));
89 public function testReleaseAll() {
90 $this->assertTrue($this->instance->acquireLock('foo', 1));
91 $this->assertTrue($this->instance->acquireLock('bar', 1));
92 $this->assertTrue($this->instance->acquireLock('nice', 1));
94 $this->assertTrue($this->instance->isLocked('foo'));
95 $this->assertTrue($this->instance->isLocked('bar'));
96 $this->assertTrue($this->instance->isLocked('nice'));
98 $this->instance->releaseAll();
100 $this->assertFalse($this->instance->isLocked('foo'));
101 $this->assertFalse($this->instance->isLocked('bar'));
102 $this->assertFalse($this->instance->isLocked('nice'));
108 public function testReleaseAfterUnlock() {
109 $this->assertFalse($this->instance->isLocked('foo'));
110 $this->assertFalse($this->instance->isLocked('bar'));
111 $this->assertFalse($this->instance->isLocked('nice'));
112 $this->assertTrue($this->instance->acquireLock('foo', 1));
113 $this->assertTrue($this->instance->acquireLock('bar', 1));
114 $this->assertTrue($this->instance->acquireLock('nice', 1));
116 $this->instance->releaseLock('foo');
118 $this->assertFalse($this->instance->isLocked('foo'));
119 $this->assertTrue($this->instance->isLocked('bar'));
120 $this->assertTrue($this->instance->isLocked('nice'));
122 $this->instance->releaseAll();
124 $this->assertFalse($this->instance->isLocked('bar'));
125 $this->assertFalse($this->instance->isLocked('nice'));
131 function testLockTTL() {
132 $this->markTestSkipped('taking too much time without mocking');
134 $this->assertFalse($this->instance->isLocked('foo'));
135 $this->assertFalse($this->instance->isLocked('bar'));
137 // 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
138 $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
139 $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
141 $this->assertTrue($this->instance->isLocked('foo'));
142 $this->assertTrue($this->instance->isLocked('bar'));
146 $this->assertFalse($this->instance->isLocked('foo'));
147 $this->assertTrue($this->instance->isLocked('bar'));
151 $this->assertFalse($this->instance->isLocked('foo'));
152 $this->assertFalse($this->instance->isLocked('bar'));