1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tourstream\RedisLockStrategy\Tests; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2017 Alexander Miehe ([email protected]) |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* You may not remove or change the name of the author above. See: |
12
|
|
|
* http://www.gnu.org/licenses/gpl-faq.html#IWantCredit |
13
|
|
|
* |
14
|
|
|
* This script is part of the Typo3 project. The Typo3 project is |
15
|
|
|
* free software; you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU General Public License as published by |
17
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
18
|
|
|
* (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* The GNU General Public License can be found at |
21
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
22
|
|
|
* A copy is found in the LICENSE and distributed with these scripts. |
23
|
|
|
* |
24
|
|
|
* |
25
|
|
|
* This script is distributed in the hope that it will be useful, |
26
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
27
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28
|
|
|
* GNU General Public License for more details. |
29
|
|
|
* |
30
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
31
|
|
|
***************************************************************/ |
32
|
|
|
|
33
|
|
|
use TYPO3\CMS\Core\Locking\LockFactory; |
34
|
|
|
use TYPO3\CMS\Core\Locking\LockingStrategyInterface; |
35
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
36
|
|
|
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @author Alexander Miehe <[email protected]> |
40
|
|
|
* |
41
|
|
|
* @covers \Tourstream\RedisLockStrategy\RedisLockStrategy |
42
|
|
|
*/ |
43
|
|
|
class RedisLockStrategyTest extends FunctionalTestCase |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var LockFactory |
47
|
|
|
*/ |
48
|
|
|
private $lockFactory; |
49
|
|
|
private $redisHost; |
50
|
|
|
private $redisDatabase; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
55
|
|
|
* @expectedExceptionMessage no configuration for redis lock strategy found |
56
|
|
|
*/ |
57
|
|
|
public function shouldThrowExceptionBecauseConfigIsMissing() |
58
|
|
|
{ |
59
|
|
|
$this->lockFactory->createLocker('test'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
65
|
|
|
* @expectedExceptionMessage no configuration for redis lock strategy found |
66
|
|
|
*/ |
67
|
|
|
public function shouldThrowExceptionBecauseConfigIsNotAnArray() |
68
|
|
|
{ |
69
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = 'test'; |
70
|
|
|
$this->lockFactory->createLocker('test'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
76
|
|
|
* @expectedExceptionMessage no host for redis lock strategy found |
77
|
|
|
*/ |
78
|
|
|
public function shouldThrowExceptionBecauseConfigHasNoHost() |
79
|
|
|
{ |
80
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = []; |
81
|
|
|
|
82
|
|
|
$this->lockFactory->createLocker('test'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @test |
87
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
88
|
|
|
* @expectedExceptionMessage no database for redis lock strategy found |
89
|
|
|
*/ |
90
|
|
|
public function shouldThrowExceptionBecauseConfigHasNoDatabase() |
91
|
|
|
{ |
92
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
93
|
|
|
'host' => $this->redisHost, |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$this->lockFactory->createLocker('test'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function shouldConnectAndAcquireAExistingLock() |
103
|
|
|
{ |
104
|
|
|
$subject = uniqid(); |
105
|
|
|
|
106
|
|
|
$locker = $this->getLocker($subject); |
107
|
|
|
|
108
|
|
|
self::assertTrue($locker->acquire()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function shouldConnectAndAcquireALock() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$subject = uniqid(); |
117
|
|
|
$mutex = sprintf('lock:mutex:%s', $subject); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$locker = $this->getLocker($subject); |
120
|
|
|
|
121
|
|
|
$redis = $this->getRedisClient(); |
122
|
|
|
|
123
|
|
|
self::assertTrue($redis->exists($mutex)); |
124
|
|
|
|
125
|
|
|
self::assertTrue($locker->acquire()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
*/ |
131
|
|
|
public function shouldConnectAndCheckIfLockIsAcquired() |
132
|
|
|
{ |
133
|
|
|
$subject = uniqid(); |
134
|
|
|
|
135
|
|
|
$locker = $this->getLocker($subject); |
136
|
|
|
|
137
|
|
|
$locker->acquire(); |
138
|
|
|
|
139
|
|
|
self::assertTrue($locker->isAcquired()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @test |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function shouldConnectAndDestroyALock() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$subject = uniqid(); |
148
|
|
|
$mutex = sprintf('lock:mutex:%s', $subject); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$locker = $this->getLocker($subject); |
151
|
|
|
|
152
|
|
|
$redis = $this->getRedisClient(); |
153
|
|
|
|
154
|
|
|
$locker->destroy(); |
155
|
|
|
|
156
|
|
|
self::assertFalse($redis->exists($mutex)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
*/ |
162
|
|
|
public function shouldAcquireNonBlockingAndReleaseMoreLocks() |
163
|
|
|
{ |
164
|
|
|
$subject = uniqid(); |
|
|
|
|
165
|
|
|
$name = sprintf('lock:name:%s', $subject); |
|
|
|
|
166
|
|
|
$mutex = sprintf('lock:mutex:%s', $subject); |
|
|
|
|
167
|
|
|
$capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK; |
|
|
|
|
168
|
|
|
$redis = $this->getRedisClient(); |
|
|
|
|
169
|
|
|
|
170
|
|
|
$locker1 = $this->getLocker($subject); |
171
|
|
|
$locker2 = $this->getLocker($subject); |
172
|
|
|
$locker3 = $this->getLocker($subject); |
173
|
|
|
|
174
|
|
|
self::assertEquals(1, $redis->llen($mutex)); |
175
|
|
|
|
176
|
|
|
self::assertTrue($locker1->acquire($capabilities)); |
177
|
|
|
self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker2->acquire($capabilities); |
|
|
|
|
178
|
|
|
self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker3->acquire($capabilities); |
|
|
|
|
179
|
|
|
|
180
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
181
|
|
|
self::assertTrue($locker1->isAcquired()); |
182
|
|
|
self::assertFalse($locker2->isAcquired()); |
183
|
|
|
self::assertFalse($locker3->isAcquired()); |
184
|
|
|
|
185
|
|
|
self::assertTrue($locker1->release()); |
186
|
|
|
|
187
|
|
|
self::assertEquals(1, $redis->llen($mutex)); |
188
|
|
|
self::assertFalse($locker1->isAcquired()); |
189
|
|
|
self::assertFalse($locker2->isAcquired()); |
190
|
|
|
self::assertFalse($locker3->isAcquired()); |
191
|
|
|
|
192
|
|
|
self::assertTrue($locker2->acquire($capabilities)); |
193
|
|
|
|
194
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
195
|
|
|
self::assertFalse($locker1->isAcquired()); |
196
|
|
|
self::assertTrue($locker2->isAcquired()); |
197
|
|
|
self::assertFalse($locker3->isAcquired()); |
198
|
|
|
|
199
|
|
|
self::assertTrue($locker3->release()); |
200
|
|
|
|
201
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
202
|
|
|
self::assertFalse($locker1->isAcquired()); |
203
|
|
|
self::assertTrue($locker2->isAcquired()); |
204
|
|
|
self::assertFalse($locker3->isAcquired()); |
205
|
|
|
|
206
|
|
|
self::assertTrue($locker1->acquire($capabilities)); |
207
|
|
|
|
208
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
209
|
|
|
self::assertTrue($locker1->isAcquired()); |
210
|
|
|
self::assertFalse($locker2->isAcquired()); |
211
|
|
|
self::assertTrue($locker3->isAcquired()); |
212
|
|
|
|
213
|
|
|
self::assertTrue($locker2->release()); |
214
|
|
|
|
215
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
216
|
|
|
self::assertTrue($locker1->isAcquired()); |
217
|
|
|
self::assertFalse($locker2->isAcquired()); |
218
|
|
|
self::assertTrue($locker3->isAcquired()); |
219
|
|
|
|
220
|
|
|
self::assertTrue($locker3->acquire($capabilities)); |
221
|
|
|
|
222
|
|
|
self::assertEquals(0, $redis->llen($mutex)); |
223
|
|
|
self::assertTrue($locker1->isAcquired()); |
224
|
|
|
self::assertFalse($locker2->isAcquired()); |
225
|
|
|
self::assertTrue($locker3->isAcquired()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
protected function setUp() |
229
|
|
|
{ |
230
|
|
|
$this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy'; |
231
|
|
|
$this->redisHost = getenv('typo3RedisHost'); |
232
|
|
|
$this->redisDatabase = getenv('typo3RedisDatabase'); |
233
|
|
|
|
234
|
|
|
parent::setUp(); |
235
|
|
|
|
236
|
|
|
$this->lockFactory = GeneralUtility::makeInstance(LockFactory::class); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function tearDown() |
240
|
|
|
{ |
241
|
|
|
parent::tearDown(); |
242
|
|
|
|
243
|
|
|
$this->getRedisClient()->flushDB(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return \Redis |
248
|
|
|
*/ |
249
|
|
|
private function getRedisClient() |
250
|
|
|
{ |
251
|
|
|
$redis = new \Redis(); |
252
|
|
|
$redis->connect($this->redisHost); |
253
|
|
|
$redis->select($this->redisDatabase); |
254
|
|
|
|
255
|
|
|
return $redis; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $id Locker id |
260
|
|
|
* |
261
|
|
|
* @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface |
262
|
|
|
*/ |
263
|
|
|
private function getLocker($id) |
264
|
|
|
{ |
265
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
266
|
|
|
'host' => $this->redisHost, |
267
|
|
|
'port' => 6379, |
268
|
|
|
'database' => $this->redisDatabase, |
269
|
|
|
]; |
270
|
|
|
|
271
|
|
|
return $this->lockFactory->createLocker($id); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.