1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432 |
- <?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://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertSame($master, $replication->getConnectionById('127.0.0.1:6379'));
- $this->assertSame($slave1, $replication->getConnectionById('127.0.0.1:6380'));
- $this->assertSame($slave2, $replication->getConnectionById('127.0.0.1:6381'));
- $this->assertSame($master, $replication->getMaster());
- $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
- }
- /**
- * @group disconnected
- */
- public function testAddingConnectionsWithoutRoleParameterDefaultsToSlaveRole()
- {
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionsFromReplication()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
- $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 testGetConnectionByIdOnEmptyReplication()
- {
- $replication = new MasterSlaveReplication();
- $this->assertNull($replication->getConnectionById('127.0.0.1:6379'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionByAlias()
- {
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6379?alias=aliased');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertSame($slave1, $replication->getConnectionByAlias('aliased'));
- $this->assertNull($replication->getConnectionByAlias('127.0.0.1:6380'));
- $this->assertNull($replication->getConnectionByAlias('unkswn'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionByAliasOnEmptyReplication()
- {
- $replication = new MasterSlaveReplication();
- $this->assertNull($replication->getConnectionByAlias('unknown'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionByRole()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($master, $replication->getConnectionByRole('master'));
- $this->assertSame($slave1, $replication->getConnectionByRole('slave'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionByRoleOnEmptyReplication()
- {
- $replication = new MasterSlaveReplication();
- $this->assertNull($replication->getConnectionByRole('master'));
- $this->assertNull($replication->getConnectionByRole('slave'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionByRoleUnknown()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $this->assertNull($replication->getConnectionByRole('unknown'));
- }
- /**
- * @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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->never())
- ->method('connect');
- $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->connect();
- $this->assertSame($master, $replication->getCurrent());
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $master
- ->expects($this->never())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('disconnect');
- $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave
- ->expects($this->once())
- ->method('disconnect');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave);
- $replication->disconnect();
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchConnection()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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 testThrowsErrorWhenSwitchingToConnectionNotInPool()
- {
- $replication = new MasterSlaveReplication();
- $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=master'));
- $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
- $unknown = $this->getMockConnection('tcp://127.0.0.1:6381');
- $replication->switchTo($unknown);
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchConnectionByInstance()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master'));
- $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $replication->switchTo($slave2);
- }
- /**
- * @group disconnected
- */
- public function testCanSwitchToMaster()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
- $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://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->switchToSlave();
- }
- /**
- * @group disconnected
- */
- public function testUsesSlavesOnReadOnlyCommands()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($slave1, $replication->getConnectionByCommand($cmd));
- $cmd = $commands->createCommand('get', array('foo'));
- $this->assertSame($slave1, $replication->getConnectionByCommand($cmd));
- }
- /**
- * @group disconnected
- */
- public function testUsesMasterOnWriteRequests()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnectionByCommand($cmd));
- $cmd = $commands->createCommand('get', array('foo'));
- $this->assertSame($master, $replication->getConnectionByCommand($cmd));
- }
- /**
- * @group disconnected
- */
- public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($master, $replication->getConnectionByCommand($cmd));
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnectionByCommand($cmd));
- }
- /**
- * @group disconnected
- */
- public function testSwitchesFromSlaveToMasterOnWriteRequests()
- {
- $commands = $this->getCommandFactory();
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave1');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($slave1, $replication->getConnectionByCommand($cmd));
- $cmd = $commands->createCommand('set', array('foo', 'bar'));
- $this->assertSame($master, $replication->getConnectionByCommand($cmd));
- $cmd = $commands->createCommand('exists', array('foo'));
- $this->assertSame($master, $replication->getConnectionByCommand($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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('writeRequest')
- ->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('readResponse')
- ->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdSet);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdWatch);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdMulti);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdEval);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdSortStore);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->never())
- ->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave&role=slave');
- $slave1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(
- new Connection\ConnectionException($slave1)
- ));
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave&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->getConnectionByAlias('slave1'));
- $this->assertSame($slave2, $replication->getConnectionByAlias('slave2'));
- }
- /**
- * @group disconnected
- */
- public function testDiscardsUnreachableSlavesAndExecutesReadOnlyCommandOnMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('key'));
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->returnValue(1));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExists)
- ->will($this->throwException(new Connection\ConnectionException($slave1)));
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
- $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('127.0.0.1:6380'));
- $this->assertNull($replication->getConnectionById('127.0.0.1:6381'));
- }
- /**
- * @group disconnected
- */
- public function testSucceedOnReadOnlyCommandAndNoConnectionSetAsMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdExists = $commands->createCommand('exists', array('key'));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6379?role=slave');
- $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://127.0.0.1:6379?role=slave');
- $slave1
- ->expects($this->never())
- ->method('executeCommand');
- $replication = new MasterSlaveReplication();
- $replication->add($slave1);
- $replication->executeCommand($cmdSet);
- }
- /**
- * @group disconnected
- */
- public function testDiscardsSlaveWhenRespondsLOADINGAndExecutesReadOnlyCommandOnNextSlave()
- {
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $master->expects($this->never())
- ->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $slave1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'EXISTS', array('key')
- ))
- ->will($this->returnValue(
- new Response\Error('LOADING')
- ));
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
- $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('127.0.0.1:6380'));
- $this->assertSame($slave2, $replication->getConnectionById('127.0.0.1:6381'));
- }
- /**
- * @group disconnected
- * @expectedException \Predis\Connection\ConnectionException
- */
- public function testFailsOnUnreachableMaster()
- {
- $commands = $this->getCommandFactory();
- $cmdSet = $commands->createCommand('set', array('key', 'value'));
- $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdSet)
- ->will($this->throwException(
- new Connection\ConnectionException($master)
- ));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master'));
- $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
- $replication->getConnectionByCommand($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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdGet);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdExistsBar);
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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://127.0.0.1:6379?role=master');
- $master
- ->expects($this->never())
- ->method('executeCommand');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $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()
- {
- $replication = new MasterSlaveReplication();
- $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=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?role=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?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory
- ->expects($this->at(0))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6381',
- 'role' => 'master',
- ))
- ->will($this->returnValue($master));
- $connFactory
- ->expects($this->at(1))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6382',
- 'role' => 'slave',
- ))
- ->will($this->returnValue($slave1));
- $connFactory
- ->expects($this->at(2))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6383',
- 'role' => 'slave',
- ))
- ->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?role=master');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory
- ->expects($this->at(0))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6381',
- 'role' => 'master',
- ))
- ->will($this->returnValue($master));
- $connFactory
- ->expects($this->at(1))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6382',
- 'role' => 'slave',
- ))
- ->will($this->returnValue($slave1));
- $connFactory
- ->expects($this->at(2))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6383',
- 'role' => 'slave',
- ))
- ->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://127.0.0.1:6379?role=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?role=slave');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
- $connFactory = $this->getMock('Predis\Connection\Factory');
- $connFactory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6382',
- 'role' => 'slave',
- ))
- ->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://127.0.0.1:6379?role=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
- $replication = new MasterSlaveReplication();
- $replication->add($master);
- $replication->add($slave1);
- $unserialized = unserialize(serialize($replication));
- $this->assertEquals($master, $unserialized->getConnectionByRole('master'));
- $this->assertEquals($slave1, $unserialized->getConnectionByRole('slave'));
- }
- }
|