RedisLockStrategyTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
50
    private $redisHost;
51
52
    private $redisDatabase;
53
54
    /**
55
     * @test
56
     * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException
57
     * @expectedExceptionMessage no configuration for redis lock strategy found
58
     */
59
    public function shouldThrowExceptionBecauseConfigIsMissing()
60
    {
61
        $this->lockFactory->createLocker('test');
62
    }
63
64
    /**
65
     * @test
66
     * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException
67
     * @expectedExceptionMessage no configuration for redis lock strategy found
68
     */
69
    public function shouldThrowExceptionBecauseConfigIsNotAnArray()
70
    {
71
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = 'test';
72
73
        $this->lockFactory->createLocker('test');
74
    }
75
76
    /**
77
     * @test
78
     * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException
79
     * @expectedExceptionMessage no host for redis lock strategy found
80
     */
81
    public function shouldThrowExceptionBecauseConfigHasNoHost()
82
    {
83
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [];
84
85
        $this->lockFactory->createLocker('test');
86
    }
87
88
    /**
89
     * @test
90
     * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException
91
     * @expectedExceptionMessage no database for redis lock strategy found
92
     */
93 View Code Duplication
    public function shouldThrowExceptionBecauseConfigHasNoDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
96
            'host' => $this->redisHost,
97
        ];
98
99
        $this->lockFactory->createLocker('test');
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function shouldConnectAndAcquireAExistingLock()
106
    {
107
        $subject = uniqid();
108
109
        $locker = $this->getLocker($subject);
110
111
        self::assertTrue($locker->acquire());
112
    }
113
114
    /**
115
     * @test
116
     */
117 View Code Duplication
    public function shouldConnectAndAcquireALock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
118
    {
119
        $subject = uniqid();
120
121
        $name = sprintf('lock:name:%s', $subject);
122
123
        $locker = $this->getLocker($subject);
124
125
        $redis = $this->getRedisClient();
126
127
        self::assertTrue($locker->acquire());
128
129
        self::assertTrue($redis->exists($name));
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function shouldConnectAndCheckIfLockIsAcquired()
136
    {
137
        $subject = uniqid();
138
139
        $locker = $this->getLocker($subject);
140
141
        $locker->acquire();
142
143
        self::assertTrue($locker->isAcquired());
144
    }
145
146
    /**
147
     * @test
148
     */
149 View Code Duplication
    public function shouldConnectAndDestroyALock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
150
    {
151
        $subject = uniqid();
152
153
        $name = sprintf('lock:name:%s', $subject);
154
155
        $locker = $this->getLocker($subject);
156
157
        $redis = $this->getRedisClient();
158
159
        $locker->destroy();
160
161
        self::assertFalse($redis->exists($name));
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function shouldAcquireNonBlockingAndReleaseMoreLocks()
168
    {
169
        $subject = uniqid();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
170
        $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
171
172
        $locker1 = $this->getLocker($subject);
173
        $locker2 = $this->getLocker($subject);
174
        $locker3 = $this->getLocker($subject);
175
176
        self::assertTrue($locker1->acquire($capabilities));
177
        self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker2->acquire($capabilities);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
178
        self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker3->acquire($capabilities);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
179
180
        self::assertTrue($locker1->isAcquired());
181
        self::assertFalse($locker2->isAcquired());
182
        self::assertFalse($locker3->isAcquired());
183
184
        self::assertTrue($locker1->release());
185
186
        self::assertFalse($locker1->isAcquired());
187
        self::assertFalse($locker2->isAcquired());
188
        self::assertFalse($locker3->isAcquired());
189
190
        self::assertTrue($locker2->acquire($capabilities));
191
192
        self::assertFalse($locker1->isAcquired());
193
        self::assertTrue($locker2->isAcquired());
194
        self::assertFalse($locker3->isAcquired());
195
196
        self::assertTrue($locker3->release());
197
198
        self::assertFalse($locker1->isAcquired());
199
        self::assertTrue($locker2->isAcquired());
200
        self::assertFalse($locker3->isAcquired());
201
202
        self::assertFalse($locker1->acquire($capabilities));
203
204
        self::assertFalse($locker1->isAcquired());
205
        self::assertTrue($locker2->isAcquired());
206
        self::assertFalse($locker3->isAcquired());
207
208
        self::assertTrue($locker2->release());
209
210
        self::assertFalse($locker1->isAcquired());
211
        self::assertFalse($locker2->isAcquired());
212
        self::assertFalse($locker3->isAcquired());
213
214
        self::assertTrue($locker3->acquire($capabilities));
215
216
        self::assertFalse($locker1->isAcquired());
217
        self::assertFalse($locker2->isAcquired());
218
        self::assertTrue($locker3->isAcquired());
219
    }
220
221
    protected function setUp()
222
    {
223
        $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy';
224
        $this->redisHost              = getenv('typo3RedisHost');
225
        $this->redisDatabase          = getenv('typo3RedisDatabase');
226
227
        parent::setUp();
228
229
        $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class);
230
    }
231
232
    protected function tearDown()
233
    {
234
        parent::tearDown();
235
236
        $this->getRedisClient()->flushDB();
237
    }
238
239
    /**
240
     * @return \Redis
241
     */
242
    private function getRedisClient()
243
    {
244
        $redis = new \Redis();
245
        $redis->connect($this->redisHost);
246
        $redis->select($this->redisDatabase);
247
248
        return $redis;
249
    }
250
251
    /**
252
     * @param string $id Locker id
253
     *
254
     * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface
255
     */
256 View Code Duplication
    private function getLocker($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
257
    {
258
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
259
            'host'     => $this->redisHost,
260
            'database' => $this->redisDatabase,
261
        ];
262
263
        return $this->lockFactory->createLocker($id);
264
    }
265
266
}
267