Completed
Pull Request — master (#8)
by
unknown
03:33
created

shouldAcquireNonBlockingAndReleaseMoreLocks()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 65
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 65
rs 9.3571
cc 3
eloc 47
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
113
     * @test
114
     */
115 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...
116
    {
117
        $subject = uniqid();
118
        $mutex = sprintf('lock:mutex:%s', $subject);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
119
120
        $locker = $this->getLocker($subject);
121
122
        $redis = $this->getRedisClient();
123
124
        self::assertTrue($redis->exists($mutex));
125
126
        self::assertTrue($locker->acquire());
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function shouldConnectAndCheckIfLockIsAcquired()
133
    {
134
        $subject = uniqid();
135
136
        $locker = $this->getLocker($subject);
137
138
        $locker->acquire();
139
140
        self::assertTrue($locker->isAcquired());
141
    }
142
143
    /**
144
     * @test
145
     */
146 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...
147
    {
148
        $subject = uniqid();
149
        $mutex = sprintf('lock:mutex:%s', $subject);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
150
151
        $locker = $this->getLocker($subject);
152
153
        $redis = $this->getRedisClient();
154
155
        $locker->destroy();
156
157
        self::assertFalse($redis->exists($mutex));
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function shouldAcquireNonBlockingAndReleaseMoreLocks()
164
    {
165
        $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...
166
        $name = sprintf('lock:name:%s', $subject);
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
167
        $mutex = sprintf('lock:mutex:%s', $subject);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
168
        $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...
169
        $redis = $this->getRedisClient();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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
171
        $locker1 = $this->getLocker($subject);
172
        $locker2 = $this->getLocker($subject);
173
        $locker3 = $this->getLocker($subject);
174
175
        self::assertEquals(1, $redis->llen($mutex));
176
177
        self::assertTrue($locker1->acquire($capabilities));
178
        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...
179
        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...
180
181
        self::assertEquals(0, $redis->llen($mutex));
182
        self::assertTrue($locker1->isAcquired());
183
        self::assertFalse($locker2->isAcquired());
184
        self::assertFalse($locker3->isAcquired());
185
186
        self::assertTrue($locker1->release());
187
188
        self::assertEquals(1, $redis->llen($mutex));
189
        self::assertFalse($locker1->isAcquired());
190
        self::assertFalse($locker2->isAcquired());
191
        self::assertFalse($locker3->isAcquired());
192
193
        self::assertTrue($locker2->acquire($capabilities));
194
195
        self::assertEquals(0, $redis->llen($mutex));
196
        self::assertFalse($locker1->isAcquired());
197
        self::assertTrue($locker2->isAcquired());
198
        self::assertFalse($locker3->isAcquired());
199
200
        self::assertTrue($locker3->release());
201
202
        self::assertEquals(0, $redis->llen($mutex));
203
        self::assertFalse($locker1->isAcquired());
204
        self::assertTrue($locker2->isAcquired());
205
        self::assertFalse($locker3->isAcquired());
206
207
        self::assertTrue($locker1->acquire($capabilities));
208
209
        self::assertEquals(0, $redis->llen($mutex));
210
        self::assertTrue($locker1->isAcquired());
211
        self::assertFalse($locker2->isAcquired());
212
        self::assertTrue($locker3->isAcquired());
213
214
        self::assertTrue($locker2->release());
215
216
        self::assertEquals(0, $redis->llen($mutex));
217
        self::assertTrue($locker1->isAcquired());
218
        self::assertFalse($locker2->isAcquired());
219
        self::assertTrue($locker3->isAcquired());
220
221
        self::assertTrue($locker3->acquire($capabilities));
222
223
        self::assertEquals(0, $redis->llen($mutex));
224
        self::assertTrue($locker1->isAcquired());
225
        self::assertFalse($locker2->isAcquired());
226
        self::assertTrue($locker3->isAcquired());
227
    }
228
229
    protected function setUp()
230
    {
231
        $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy';
232
        $this->redisHost              = getenv('typo3RedisHost');
233
        $this->redisDatabase          = getenv('typo3RedisDatabase');
234
235
        parent::setUp();
236
237
        $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class);
238
    }
239
240
    protected function tearDown()
241
    {
242
        parent::tearDown();
243
244
        $this->getRedisClient()->flushDB();
245
    }
246
247
    /**
248
     * @return \Redis
249
     */
250
    private function getRedisClient()
251
    {
252
        $redis = new \Redis();
253
        $redis->connect($this->redisHost);
254
        $redis->select($this->redisDatabase);
255
256
        return $redis;
257
    }
258
259
    /**
260
     * @param string $id Locker id
261
     *
262
     * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface
263
     */
264
    private function getLocker($id)
265
    {
266
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
267
            'host'     => $this->redisHost,
268
            'port'     => 6379,
269
            'database' => $this->redisDatabase,
270
        ];
271
272
        return $this->lockFactory->createLocker($id);
273
    }
274
275
}
276