Skip to content

Commit 3cb8139

Browse files
author
Willem Stuursma
committed
#16 Try running with a small cluster of redis instances
1 parent 96cd2d8 commit 3cb8139

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ php:
1616
env:
1717
global:
1818
- MEMCACHE_HOST=localhost
19-
- REDIS_URIS=redis://localhost,redis://localhost:63791
19+
- REDIS_URIS=redis://localhost:63791,redis://localhost:63792,redis://localhost:63793
2020
- MYSQL_DSN="mysql:host=localhost;dbname=test"
2121
- MYSQL_USER=travis
2222
- PGSQL_DSN="pgsql:host=localhost;dbname=test;"
@@ -36,6 +36,8 @@ matrix:
3636
before_install:
3737
- phpenv config-add tests/php-travis.ini
3838
- redis-server --port 63791 &
39+
- redis-server --port 63792 &
40+
- redis-server --port 63793 &
3941

4042
install:
4143
- composer install --no-scripts --no-suggest --no-interaction

tests/mutex/PHPRedisMutexTest.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
class PHPRedisMutexTest extends \PHPUnit_Framework_TestCase
2222
{
2323
/**
24-
* @var Redis The Redis API.
24+
* @var Redis[]
2525
*/
26-
private $redis;
27-
26+
private $connections = [];
27+
2828
/**
2929
* @var PHPRedisMutex The SUT.
3030
*/
@@ -34,19 +34,34 @@ protected function setUp()
3434
{
3535
parent::setUp();
3636

37-
$this->redis = new Redis();
38-
3937
$uris = explode(",", getenv("REDIS_URIS") ?: "redis://localhost");
40-
$uri = parse_url($uris[0]);
41-
if (!empty($uri["port"])) {
42-
$this->redis->connect($uri["host"], $uri["port"]);
43-
} else {
44-
$this->redis->connect($uri["host"]);
38+
39+
foreach ($uris as $redisUri) {
40+
$uri = parse_url($redisUri);
41+
42+
$connection = new Redis();
43+
44+
if (!empty($uri["port"])) {
45+
$connection->connect($uri["host"], $uri["port"]);
46+
} else {
47+
$connection->connect($uri["host"]);
48+
}
49+
50+
$connection->flushAll(); // Clear any existing locks.
51+
52+
$this->connections[] = $connection;
4553
}
4654

47-
$this->redis->flushAll(); // Clear any existing locks.
55+
$this->mutex = new PHPRedisMutex($this->connections, "test");
56+
}
57+
58+
private function closeMajorityConnections()
59+
{
60+
$numberToClose = ceil(count($this->connections) / 2);
4861

49-
$this->mutex = new PHPRedisMutex([$this->redis], "test");
62+
foreach (array_rand($this->connections, $numberToClose) as $keyToClose) {
63+
$this->connections[$keyToClose]->close();
64+
}
5065
}
5166

5267
/**
@@ -58,7 +73,8 @@ protected function setUp()
5873
*/
5974
public function testAddFails()
6075
{
61-
$this->redis->close();
76+
$this->closeMajorityConnections();
77+
6278
$this->mutex->synchronized(function () {
6379
$this->fail("Code execution is not expected");
6480
});
@@ -73,7 +89,7 @@ public function testAddFails()
7389
public function testEvalScriptFails()
7490
{
7591
$this->mutex->synchronized(function () {
76-
$this->redis->close();
92+
$this->closeMajorityConnections();
7793
});
7894
}
7995

@@ -83,7 +99,9 @@ public function testEvalScriptFails()
8399
*/
84100
public function testSyncronizedWorks($serialization)
85101
{
86-
$this->redis->setOption(Redis::OPT_SERIALIZER, $serialization);
102+
foreach ($this->connections as $connection) {
103+
$connection->setOption(Redis::OPT_SERIALIZER, $serialization);
104+
}
87105

88106
$this->mutex->synchronized(function () {
89107
$this->assertTrue(true);

0 commit comments

Comments
 (0)