]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Lock/LockTest.php
Merge pull request #6582 from MrPetovan/bug/6570-fix-diaspora-hashtags
[friendica.git] / tests / src / Core / Lock / LockTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Lock;
4
5 use Friendica\Test\MockedTest;
6 use Friendica\Test\Util\AppMockTrait;
7 use Friendica\Test\Util\VFSTrait;
8
9 abstract class LockTest extends MockedTest
10 {
11         use VFSTrait;
12         use AppMockTrait;
13
14         /**
15          * @var int Start time of the mock (used for time operations)
16          */
17         protected $startTime = 1417011228;
18
19         /**
20          * @var \Friendica\Core\Lock\ILockDriver
21          */
22         protected $instance;
23
24         abstract protected function getInstance();
25
26         protected function setUp()
27         {
28                 parent::setUp();
29                 $this->instance = $this->getInstance();
30                 $this->instance->releaseAll();
31
32                 // Reusable App object
33                 $this->setUpVfsDir();
34                 $this->mockApp($this->root);
35                 $this->app
36                         ->shouldReceive('getHostname')
37                         ->andReturn('friendica.local');
38
39                 // Default config
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');
45         }
46
47         protected function tearDown()
48         {
49                 $this->instance->releaseAll();
50                 parent::tearDown();
51         }
52
53         /**
54          * @small
55          */
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'));
61         }
62
63         /**
64          * @small
65          */
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));
72         }
73
74         /**
75          * @small
76          */
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'));
83         }
84
85         /**
86          * @small
87          */
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));
92
93                 $this->assertTrue($this->instance->isLocked('foo'));
94                 $this->assertTrue($this->instance->isLocked('bar'));
95                 $this->assertTrue($this->instance->isLocked('nice'));
96
97                 $this->instance->releaseAll();
98
99                 $this->assertFalse($this->instance->isLocked('foo'));
100                 $this->assertFalse($this->instance->isLocked('bar'));
101                 $this->assertFalse($this->instance->isLocked('nice'));
102         }
103
104         /**
105          * @small
106          */
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));
114
115                 $this->instance->releaseLock('foo');
116
117                 $this->assertFalse($this->instance->isLocked('foo'));
118                 $this->assertTrue($this->instance->isLocked('bar'));
119                 $this->assertTrue($this->instance->isLocked('nice'));
120
121                 $this->instance->releaseAll();
122
123                 $this->assertFalse($this->instance->isLocked('bar'));
124                 $this->assertFalse($this->instance->isLocked('nice'));
125         }
126
127         /**
128          * @medium
129          */
130         function testLockTTL() {
131                 $this->markTestSkipped('taking too much time without mocking');
132
133                 $this->assertFalse($this->instance->isLocked('foo'));
134                 $this->assertFalse($this->instance->isLocked('bar'));
135
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));
139
140                 $this->assertTrue($this->instance->isLocked('foo'));
141                 $this->assertTrue($this->instance->isLocked('bar'));
142
143                 sleep(2);
144
145                 $this->assertFalse($this->instance->isLocked('foo'));
146                 $this->assertTrue($this->instance->isLocked('bar'));
147
148                 sleep(2);
149
150                 $this->assertFalse($this->instance->isLocked('foo'));
151                 $this->assertFalse($this->instance->isLocked('bar'));
152         }
153 }