Skip to content

Commit 61dbd5f

Browse files
Fix predis tests using predis cluster
1 parent f12f35e commit 61dbd5f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

classes/mutex/PredisMutex.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace malkusch\lock\mutex;
44

5-
use Predis\Client;
5+
use Predis\ClientInterface;
66
use Predis\PredisException;
77
use malkusch\lock\exception\LockAcquireException;
88
use malkusch\lock\exception\LockReleaseException;
@@ -22,7 +22,7 @@ class PredisMutex extends RedisMutex
2222
/**
2323
* Sets the Redis connections.
2424
*
25-
* @param Client[] $clients The Redis clients.
25+
* @param ClientInterface[] $clients The Redis clients.
2626
* @param string $name The lock name.
2727
* @param int $timeout The time in seconds a lock expires, default is 3.
2828
*
@@ -38,6 +38,7 @@ public function __construct(array $clients, $name, $timeout = 3)
3838
*/
3939
protected function add($client, $key, $value, $expire)
4040
{
41+
/** @var ClientInterface $client */
4142
try {
4243
return $client->set($key, $value, "EX", $expire, "NX");
4344
} catch (PredisException $e) {
@@ -55,6 +56,7 @@ protected function add($client, $key, $value, $expire)
5556
*/
5657
protected function evalScript($client, $script, $numkeys, array $arguments)
5758
{
59+
/** @var ClientInterface $client */
5860
try {
5961
return $client->eval(...array_merge([$script, $numkeys], $arguments));
6062
} catch (PredisException $e) {
@@ -71,6 +73,7 @@ protected function evalScript($client, $script, $numkeys, array $arguments)
7173
*/
7274
protected function getRedisIdentifier($client)
7375
{
76+
/** @var ClientInterface $client */
7477
return (string) $client->getConnection();
7578
}
7679
}

tests/mutex/PredisMutexTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ protected function setUp()
2929
{
3030
parent::setUp();
3131

32-
$this->client = new Client(getenv("REDIS_URIS") ?: "redis://localhost");
33-
$this->client->flushall(); // Clear any existing locks
32+
$this->client = new Client($this->getPredisConfig());
33+
34+
if (count($this->getPredisConfig()) == 1) {
35+
$this->client->flushall(); // Clear any existing locks
36+
}
37+
}
38+
39+
private function getPredisConfig()
40+
{
41+
if (getenv("REDIS_URIS") === false) {
42+
return null;
43+
}
44+
45+
$servers = explode(",", getenv("REDIS_URIS"));
46+
47+
return array_map(function($redisUri) {
48+
return str_replace("redis://", "tcp://", $redisUri);
49+
}, $servers);
3450
}
3551

3652
/**

0 commit comments

Comments
 (0)