3 namespace Friendica\Test\src\Core\Lock;
5 use Friendica\Test\MockedTest;
6 use Friendica\Test\Util\AppMockTrait;
7 use Friendica\Test\Util\VFSTrait;
8 use Psr\Log\LoggerInterface;
9 use Psr\Log\NullLogger;
11 abstract class LockTest extends MockedTest
17 * @var int Start time of the mock (used for time operations)
19 protected $startTime = 1417011228;
22 * @var \Friendica\Core\Lock\ILockDriver
26 abstract protected function getInstance();
28 protected function setUp()
30 // Reusable App object
32 $this->mockApp($this->root);
34 ->shouldReceive('getHostname')
35 ->andReturn('friendica.local');
37 $logger = new NullLogger();
38 $this->dice->shouldReceive('create')
39 ->with(LoggerInterface::class)
43 $this->instance = $this->getInstance();
44 $this->instance->releaseAll();
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->assertTrue($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->assertTrue($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->assertTrue($this->instance->releaseAll());
123 $this->assertFalse($this->instance->isLocked('bar'));
124 $this->assertFalse($this->instance->isLocked('nice'));
130 public function testReleaseWitTTL()
132 $this->assertFalse($this->instance->isLocked('test'));
133 $this->assertTrue($this->instance->acquireLock('test', 1, 10));
134 $this->assertTrue($this->instance->isLocked('test'));
135 $this->assertTrue($this->instance->releaseLock('test'));
136 $this->assertFalse($this->instance->isLocked('test'));
142 function testLockTTL() {
143 $this->markTestSkipped('taking too much time without mocking');
145 $this->assertFalse($this->instance->isLocked('foo'));
146 $this->assertFalse($this->instance->isLocked('bar'));
148 // 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
149 $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
150 $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
152 $this->assertTrue($this->instance->isLocked('foo'));
153 $this->assertTrue($this->instance->isLocked('bar'));
157 $this->assertFalse($this->instance->isLocked('foo'));
158 $this->assertTrue($this->instance->isLocked('bar'));
162 $this->assertFalse($this->instance->isLocked('foo'));
163 $this->assertFalse($this->instance->isLocked('bar'));