12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\Connection\Replication;
- use Predis\Command;
- use Predis\Connection;
- use Predis\Replication\ReplicationStrategy;
- use Predis\Response;
- use PredisTestCase;
- /**
- *
- */
- class MasterSlaveReplicationTest extends PredisTestCase
- {
- /**
- * @group disconnected
- */
- public function testAddingConnectionsToReplication()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertSame($master, $replication->getConnectionById('master'));
- $this->assertSame($slave1, $replication->getConnectionById('slave1'));
- $this->assertSame($slave2, $replication->getConnectionById('slave2'));
- $this->assertSame($master, $replication->getMaster());
- $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionsFromReplication()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertTrue($replication->remove($slave1));
- $this->assertFalse($replication->remove($slave2));
- $this->assertSame($master, $replication->getMaster());
- $this->assertSame(array(), $replication->getSlaves());
- }
- /**
- * @group disconnected
- */
- public function testAddingConnectionsToReplicationWithoutAliasesResultsInCustomId()
- {
- $slave1 = $this->getMockConnection('tcp://host1');
- $slave2 = $this->getMockConnection('tcp://host2:6380');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertSame($slave1, $replication->getConnectionById('slave-host1:6379'));
- $this->assertSame($slave2, $replication->getConnectionById('slave-host2:6380'));
- }
- /**
- * @group disconnected
- * @expectedException \Predis\ClientException
- * @expectedExceptionMessage No available connection for replication
- */
- public function testThrowsExceptionOnEmptyReplication()
- {
- $replication = new MasterSlaveReplication();
- $replication->connect();
- }
- /**
- * @group disconnected
- */
- public function testConnectsToOneOfSlaves()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->never())->method('connect');
- $slave = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave->expects($this->once())->method('connect');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave);
- $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
- */
- public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->never())->method('isConnected')->will($this->returnValue(false));
- $slave = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave->expects($this->once())->method('isConnected')->will($this->returnValue(true));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave);
- $replication->connect();
- $this->assertTrue($replication->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->any())->method('isConnected')->will($this->returnValue(false));
- $slave = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave->expects($this->any())->method('isConnected')->will($this->returnValue(false));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave);
- $this->assertFalse($replication->isConnected());
- $replication->connect();
- $replication->disconnect();
- $this->assertFalse($replication->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testDisconnectForcesCurrentConnectionToDisconnect()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('disconnect');
- $slave = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave->expects($this->once())->method('disconnect');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave);
- $replication->disconnect();
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchConnectionByAlias()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertNull($replication->getCurrent());
- $replication->switchTo('master');
- $this->assertSame($master, $replication->getCurrent());
- $replication->switchTo('slave1');
- $this->assertSame($slave1, $replication->getCurrent());
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid connection or connection not found.
- */
- public function testThrowsErrorWhenSwitchingToUnknownConnectionByAlias()
- {
- $replication = new MasterSlaveReplication();
- $replication->add($this->getMockConnection('tcp://host1?alias=master'));
- $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
- $replication->switchTo('unknown');
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchConnectionByInstance()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertNull($replication->getCurrent());
- $replication->switchTo($master);
- $this->assertSame($master, $replication->getCurrent());
- $replication->switchTo($slave1);
- $this->assertSame($slave1, $replication->getCurrent());
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid connection or connection not found.
- */
- public function testThrowsErrorWhenSwitchingToUnknownConnectionByInstance()
- {
- $replication = new MasterSlaveReplication();
- $replication->add($this->getMockConnection('tcp://host1?alias=master'));
- $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $replication->switchTo($slave2);
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchToMaster()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertNull($replication->getCurrent());
- $replication->switchToMaster();
- $this->assertSame($master, $replication->getCurrent());
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid connection or connection not found.
- */
- public function testThrowsErrorOnSwitchToMasterWithNoMasterDefined()
- {
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->switchToMaster();
- }
- /**
- * @group disconnected
- *
- * @todo We should find a way to test that the slave is indeed randomly selected.
- */
- public function testCanSwitchToRandomSlave()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertNull($replication->getCurrent());
- $replication->switchToSlave();
- $this->assertSame($slave1, $replication->getCurrent());
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid connection or connection not found.
- */
- public function testThrowsErrorOnSwitchToRandomSlaveWithNoSlavesDefined()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->switchToSlave();
- }
- /**
- * @group disconnected
- */
- public function testUsesSlavesOnReadOnlyCommands()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($slave1, $replication->getConnection($cmd));
- $cmd = $commands->createCommand('get', array('foo'));
- $this->assertSame($slave1, $replication->getConnection($cmd));
- }
- /**
- * @group disconnected
- */
- public function testUsesMasterOnWriteRequests()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnection($cmd));
- $cmd = $commands->createCommand('get', array('foo'));
- $this->assertSame($master, $replication->getConnection($cmd));
- }
- /**
- * @group disconnected
- */
- public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($master, $replication->getConnection($cmd));
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnection($cmd));
- }
- /**
- * @group disconnected
- */
- public function testSwitchesFromSlaveToMasterOnWriteRequestss()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($slave1, $replication->getConnection($cmd));
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnection($cmd));
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($master, $replication->getConnection($cmd));
- }
- /**
- * @group disconnected
- */
- public function testWritesCommandToCorrectConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('foo'));
- $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('writeRequest')->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->writeRequest($cmdExists);
- $replication->writeRequest($cmdSet);
- }
- /**
- * @group disconnected
- */
- public function testReadsCommandFromCorrectConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('foo'));
- $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('readResponse')->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->readResponse($cmdExists);
- $replication->readResponse($cmdSet);
- }
- /**
- * @group disconnected
- */
- public function testExecutesCommandOnCorrectConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('foo'));
- $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdExists);
- $replication->executeCommand($cmdSet);
- }
- /**
- * @group disconnected
- */
- public function testWatchTriggersSwitchToMasterConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdWatch = $commands->createCommand('watch', array('foo'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->never())->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdWatch);
- }
- /**
- * @group disconnected
- */
- public function testMultiTriggersSwitchToMasterConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdMulti = $commands->createCommand('multi');
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->never())->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdMulti);
- }
- /**
- * @group disconnected
- */
- public function testEvalTriggersSwitchToMasterConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdEval = $commands->createCommand('eval', array("return redis.call('info')"));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdEval);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->never())->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdEval);
- }
- /**
- * @group disconnected
- */
- public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
- {
- $commands = $this->getCommandFactory();
- $cmdSortNormal = $commands->createCommand('sort', array('key'));
- $cmdSortStore = $commands->createCommand('sort', array('key', array('store' => 'key:store')));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdSortNormal);
- $replication->executeCommand($cmdSortStore);
- }
- /**
- * @group disconnected
- */
- public function testDiscardsUnreachableSlaveAndExecutesReadOnlyCommandOnNextSlave()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('key'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->never())->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(new Connection\ConnectionException($slave1)));
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $slave2->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->returnValue(1));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $replication->switchTo($slave1);
- $response = $replication->executeCommand($cmdExists);
- $this->assertSame(1, $response);
- $this->assertNull($replication->getConnectionById('slave1'));
- $this->assertSame($slave2, $replication->getConnectionById('slave2'));
- }
- /**
- * @group disconnected
- */
- public function testDiscardsUnreachableSlavesAndExecutesReadOnlyCommandOnMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('key'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->returnValue(1));
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(new Connection\ConnectionException($slave1)));
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $slave2->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(new Connection\ConnectionException($slave2)));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $replication->switchTo($slave1);
- $response = $replication->executeCommand($cmdExists);
- $this->assertSame(1, $response);
- $this->assertNull($replication->getConnectionById('slave1'));
- $this->assertNull($replication->getConnectionById('slave2'));
- }
- /**
- * @group disconnected
- */
- public function testSucceedOnReadOnlyCommandAndNoConnectionSetAsMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('key'));
- $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->returnValue(1));
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $response = $replication->executeCommand($cmdExists);
- $this->assertSame(1, $response);
- }
- /**
- * @group disconnected
- * @expectedException \Predis\Replication\MissingMasterException
- * @expectedMessage No master server available for replication
- */
- public function testFailsOnWriteCommandAndNoConnectionSetAsMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdSet = $commands->createCommand('set', array('key', 'value'));
- $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
- $slave1->expects($this->never())->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->executeCommand($cmdSet);
- }
- /**
- * @group disconnected
- */
- public function testDiscardsSlaveWhenRespondsLOADINGAndExecutesReadOnlyCommandOnNextSlave()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->never())
- ->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'EXISTS', array('key')
- ))
- ->will($this->returnValue(
- new Response\Error('LOADING')
- ));
- $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
- $slave2->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'EXISTS', array('key')
- ))
- ->will($this->returnValue(1));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $replication->switchTo($slave1);
- $response = $replication->executeCommand(
- Command\RawCommand::create('exists', 'key')
- );
- $this->assertSame(1, $response);
- $this->assertNull($replication->getConnectionById('slave1'));
- $this->assertSame($slave2, $replication->getConnectionById('slave2'));
- }
- /**
- * @group disconnected
- * @expectedException \Predis\Connection\ConnectionException
- */
- public function testFailsOnUnreachableMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdSet = $commands->createCommand('set', array('key', 'value'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdSet)
- ->will($this->throwException(new Connection\ConnectionException($master)));
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->never())
- ->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->executeCommand($cmdSet);
- }
- /**
- * @group disconnected
- * @expectedException \Predis\NotSupportedException
- * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
- */
- public function testThrowsExceptionOnNonSupportedCommand()
- {
- $cmd = $this->getCommandFactory()->createCommand('info');
- $replication = new MasterSlaveReplication();
- $replication->add($this->getMockConnection('tcp://host1?alias=master'));
- $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
- $replication->getConnection($cmd);
- }
- /**
- * @group disconnected
- */
- public function testCanOverrideReadOnlyFlagForCommands()
- {
- $commands = $this->getCommandFactory();
- $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
- $cmdGet = $commands->createCommand('get', array('foo'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdGet);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
- $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
- $replication->executeCommand($cmdSet);
- $replication->executeCommand($cmdGet);
- }
- /**
- * @group disconnected
- */
- public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
- {
- $commands = $this->getCommandFactory();
- $cmdExistsFoo = $commands->createCommand('exists', array('foo'));
- $cmdExistsBar = $commands->createCommand('exists', array('bar'));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
- list($arg1) = $cmd->getArguments();
- return $arg1 === 'foo';
- });
- $replication->executeCommand($cmdExistsFoo);
- $replication->executeCommand($cmdExistsBar);
- }
- /**
- * @group disconnected
- */
- public function testCanSetReadOnlyFlagForEvalScripts()
- {
- $commands = $this->getCommandFactory();
- $cmdEval = $commands->createCommand('eval', array($script = "return redis.call('info');"));
- $cmdEvalSha = $commands->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $master->expects($this->never())->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $slave1->expects($this->exactly(2))
- ->method('executeCommand')
- ->with($this->logicalOr($cmdEval, $cmdEvalSha));
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->getReplicationStrategy()->setScriptReadOnly($script);
- $replication->executeCommand($cmdEval);
- $replication->executeCommand($cmdEvalSha);
- }
- /**
- * @group disconnected
- * @expectedException \Predis\ClientException
- * @expectedMessage Discovery requires a connection factory
- */
- public function testDiscoveryRequiresConnectionFactory()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->discover();
- }
- /**
- * @group disconnected
- */
- public function testDiscoversReplicationConfigurationFromMaster()
- {
- $connFactory = new Connection\Factory();
- $cmdInfo = Command\RawCommand::create('INFO', 'REPLICATION');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=127.0.0.1,port=6382,state=online,offset=12979,lag=0
- slave1:ip=127.0.0.1,port=6383,state=online,offset=12979,lag=1
- master_repl_offset:12979
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:2
- repl_backlog_histlen:12978
- '));
- $replication = new MasterSlaveReplication();
- $replication->setConnectionFactory($connFactory);
- $replication->add($master);
- $replication->discover();
- $this->assertCount(2, $slaves = $replication->getSlaves());
- $this->assertContainsOnlyInstancesOf('Predis\Connection\ConnectionInterface', $slaves);
- $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
- $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
- $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
- }
- /**
- * @group disconnected
- */
- public function testDiscoversReplicationConfigurationFromSlave()
- {
- $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory->expects($this->at(0))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6381', 'alias' => 'master'))
- ->will($this->returnValue($master));
- $connFactory->expects($this->at(1))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6382'))
- ->will($this->returnValue($slave1));
- $connFactory->expects($this->at(2))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6383'))
- ->will($this->returnValue($slave2));
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:slave
- master_host:127.0.0.1
- master_port:6381
- master_link_status:up
- master_last_io_seconds_ago:8
- master_sync_in_progress:0
- slave_repl_offset:17715532
- slave_priority:100
- slave_read_only:1
- connected_slaves:0
- master_repl_offset:0
- repl_backlog_active:0
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:0
- repl_backlog_histlen:0
- '));
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=127.0.0.1,port=6382,state=online,offset=12979,lag=0
- slave1:ip=127.0.0.1,port=6383,state=online,offset=12979,lag=1
- master_repl_offset:12979
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:2
- repl_backlog_histlen:12978
- '));
- $replication = new MasterSlaveReplication();
- $replication->setConnectionFactory($connFactory);
- $replication->add($slave1);
- $replication->discover();
- $this->assertCount(2, $slaves = $replication->getSlaves());
- $this->assertContainsOnlyInstancesOf('Predis\Connection\ConnectionInterface', $slaves);
- $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
- $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
- $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
- }
- /**
- * @group disconnected
- */
- public function testDiscoversReplicationConfigurationFromSlaveIfMasterFails()
- {
- $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
- $masterKO = $this->getMockConnection('tcp://127.0.0.1:7381?alias=master');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory->expects($this->at(0))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6381', 'alias' => 'master'))
- ->will($this->returnValue($master));
- $connFactory->expects($this->at(1))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6382'))
- ->will($this->returnValue($slave1));
- $connFactory->expects($this->at(2))
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6383'))
- ->will($this->returnValue($slave2));
- $masterKO->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->throwException(new Connection\ConnectionException($masterKO)));
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:slave
- master_host:127.0.0.1
- master_port:6381
- master_link_status:up
- master_last_io_seconds_ago:8
- master_sync_in_progress:0
- slave_repl_offset:17715532
- slave_priority:100
- slave_read_only:1
- connected_slaves:0
- master_repl_offset:0
- repl_backlog_active:0
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:0
- repl_backlog_histlen:0
- '));
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=127.0.0.1,port=6382,state=online,offset=12979,lag=0
- slave1:ip=127.0.0.1,port=6383,state=online,offset=12979,lag=1
- master_repl_offset:12979
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:2
- repl_backlog_histlen:12978
- '));
- $replication = new MasterSlaveReplication();
- $replication->setConnectionFactory($connFactory);
- $replication->add($masterKO);
- $replication->add($slave1);
- $replication->discover();
- $this->assertCount(2, $slaves = $replication->getSlaves());
- $this->assertContainsOnlyInstancesOf('Predis\Connection\ConnectionInterface', $slaves);
- $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
- $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
- $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
- }
- /**
- * @group disconnected
- * @expectedException \Predis\ClientException
- * @expectedMessage Automatic discovery requires a connection factory
- */
- public function testAutomaticDiscoveryRequiresConnectionFactory()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->setAutoDiscovery(true);
- }
- /**
- * @group disconnected
- */
- public function testAutomaticDiscoveryOnUnreachableServer()
- {
- $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
- $cmdExists = $command = Command\RawCommand::create('EXISTS', 'key');
- $slaveKO = $this->getMockConnection('tcp://127.0.0.1:7382?alias=slaveKO');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory->expects($this->once())
- ->method('create')
- ->with(array('host' => '127.0.0.1', 'port' => '6382'))
- ->will($this->returnValue($slave1));
- $slaveKO->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(new Connection\ConnectionException($slaveKO)));
- $slave1->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->returnValue(1));
- $master->expects($this->once())
- ->method('executeCommand')
- ->with($cmdInfo)
- ->will($this->returnValue('
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=127.0.0.1,port=6382,state=online,offset=12979,lag=0
- master_repl_offset:12979
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:2
- repl_backlog_histlen:12978
- '));
- $replication = new MasterSlaveReplication();
- $replication->setConnectionFactory($connFactory);
- $replication->setAutoDiscovery(true);
- $replication->add($master);
- $replication->add($slaveKO);
- $replication->executeCommand($cmdExists);
- }
- /**
- * @group disconnected
- */
- public function testExposesReplicationStrategy()
- {
- $replication = new MasterSlaveReplication();
- $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
- $strategy = new ReplicationStrategy();
- $replication = new MasterSlaveReplication($strategy);
- $this->assertSame($strategy, $replication->getReplicationStrategy());
- }
- /**
- * @group disconnected
- */
- public function testCanBeSerialized()
- {
- $master = $this->getMockConnection('tcp://host1?alias=master');
- $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $unserialized = unserialize(serialize($replication));
- $this->assertEquals($master, $unserialized->getConnectionById('master'));
- $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
- }
- }
|