ソースを参照

Use master for connect() when sentinel reports no slaves.

Fixes #342.
Daniele Alessandri 9 年 前
コミット
a523cf7731

+ 3 - 1
src/Connection/Aggregate/SentinelReplication.php

@@ -617,7 +617,9 @@ class SentinelReplication implements ReplicationInterface
     public function connect()
     public function connect()
     {
     {
         if (!$this->current) {
         if (!$this->current) {
-            $this->current = $this->pickSlave();
+            if (!$this->current = $this->pickSlave()) {
+                $this->current = $this->getMaster();
+            }
         }
         }
 
 
         $this->current->connect();
         $this->current->connect();

+ 42 - 0
tests/Predis/Connection/Aggregate/SentinelReplicationTest.php

@@ -552,6 +552,48 @@ class SentinelReplicationTest extends PredisTestCase
         $replication->connect();
         $replication->connect();
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToMasterIfStillEmpty()
+    {
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1->expects($this->at(0))
+                  ->method('executeCommand')
+                  ->with($this->isRedisCommand(
+                      'SENTINEL', array('slaves', 'svc')
+                  ))
+                  ->will($this->returnValue(
+                      array()
+                  ));
+        $sentinel1->expects($this->at(1))
+                  ->method('executeCommand')
+                  ->with($this->isRedisCommand(
+                      'SENTINEL', array('get-master-addr-by-name', 'svc')
+                  ))
+                  ->will($this->returnValue(
+                      array('127.0.0.1', '6381')
+                  ));
+
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master->expects($this->once())
+               ->method('connect');
+
+        $factory = $this->getMock('Predis\Connection\FactoryInterface');
+        $factory->expects($this->once())
+                 ->method('create')
+                 ->with(array(
+                    'host' => '127.0.0.1',
+                    'port' => '6381',
+                    'alias' => 'master',
+                  ))
+                 ->will($this->returnValue($master));
+
+        $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
+
+        $replication->connect();
+    }
+
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */