diff --git a/src/Connection/Aggregate/MasterSlaveReplication.php b/src/Connection/Aggregate/MasterSlaveReplication.php index 8a8bda02..a87db892 100644 --- a/src/Connection/Aggregate/MasterSlaveReplication.php +++ b/src/Connection/Aggregate/MasterSlaveReplication.php @@ -53,16 +53,6 @@ class MasterSlaveReplication implements ReplicationInterface $this->strategy = $strategy ?: new ReplicationStrategy(); } - /** - * Checks if one master and at least one slave have been defined. - */ - protected function check() - { - if (!isset($this->master) || !$this->slaves) { - throw new \RuntimeException('Replication needs one master and at least one slave.'); - } - } - /** * Resets the connection state. */ @@ -156,8 +146,6 @@ class MasterSlaveReplication implements ReplicationInterface */ public function switchTo($connection) { - $this->check(); - if (!$connection instanceof NodeConnectionInterface) { $connection = $this->getConnectionById($connection); } @@ -242,9 +230,12 @@ class MasterSlaveReplication implements ReplicationInterface */ public function connect() { - if ($this->current === null) { - $this->check(); - $this->current = $this->pickSlave(); + if (!$this->current) { + if (!$this->current = $this->pickSlave()) { + if (!$this->current = $this->getMaster()) { + throw new ClientException("No available connection for replication"); + } + } } $this->current->connect(); diff --git a/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php b/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php index f0e8836c..ff702920 100644 --- a/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php +++ b/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php @@ -65,8 +65,8 @@ class MasterSlaveReplicationTest extends PredisTestCase /** * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. + * @expectedException \Predis\ClientException + * @expectedExceptionMessage No available connection for replication */ public function testThrowsExceptionOnEmptyReplication() { @@ -76,34 +76,8 @@ class MasterSlaveReplicationTest extends PredisTestCase /** * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. */ - public function testThrowsExceptionOnMissingMaster() - { - $replication = new MasterSlaveReplication(); - $replication->add($this->getMockConnection('tcp://host2?alias=slave1')); - - $replication->connect(); - } - - /** - * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. - */ - public function testThrowsExceptionOnMissingSlave() - { - $replication = new MasterSlaveReplication(); - $replication->add($this->getMockConnection('tcp://host1?alias=master')); - - $replication->connect(); - } - - /** - * @group disconnected - */ - public function testConnectForcesConnectionToOneOfSlaves() + public function testConnectsToOneOfSlaves() { $master = $this->getMockConnection('tcp://host1?alias=master'); $master->expects($this->never())->method('connect'); @@ -118,6 +92,20 @@ class MasterSlaveReplicationTest extends PredisTestCase $replication->connect(); } + /** + * @group disconnected + */ + public function testConnectsToMasterOnMissingSlaves() + { + $master = $this->getMockConnection('tcp://host1?alias=master'); + + $replication = new MasterSlaveReplication(); + $replication->add($master); + + $replication->connect(); + $this->assertSame($master, $replication->getCurrent()); + } + /** * @group disconnected */