Skip to content

Commit f6d4e56

Browse files
committed
Added test for compression.
1 parent d43798c commit f6d4e56

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

classes/mutex/PHPRedisMutex.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use malkusch\lock\exception\LockAcquireException;
66
use malkusch\lock\exception\LockReleaseException;
7+
use Redis;
78
use RedisException;
89

910
/**
@@ -70,14 +71,14 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum
7071
{
7172
// Determine if we need to compress eval arguments.
7273
$lzfCompression = false;
73-
if (\defined(\Redis::class . '::COMPRESSION_LZF') &&
74-
\Redis::COMPRESSION_LZF === $redis->getOption(\Redis::OPT_COMPRESSION) &&
75-
\function_exists('\lzf_compress')
74+
if (defined("Redis::COMPRESSION_LZF") &&
75+
constant("Redis::COMPRESSION_LZF") === $redis->getOption(Redis::OPT_COMPRESSION) &&
76+
function_exists('lzf_compress')
7677
) {
7778
$lzfCompression = true;
7879
}
7980

80-
for ($i = $numkeys, $iMax = \count($arguments); $i < $iMax; $i++) {
81+
for ($i = $numkeys, $iMax = count($arguments); $i < $iMax; $i++) {
8182
/* If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be
8283
* serialized by us, because phpredis does not do this for the eval command.
8384
*/
@@ -87,7 +88,7 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum
8788
* extension installed, compress the arguments as the final step.
8889
*/
8990
if ($lzfCompression) {
90-
$arguments[$i] = \lzf_compress($arguments[$i]);
91+
$arguments[$i] = lzf_compress($arguments[$i]);
9192
}
9293
}
9394

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"psr/log": "^1"
4747
},
4848
"require-dev": {
49+
"ext-lzf": "*",
4950
"ext-memcached": "*",
5051
"ext-pcntl": "*",
5152
"ext-pdo_mysql": "*",
@@ -62,9 +63,10 @@
6263
"squizlabs/php_codesniffer": "^3.3"
6364
},
6465
"suggest": {
66+
"ext-lzf": "To use this library with PHP Redis lzf compression enabled.",
6567
"ext-pnctl": "Enables locking with flock without busy waiting in CLI scripts.",
66-
"ext-sysvsem": "Enables locking using semaphores.",
6768
"ext-redis": "To use this library with the PHP Redis extension.",
69+
"ext-sysvsem": "Enables locking using semaphores.",
6870
"predis/predis": "To use this library with predis."
6971
},
7072
"archive": {

tests/mutex/PHPRedisMutexTest.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ protected function setUp()
4141
$uri = parse_url($redisUri);
4242

4343
// original Redis::set and Redis::eval calls will reopen the connection
44-
$connection = new class extends Redis
45-
{
44+
$connection = new class extends Redis {
4645
private $is_closed = false;
4746

4847
public function close()
@@ -100,6 +99,9 @@ private function closeMinorityConnections()
10099
}
101100

102101
$numberToClose = ceil(count($this->connections) / 2) - 1;
102+
if (0 >= $numberToClose) {
103+
return;
104+
}
103105

104106
foreach ((array) array_rand($this->connections, $numberToClose) as $keyToClose) {
105107
$this->connections[$keyToClose]->close();
@@ -132,27 +134,36 @@ public function testEvalScriptFails()
132134
}
133135

134136
/**
135-
* @param $serialization
136-
* @dataProvider dpSerializationModes
137+
* @dataProvider serializationAndCompressionModes
138+
*
139+
* @param array $serializer Serializer to test.
140+
* @param array $compressor Compressor to test.
137141
*/
138-
public function testSynchronizedWorks($serialization)
142+
public function testSerializersAndCompressors($serializer, $compressor)
139143
{
140144
foreach ($this->connections as $connection) {
141-
$connection->setOption(Redis::OPT_SERIALIZER, $serialization);
145+
$connection->setOption(Redis::OPT_SERIALIZER, $serializer);
146+
$connection->setOption(Redis::OPT_COMPRESSION, $compressor);
142147
}
143148

144-
$this->assertSame("test", $this->mutex->synchronized(function (): string {
145-
return "test";
146-
}));
149+
$this->assertSame(
150+
"test",
151+
$this->mutex->synchronized(function (): string {
152+
return "test";
153+
})
154+
);
147155
}
148156

149157
public function testResistantToPartialClusterFailuresForAcquiringLock()
150158
{
151159
$this->closeMinorityConnections();
152160

153-
$this->assertSame("test", $this->mutex->synchronized(function (): string {
154-
return "test";
155-
}));
161+
$this->assertSame(
162+
"test",
163+
$this->mutex->synchronized(function (): string {
164+
return "test";
165+
})
166+
);
156167
}
157168

158169
public function testResistantToPartialClusterFailuresForReleasingLock()
@@ -163,21 +174,42 @@ public function testResistantToPartialClusterFailuresForReleasingLock()
163174
}));
164175
}
165176

166-
public function dpSerializationModes()
177+
public function serializationAndCompressionModes()
167178
{
168179
if (!class_exists(Redis::class)) {
169180
return [];
170181
}
171182

172-
$serializers = [
173-
[Redis::SERIALIZER_NONE],
174-
[Redis::SERIALIZER_PHP],
183+
$options = [
184+
[Redis::SERIALIZER_NONE, Redis::COMPRESSION_NONE],
185+
[Redis::SERIALIZER_PHP, Redis::COMPRESSION_NONE],
175186
];
176187

177188
if (defined("Redis::SERIALIZER_IGBINARY")) {
178-
$serializers[] = [constant("Redis::SERIALIZER_IGBINARY")];
189+
$options[] = [
190+
constant("Redis::SERIALIZER_IGBINARY"),
191+
Redis::COMPRESSION_NONE
192+
];
193+
}
194+
195+
if (defined("Redis::COMPRESSION_LZF")) {
196+
$options[] = [
197+
Redis::SERIALIZER_NONE,
198+
constant("Redis::COMPRESSION_LZF")
199+
];
200+
$options[] = [
201+
Redis::SERIALIZER_PHP,
202+
constant("Redis::COMPRESSION_LZF")
203+
];
204+
205+
if (defined("Redis::SERIALIZER_IGBINARY")) {
206+
$options[] = [
207+
constant("Redis::SERIALIZER_IGBINARY"),
208+
constant("Redis::COMPRESSION_LZF")
209+
];
210+
}
179211
}
180212

181-
return $serializers;
213+
return $options;
182214
}
183215
}

0 commit comments

Comments
 (0)