]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Lock/LockTest.php
Disable testImagickNotFound due low impact and test is not working (needs care in...
[friendica.git] / tests / src / Core / Lock / LockTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Lock;
23
24 use Friendica\Test\MockedTest;
25
26 abstract class LockTest extends MockedTest
27 {
28         /**
29          * @var int Start time of the mock (used for time operations)
30          */
31         protected $startTime = 1417011228;
32
33         /**
34          * @var \Friendica\Core\Lock\ILock
35          */
36         protected $instance;
37
38         abstract protected function getInstance();
39
40         protected function setUp()
41         {
42                 parent::setUp();
43
44                 $this->instance = $this->getInstance();
45                 $this->instance->releaseAll(true);
46         }
47
48         protected function tearDown()
49         {
50                 $this->instance->releaseAll(true);
51                 parent::tearDown();
52         }
53
54         /**
55          * @small
56          */
57         public function testLock()
58         {
59                 $this->assertFalse($this->instance->isLocked('foo'));
60                 $this->assertTrue($this->instance->acquire('foo', 1));
61                 $this->assertTrue($this->instance->isLocked('foo'));
62                 $this->assertFalse($this->instance->isLocked('bar'));
63         }
64
65         /**
66          * @small
67          */
68         public function testDoubleLock()
69         {
70                 $this->assertFalse($this->instance->isLocked('foo'));
71                 $this->assertTrue($this->instance->acquire('foo', 1));
72                 $this->assertTrue($this->instance->isLocked('foo'));
73                 // We already locked it
74                 $this->assertTrue($this->instance->acquire('foo', 1));
75         }
76
77         /**
78          * @small
79          */
80         public function testReleaseLock()
81         {
82                 $this->assertFalse($this->instance->isLocked('foo'));
83                 $this->assertTrue($this->instance->acquire('foo', 1));
84                 $this->assertTrue($this->instance->isLocked('foo'));
85                 $this->instance->release('foo');
86                 $this->assertFalse($this->instance->isLocked('foo'));
87         }
88
89         /**
90          * @small
91          */
92         public function testReleaseAll()
93         {
94                 $this->assertTrue($this->instance->acquire('foo', 1));
95                 $this->assertTrue($this->instance->acquire('bar', 1));
96                 $this->assertTrue($this->instance->acquire('nice', 1));
97
98                 $this->assertTrue($this->instance->isLocked('foo'));
99                 $this->assertTrue($this->instance->isLocked('bar'));
100                 $this->assertTrue($this->instance->isLocked('nice'));
101
102                 $this->assertTrue($this->instance->releaseAll());
103
104                 $this->assertFalse($this->instance->isLocked('foo'));
105                 $this->assertFalse($this->instance->isLocked('bar'));
106                 $this->assertFalse($this->instance->isLocked('nice'));
107         }
108
109         /**
110          * @small
111          */
112         public function testReleaseAfterUnlock()
113         {
114                 $this->assertFalse($this->instance->isLocked('foo'));
115                 $this->assertFalse($this->instance->isLocked('bar'));
116                 $this->assertFalse($this->instance->isLocked('nice'));
117                 $this->assertTrue($this->instance->acquire('foo', 1));
118                 $this->assertTrue($this->instance->acquire('bar', 1));
119                 $this->assertTrue($this->instance->acquire('nice', 1));
120
121                 $this->assertTrue($this->instance->release('foo'));
122
123                 $this->assertFalse($this->instance->isLocked('foo'));
124                 $this->assertTrue($this->instance->isLocked('bar'));
125                 $this->assertTrue($this->instance->isLocked('nice'));
126
127                 $this->assertTrue($this->instance->releaseAll());
128
129                 $this->assertFalse($this->instance->isLocked('bar'));
130                 $this->assertFalse($this->instance->isLocked('nice'));
131         }
132
133         /**
134          * @small
135          */
136         public function testReleaseWitTTL()
137         {
138                 $this->assertFalse($this->instance->isLocked('test'));
139                 $this->assertTrue($this->instance->acquire('test', 1, 10));
140                 $this->assertTrue($this->instance->isLocked('test'));
141                 $this->assertTrue($this->instance->release('test'));
142                 $this->assertFalse($this->instance->isLocked('test'));
143         }
144
145         /**
146          * @small
147          */
148         public function testGetLocks()
149         {
150                 $this->assertTrue($this->instance->acquire('foo', 1));
151                 $this->assertTrue($this->instance->acquire('bar', 1));
152                 $this->assertTrue($this->instance->acquire('nice', 1));
153
154                 $this->assertTrue($this->instance->isLocked('foo'));
155                 $this->assertTrue($this->instance->isLocked('bar'));
156                 $this->assertTrue($this->instance->isLocked('nice'));
157
158                 $locks = $this->instance->getLocks();
159
160                 $this->assertContains('foo', $locks);
161                 $this->assertContains('bar', $locks);
162                 $this->assertContains('nice', $locks);
163         }
164
165         /**
166          * @small
167          */
168         public function testGetLocksWithPrefix()
169         {
170                 $this->assertTrue($this->instance->acquire('foo', 1));
171                 $this->assertTrue($this->instance->acquire('test1', 1));
172                 $this->assertTrue($this->instance->acquire('test2', 1));
173
174                 $this->assertTrue($this->instance->isLocked('foo'));
175                 $this->assertTrue($this->instance->isLocked('test1'));
176                 $this->assertTrue($this->instance->isLocked('test2'));
177
178                 $locks = $this->instance->getLocks('test');
179
180                 $this->assertContains('test1', $locks);
181                 $this->assertContains('test2', $locks);
182                 $this->assertNotContains('foo', $locks);
183         }
184
185         /**
186          * @medium
187          */
188         function testLockTTL()
189         {
190                 $this->markTestSkipped('taking too much time without mocking');
191
192                 $this->assertFalse($this->instance->isLocked('foo'));
193                 $this->assertFalse($this->instance->isLocked('bar'));
194
195                 // 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
196                 $this->assertTrue($this->instance->acquire('foo', 2, 1));
197                 $this->assertTrue($this->instance->acquire('bar', 2, 3));
198
199                 $this->assertTrue($this->instance->isLocked('foo'));
200                 $this->assertTrue($this->instance->isLocked('bar'));
201
202                 sleep(2);
203
204                 $this->assertFalse($this->instance->isLocked('foo'));
205                 $this->assertTrue($this->instance->isLocked('bar'));
206
207                 sleep(2);
208
209                 $this->assertFalse($this->instance->isLocked('foo'));
210                 $this->assertFalse($this->instance->isLocked('bar'));
211         }
212
213         /**
214          * Test if releasing a non-existing lock doesn't throw errors
215          */
216         public function testReleaseLockWithoutLock()
217         {
218                 $this->assertFalse($this->instance->isLocked('wrongLock'));
219                 $this->assertFalse($this->instance->release('wrongLock'));
220         }
221 }