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\Tests\FunctionalTestCase; |
35
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @author Alexander Miehe <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @covers \Tourstream\RedisLockStrategy\RedisLockStrategy |
41
|
|
|
*/ |
42
|
|
|
class RedisLockStrategyTest extends FunctionalTestCase |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var LockFactory |
46
|
|
|
*/ |
47
|
|
|
private $lockFactory; |
48
|
|
|
private $redisHost; |
49
|
|
|
private $redisDatabase; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception |
54
|
|
|
* @expectedExceptionMessage no configuration for redis lock strategy found |
55
|
|
|
*/ |
56
|
|
|
public function should_throw_exception_because_config_is_missing() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->lockFactory->createLocker('test'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception |
64
|
|
|
* @expectedExceptionMessage no configuration for redis lock strategy found |
65
|
|
|
*/ |
66
|
|
|
public function should_throw_exception_because_config_is_not_an_array() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = 'test'; |
69
|
|
|
$this->lockFactory->createLocker('test'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception |
75
|
|
|
* @expectedExceptionMessage no host for redis lock strategy found |
76
|
|
|
*/ |
77
|
|
|
public function should_throw_exception_because_config_has_no_host() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = []; |
80
|
|
|
|
81
|
|
|
$this->lockFactory->createLocker('test'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
* @expectedException \TYPO3\CMS\Core\Locking\Exception |
87
|
|
|
* @expectedExceptionMessage no database for redis lock strategy found |
88
|
|
|
*/ |
89
|
|
|
public function should_throw_exception_because_config_has_no_database() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
92
|
|
|
'host' => $this->redisHost, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$this->lockFactory->createLocker('test'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @test |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function should_connect_and_acquire_a_lock() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$id = uniqid(); |
104
|
|
|
|
105
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
106
|
|
|
'host' => $this->redisHost, |
107
|
|
|
'port' => 6379, |
108
|
|
|
'database' => $this->redisDatabase, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$locker = $this->lockFactory->createLocker($id); |
112
|
|
|
|
113
|
|
|
$redis = $this->getRedisClient(); |
114
|
|
|
|
115
|
|
|
$redis->set($id, 'testvalue'); |
116
|
|
|
|
117
|
|
|
self::assertTrue($locker->acquire()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \Redis |
122
|
|
|
*/ |
123
|
|
|
private function getRedisClient() |
124
|
|
|
{ |
125
|
|
|
$redis = new \Redis(); |
126
|
|
|
$redis->connect($this->redisHost); |
127
|
|
|
$redis->select($this->redisDatabase); |
128
|
|
|
|
129
|
|
|
return $redis; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function should_connect_and_acquire_a_existing_lock() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$id = uniqid(); |
138
|
|
|
|
139
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
140
|
|
|
'host' => $this->redisHost, |
141
|
|
|
'port' => 6379, |
142
|
|
|
'database' => $this->redisDatabase, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$locker = $this->lockFactory->createLocker($id); |
146
|
|
|
|
147
|
|
|
self::assertTrue($locker->acquire()); |
148
|
|
|
|
149
|
|
|
$redis = $this->getRedisClient(); |
150
|
|
|
|
151
|
|
|
self::assertTrue($redis->exists($id)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function should_connect_and_check_if_lock_is_acquired() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$id = uniqid(); |
160
|
|
|
|
161
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
162
|
|
|
'host' => $this->redisHost, |
163
|
|
|
'port' => 6379, |
164
|
|
|
'database' => $this->redisDatabase, |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
$locker = $this->lockFactory->createLocker($id); |
168
|
|
|
|
169
|
|
|
$redis = $this->getRedisClient(); |
170
|
|
|
|
171
|
|
|
$redis->set($id, 'testvalue'); |
172
|
|
|
|
173
|
|
|
self::assertTrue($locker->isAcquired()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @test |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function should_connect_and_destroy_a_lock() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$id = uniqid(); |
182
|
|
|
|
183
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
184
|
|
|
'host' => $this->redisHost, |
185
|
|
|
'port' => 6379, |
186
|
|
|
'database' => $this->redisDatabase, |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
$locker = $this->lockFactory->createLocker($id); |
190
|
|
|
|
191
|
|
|
$redis = $this->getRedisClient(); |
192
|
|
|
|
193
|
|
|
$redis->set($id, 'testvalue'); |
194
|
|
|
|
195
|
|
|
$locker->destroy(); |
196
|
|
|
|
197
|
|
|
self::assertFalse($redis->exists($id)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @test |
202
|
|
|
*/ |
203
|
|
View Code Duplication |
public function should_connect_and_destroy_a_not_existing_lock() |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$id = uniqid(); |
206
|
|
|
|
207
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
208
|
|
|
'host' => $this->redisHost, |
209
|
|
|
'port' => 6379, |
210
|
|
|
'database' => $this->redisDatabase, |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
$locker = $this->lockFactory->createLocker($id); |
214
|
|
|
|
215
|
|
|
$redis = $this->getRedisClient(); |
216
|
|
|
|
217
|
|
|
$locker->destroy(); |
218
|
|
|
|
219
|
|
|
self::assertFalse($redis->exists($id)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function setUp() |
223
|
|
|
{ |
224
|
|
|
$this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy'; |
225
|
|
|
$this->redisHost = getenv('typo3RedisHost'); |
|
|
|
|
226
|
|
|
$this->redisDatabase = getenv('typo3RedisDatabase'); |
|
|
|
|
227
|
|
|
|
228
|
|
|
parent::setUp(); |
229
|
|
|
|
230
|
|
|
$this->lockFactory = GeneralUtility::makeInstance(LockFactory::class); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
protected function tearDown() |
234
|
|
|
{ |
235
|
|
|
parent::tearDown(); |
236
|
|
|
|
237
|
|
|
$this->getRedisClient()->flushDB(); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker
.