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()
28 // Reusable App object
30 $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
31 $this->mockApp($this->root, $configMock);
33 ->shouldReceive('getHostname')
34 ->andReturn('friendica.local');
37 $this->instance = $this->getInstance();
38 $this->instance->releaseAll();
41 protected function tearDown()
43 $this->instance->releaseAll();
50 public function testLock() {
51 $this->assertFalse($this->instance->isLocked('foo'));
52 $this->assertTrue($this->instance->acquireLock('foo', 1));
53 $this->assertTrue($this->instance->isLocked('foo'));
54 $this->assertFalse($this->instance->isLocked('bar'));
60 public function testDoubleLock() {
61 $this->assertFalse($this->instance->isLocked('foo'));
62 $this->assertTrue($this->instance->acquireLock('foo', 1));
63 $this->assertTrue($this->instance->isLocked('foo'));
64 // We already locked it
65 $this->assertTrue($this->instance->acquireLock('foo', 1));
71 public function testReleaseLock() {
72 $this->assertFalse($this->instance->isLocked('foo'));
73 $this->assertTrue($this->instance->acquireLock('foo', 1));
74 $this->assertTrue($this->instance->isLocked('foo'));
75 $this->instance->releaseLock('foo');
76 $this->assertFalse($this->instance->isLocked('foo'));
82 public function testReleaseAll() {
83 $this->assertTrue($this->instance->acquireLock('foo', 1));
84 $this->assertTrue($this->instance->acquireLock('bar', 1));
85 $this->assertTrue($this->instance->acquireLock('nice', 1));
87 $this->assertTrue($this->instance->isLocked('foo'));
88 $this->assertTrue($this->instance->isLocked('bar'));
89 $this->assertTrue($this->instance->isLocked('nice'));
91 $this->instance->releaseAll();
93 $this->assertFalse($this->instance->isLocked('foo'));
94 $this->assertFalse($this->instance->isLocked('bar'));
95 $this->assertFalse($this->instance->isLocked('nice'));
101 public function testReleaseAfterUnlock() {
102 $this->assertFalse($this->instance->isLocked('foo'));
103 $this->assertFalse($this->instance->isLocked('bar'));
104 $this->assertFalse($this->instance->isLocked('nice'));
105 $this->assertTrue($this->instance->acquireLock('foo', 1));
106 $this->assertTrue($this->instance->acquireLock('bar', 1));
107 $this->assertTrue($this->instance->acquireLock('nice', 1));
109 $this->instance->releaseLock('foo');
111 $this->assertFalse($this->instance->isLocked('foo'));
112 $this->assertTrue($this->instance->isLocked('bar'));
113 $this->assertTrue($this->instance->isLocked('nice'));
115 $this->instance->releaseAll();
117 $this->assertFalse($this->instance->isLocked('bar'));
118 $this->assertFalse($this->instance->isLocked('nice'));
124 function testLockTTL() {
125 $this->markTestSkipped('taking too much time without mocking');
127 $this->assertFalse($this->instance->isLocked('foo'));
128 $this->assertFalse($this->instance->isLocked('bar'));
130 // 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
131 $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
132 $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
134 $this->assertTrue($this->instance->isLocked('foo'));
135 $this->assertTrue($this->instance->isLocked('bar'));
139 $this->assertFalse($this->instance->isLocked('foo'));
140 $this->assertTrue($this->instance->isLocked('bar'));
144 $this->assertFalse($this->instance->isLocked('foo'));
145 $this->assertFalse($this->instance->isLocked('bar'));