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,31 +34,53 @@ 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 );
61+
62+ foreach (array_rand ($ this ->connections , $ numberToClose ) as $ keyToClose ) {
63+ $ this ->connections [$ keyToClose ]->close ();
64+ }
65+ }
66+
67+ private function closeMinortyConnections ()
68+ {
69+ $ numberToClose = ceil (count ($ this ->connections ) / 2 ) - 1 ;
4870
49- $ this ->mutex = new PHPRedisMutex ([$ this ->redis ], "test " );
71+ foreach ((array ) array_rand ($ this ->connections , $ numberToClose ) as $ keyToClose ) {
72+ $ this ->connections [$ keyToClose ]->close ();
73+ }
5074 }
5175
5276 /**
53- * Tests add() fails.
54- *
55- * @test
5677 * @expectedException \malkusch\lock\exception\LockAcquireException
5778 * @expectedExceptionCode \malkusch\lock\exception\MutexException::REDIS_NOT_ENOUGH_SERVERS
5879 */
5980 public function testAddFails ()
6081 {
61- $ this ->redis ->close ();
82+ $ this ->closeMajorityConnections ();
83+
6284 $ this ->mutex ->synchronized (function () {
6385 $ this ->fail ("Code execution is not expected " );
6486 });
@@ -73,21 +95,40 @@ public function testAddFails()
7395 public function testEvalScriptFails ()
7496 {
7597 $ this ->mutex ->synchronized (function () {
76- $ this ->redis -> close ();
98+ $ this ->closeMajorityConnections ();
7799 });
78100 }
79101
80102 /**
81103 * @param $serialization
82104 * @dataProvider dpSerializationModes
83105 */
84- public function testSyncronizedWorks ($ serialization )
106+ public function testSynchronizedWorks ($ serialization )
85107 {
86- $ this ->redis ->setOption (Redis::OPT_SERIALIZER , $ serialization );
108+ foreach ($ this ->connections as $ connection ) {
109+ $ connection ->setOption (Redis::OPT_SERIALIZER , $ serialization );
110+ }
87111
88- $ this ->mutex ->synchronized (function () {
89- $ this ->assertTrue (true );
90- });
112+ $ this ->assertNull ($ this ->mutex ->synchronized (function () {
113+ return null ;
114+ }));
115+ }
116+
117+ public function testResistantToPartialClusterFailuresForAcquiringLock ()
118+ {
119+ $ this ->closeMinortyConnections ();
120+
121+ $ this ->assertNull ($ this ->mutex ->synchronized (function () {
122+ return null ;
123+ }));
124+ }
125+
126+ public function testResistantToPartialClusterFailuresForReleasingLock ()
127+ {
128+ $ this ->assertNull ($ this ->mutex ->synchronized (function () {
129+ $ this ->closeMinortyConnections ();
130+ return null ;
131+ }));
91132 }
92133
93134 public function dpSerializationModes ()
0 commit comments