浏览代码

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

Fixes #342.
Daniele Alessandri 9 年之前
父节点
当前提交
2640062803

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

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

+ 46 - 0
tests/Predis/Connection/Replication/SentinelReplicationTest.php

@@ -569,6 +569,52 @@ class SentinelReplicationTest extends PredisTestCase
         $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
      */