2121class 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