21
21
class PHPRedisMutexTest extends \PHPUnit_Framework_TestCase
22
22
{
23
23
/**
24
- * @var Redis The Redis API.
24
+ * @var Redis[]
25
25
*/
26
- private $ redis ;
27
-
26
+ private $ connections = [] ;
27
+
28
28
/**
29
29
* @var PHPRedisMutex The SUT.
30
30
*/
@@ -34,19 +34,34 @@ protected function setUp()
34
34
{
35
35
parent ::setUp ();
36
36
37
- $ this ->redis = new Redis ();
38
-
39
37
$ 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 ;
45
53
}
46
54
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 );
48
61
49
- $ this ->mutex = new PHPRedisMutex ([$ this ->redis ], "test " );
62
+ foreach (array_rand ($ this ->connections , $ numberToClose ) as $ keyToClose ) {
63
+ $ this ->connections [$ keyToClose ]->close ();
64
+ }
50
65
}
51
66
52
67
/**
@@ -58,7 +73,8 @@ protected function setUp()
58
73
*/
59
74
public function testAddFails ()
60
75
{
61
- $ this ->redis ->close ();
76
+ $ this ->closeMajorityConnections ();
77
+
62
78
$ this ->mutex ->synchronized (function () {
63
79
$ this ->fail ("Code execution is not expected " );
64
80
});
@@ -73,7 +89,7 @@ public function testAddFails()
73
89
public function testEvalScriptFails ()
74
90
{
75
91
$ this ->mutex ->synchronized (function () {
76
- $ this ->redis -> close ();
92
+ $ this ->closeMajorityConnections ();
77
93
});
78
94
}
79
95
@@ -83,7 +99,9 @@ public function testEvalScriptFails()
83
99
*/
84
100
public function testSyncronizedWorks ($ serialization )
85
101
{
86
- $ this ->redis ->setOption (Redis::OPT_SERIALIZER , $ serialization );
102
+ foreach ($ this ->connections as $ connection ) {
103
+ $ connection ->setOption (Redis::OPT_SERIALIZER , $ serialization );
104
+ }
87
105
88
106
$ this ->mutex ->synchronized (function () {
89
107
$ this ->assertTrue (true );
0 commit comments