123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356 |
- <?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;
- use Predis\Response;
- use PredisTestCase;
- /**
- *
- */
- class SentinelReplicationTest extends PredisTestCase
- {
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodGetSentinelConnectionThrowsExceptionOnEmptySentinelsPool()
- {
- $replication = $this->getReplicationConnection('svc', array());
- $replication->getSentinelConnection();
- }
- /**
- * @group disconnected
- */
- public function testParametersForSentinelConnectionShouldNotUseDatabaseAndPassword()
- {
- $replication = $this->getReplicationConnection('svc', array(
- 'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret',
- ));
- $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
- $this->assertArraySubset(array('database' => null, 'password' => null), $parameters);
- }
- /**
- * @group disconnected
- */
- public function testParametersForSentinelConnectionHaveDefaultTimeout()
- {
- $replication = $this->getReplicationConnection('svc', array(
- 'tcp://127.0.0.1:5381?alias=sentinel',
- ));
- $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
- $this->assertArrayHasKey('timeout', $parameters);
- $this->assertSame(0.100, $parameters['timeout']);
- }
- /**
- * @group disconnected
- */
- public function testParametersForSentinelConnectionCanOverrideDefaultTimeout()
- {
- $replication = $this->getReplicationConnection('svc', array(
- 'tcp://127.0.0.1:5381?alias=sentinel&timeout=1',
- ));
- $parameters = $replication
- ->getSentinelConnection()
- ->getParameters()
- ->toArray();
- $this->assertArrayHasKey('timeout', $parameters);
- $this->assertSame('1', $parameters['timeout']);
- }
- /**
- * @group disconnected
- */
- public function testConnectionParametersInstanceForSentinelConnectionIsNotModified()
- {
- $originalParameters = Connection\Parameters::create(
- 'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret'
- );
- $replication = $this->getReplicationConnection('svc', array($originalParameters));
- $parameters = $replication
- ->getSentinelConnection()
- ->getParameters();
- $this->assertSame($originalParameters, $parameters);
- $this->assertNotNull($parameters->password);
- $this->assertNotNull($parameters->database);
- }
- /**
- * @group disconnected
- */
- public function testMethodGetSentinelConnectionReturnsFirstAvailableSentinel()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
- $sentinel3 = $this->getMockSentinelConnection('tcp://127.0.0.1:5383?alias=sentinel3');
- $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2, $sentinel3));
- $this->assertSame($sentinel1, $replication->getSentinelConnection());
- }
- /**
- * @group disconnected
- */
- public function testMethodAddAttachesMasterOrSlaveNodesToReplication()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $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');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $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 testMethodRemoveDismissesMasterOrSlaveNodesFromReplication()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $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');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $this->assertTrue($replication->remove($slave1));
- $this->assertFalse($replication->remove($sentinel1));
- $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
- $this->assertCount(1, $slaves = $replication->getSlaves());
- $this->assertSame('127.0.0.1:6383', (string) $slaves[0]);
- }
- /**
- * @group disconnected
- */
- public function testMethodUpdateSentinelsFetchesSentinelNodes()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('sentinels', 'svc')
- ))
- ->will($this->returnValue(
- array(
- array(
- 'name', '127.0.0.1:5382',
- 'ip', '127.0.0.1',
- 'port', '5382',
- 'runid', 'a113aa7a0d4870a85bb22b4b605fd26eb93ed40e',
- 'flags', 'sentinel',
- ),
- array(
- 'name', '127.0.0.1:5383',
- 'ip', '127.0.0.1',
- 'port', '5383',
- 'runid', 'f53b52d281be5cdd4873700c94846af8dbe47209',
- 'flags', 'sentinel',
- ),
- )
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->updateSentinels();
- // TODO: sorry for the smell...
- $reflection = new \ReflectionProperty($replication, 'sentinels');
- $reflection->setAccessible(true);
- $expected = array(
- array('host' => '127.0.0.1', 'port' => '5381'),
- array('host' => '127.0.0.1', 'port' => '5382'),
- array('host' => '127.0.0.1', 'port' => '5383'),
- );
- $this->assertSame($sentinel1, $replication->getSentinelConnection());
- $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
- }
- /**
- * @group disconnected
- */
- public function testMethodUpdateSentinelsRemovesCurrentSentinelAndRetriesNextOneOnFailure()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('sentinels', 'svc')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
- ));
- $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
- $sentinel2
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('sentinels', 'svc')
- ))
- ->will($this->returnValue(
- array(
- array(
- 'name', '127.0.0.1:5383',
- 'ip', '127.0.0.1',
- 'port', '5383',
- 'runid', 'f53b52d281be5cdd4873700c94846af8dbe47209',
- 'flags', 'sentinel',
- ),
- )
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2));
- $replication->updateSentinels();
- // TODO: sorry for the smell...
- $reflection = new \ReflectionProperty($replication, 'sentinels');
- $reflection->setAccessible(true);
- $expected = array(
- array('host' => '127.0.0.1', 'port' => '5382'),
- array('host' => '127.0.0.1', 'port' => '5383'),
- );
- $this->assertSame($sentinel2, $replication->getSentinelConnection());
- $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodUpdateSentinelsThrowsExceptionOnNoAvailableSentinel()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('sentinels', 'svc')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->updateSentinels();
- }
- /**
- * @group disconnected
- */
- public function testMethodQuerySentinelFetchesMasterNodeSlaveNodesAndSentinelNodes()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->exactly(3))
- ->method('executeCommand')
- ->withConsecutive(
- $this->isRedisCommand('SENTINEL', array('sentinels', 'svc')),
- $this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc')),
- $this->isRedisCommand('SENTINEL', array('slaves', 'svc'))
- )
- ->will($this->onConsecutiveCalls(
- // SENTINEL sentinels svc
- array(
- array(
- 'name', '127.0.0.1:5382',
- 'ip', '127.0.0.1',
- 'port', '5382',
- 'runid', 'a113aa7a0d4870a85bb22b4b605fd26eb93ed40e',
- 'flags', 'sentinel',
- ),
- ),
- // SENTINEL get-master-addr-by-name svc
- array('127.0.0.1', '6381'),
- // SENTINEL slaves svc
- array(
- array(
- 'name', '127.0.0.1:6382',
- 'ip', '127.0.0.1',
- 'port', '6382',
- 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- array(
- 'name', '127.0.0.1:6383',
- 'ip', '127.0.0.1',
- 'port', '6383',
- 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- )
- ));
- $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
- $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');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->querySentinel();
- // TODO: sorry for the smell...
- $reflection = new \ReflectionProperty($replication, 'sentinels');
- $reflection->setAccessible(true);
- $sentinels = array(
- array('host' => '127.0.0.1', 'port' => '5381'),
- array('host' => '127.0.0.1', 'port' => '5382'),
- );
- $this->assertSame($sentinel1, $replication->getSentinelConnection());
- $this->assertSame($sentinels, array_intersect_key($sentinels, $reflection->getValue($replication)));
- $master = $replication->getMaster();
- $slaves = $replication->getSlaves();
- $this->assertSame('127.0.0.1:6381', (string) $master);
- $this->assertCount(2, $slaves);
- $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
- $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
- }
- /**
- * @group disconnected
- */
- public function testMethodGetMasterAsksSentinelForMasterOnMasterNotSet()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->at(0))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('get-master-addr-by-name', 'svc')
- ))
- ->will($this->returnValue(
- array('127.0.0.1', '6381')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodGetMasterThrowsExceptionOnNoAvailableSentinels()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('get-master-addr-by-name', 'svc')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->getMaster();
- }
- /**
- * @group disconnected
- */
- public function testMethodGetSlavesOnEmptySlavePoolAsksSentinelForSlaves()
- {
- $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(
- array(
- 'name', '127.0.0.1:6382',
- 'ip', '127.0.0.1',
- 'port', '6382',
- 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- array(
- 'name', '127.0.0.1:6383',
- 'ip', '127.0.0.1',
- 'port', '6383',
- 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- )
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $slaves = $replication->getSlaves();
- $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
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodGetSlavesThrowsExceptionOnNoAvailableSentinels()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('slaves', 'svc')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->getSlaves();
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodConnectThrowsExceptionOnConnectWithEmptySentinelsPool()
- {
- $replication = $this->getReplicationConnection('svc', array());
- $replication->connect();
- }
- /**
- * @group disconnected
- */
- public function testMethodConnectForcesConnectionToSlave()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->never())
- ->method('connect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->once())
- ->method('connect');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->connect();
- }
- /**
- * @group disconnected
- */
- public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToSlave()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('slaves', 'svc')
- ))
- ->will($this->returnValue(
- array(
- array(
- 'name', '127.0.0.1:6382',
- 'ip', '127.0.0.1',
- 'port', '6382',
- 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- )
- ));
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->never())
- ->method('connect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->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' => '6382',
- 'alias' => 'slave-127.0.0.1:6382',
- ))
- ->will($this->returnValue($slave1));
- $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
- $replication->add($master);
- $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
- */
- public function testMethodDisconnectForcesDisconnectionOnAllConnectionsInPool()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->never())
- ->method('disconnect');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->once())
- ->method('disconnect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->once())
- ->method('disconnect');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
- $slave2
- ->expects($this->once())
- ->method('disconnect');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $replication->disconnect();
- }
- /**
- * @group disconnected
- */
- public function testMethodIsConnectedReturnConnectionStatusOfCurrentConnection()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->exactly(2))
- ->method('isConnected')
- ->will($this->onConsecutiveCalls(true, false));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($slave1);
- $this->assertFalse($replication->isConnected());
- $replication->connect();
- $this->assertTrue($replication->isConnected());
- $replication->getConnectionById('slave1')->disconnect();
- $this->assertFalse($replication->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testMethodGetConnectionByIdReturnsConnectionWhenFound()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($master, $replication->getConnectionById('master'));
- $this->assertSame($slave1, $replication->getConnectionById('slave1'));
- $this->assertNull($replication->getConnectionById('unknown'));
- }
- /**
- * @group disconnected
- */
- public function testMethodSwitchToSelectsCurrentConnectionByConnectionAlias()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->once())
- ->method('connect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->never())
- ->method('connect');
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave2');
- $slave2
- ->expects($this->once())
- ->method('connect');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $replication->switchTo('master');
- $this->assertSame($master, $replication->getCurrent());
- $replication->switchTo('slave2');
- $this->assertSame($slave2, $replication->getCurrent());
- }
- /**
- * @group disconnected
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage Invalid connection or connection not found.
- */
- public function testMethodSwitchToThrowsExceptionOnConnectionNotFound()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->switchTo('unknown');
- }
- /**
- * @group disconnected
- */
- public function testMethodSwitchToMasterSelectsCurrentConnectionToMaster()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->once())
- ->method('connect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->never())
- ->method('connect');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->switchToMaster();
- $this->assertSame($master, $replication->getCurrent());
- }
- /**
- * @group disconnected
- */
- public function testMethodSwitchToSlaveSelectsCurrentConnectionToRandomSlave()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->never())
- ->method('connect');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->once())
- ->method('connect');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $replication->switchToSlave();
- $this->assertSame($slave1, $replication->getCurrent());
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionReturnsMasterForWriteCommands()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->exactly(2))
- ->method('isConnected')
- ->will($this->onConsecutiveCalls(false, true));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
- )));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($master, $replication->getConnection(
- Command\RawCommand::create('set', 'key', 'value')
- ));
- $this->assertSame($master, $replication->getConnection(
- Command\RawCommand::create('del', 'key')
- ));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionReturnsSlaveForReadOnlyCommands()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->exactly(2))
- ->method('isConnected')
- ->will($this->onConsecutiveCalls(false, true));
- $slave1
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'slave', '127.0.0.1', 9000, 'connected', 3167038,
- )));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($slave1, $replication->getConnection(
- Command\RawCommand::create('get', 'key')
- ));
- $this->assertSame($slave1, $replication->getConnection(
- Command\RawCommand::create('exists', 'key')
- ));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionSwitchesToMasterAfterWriteCommand()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->exactly(2))
- ->method('isConnected')
- ->will($this->onConsecutiveCalls(false, true));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
- )));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->exactly(1))
- ->method('isConnected')
- ->will($this->onConsecutiveCalls(false));
- $slave1
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'slave', '127.0.0.1', 9000, 'connected', 3167038,
- )));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($slave1, $replication->getConnection(
- Command\RawCommand::create('exists', 'key')
- ));
- $this->assertSame($master, $replication->getConnection(
- Command\RawCommand::create('set', 'key', 'value')
- ));
- $this->assertSame($master, $replication->getConnection(
- Command\RawCommand::create('get', 'key')
- ));
- }
- /**
- * @group disconnected
- * @expectedException Predis\Replication\RoleException
- * @expectedExceptionMessage Expected master but got slave [127.0.0.1:6381]
- */
- public function testGetConnectionThrowsExceptionOnNodeRoleMismatch()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'slave', '127.0.0.1', 9000, 'connected', 3167038,
- )));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->getConnection(Command\RawCommand::create('del', 'key'));
- }
- /**
- * @group disconnected
- */
- public function testGetConnectionReturnsMasterForReadOnlyOperationsOnUnavailableSlaves()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('slaves', 'svc')
- ))
- ->will($this->returnValue(
- array(
- array(
- 'name', '127.0.0.1:6382',
- 'ip', '127.0.0.1',
- 'port', '6382',
- 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
- 'flags', 'slave,s_down,disconnected',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- )
- ));
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ROLE'))
- ->will($this->returnValue(array(
- 'master', '0', array(),
- )));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->getConnection(Command\RawCommand::create('get', 'key'));
- }
- /**
- * @group disconnected
- */
- public function testMethodExecuteCommandSendsCommandToNodeAndReturnsResponse()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $cmdGet = Command\RawCommand::create('get', 'key');
- $cmdGetResponse = 'value';
- $cmdSet = Command\RawCommand::create('set', 'key', 'value');
- $cmdSetResponse = Response\Status::get('OK');
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SET', array('key', $cmdGetResponse)
- ))
- ->will($this->returnValue($cmdSetResponse));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $slave1
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('key')
- ))
- ->will($this->returnValue($cmdGetResponse));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame($cmdGetResponse, $replication->executeCommand($cmdGet));
- $this->assertSame($cmdSetResponse, $replication->executeCommand($cmdSet));
- }
- /**
- * @group disconnected
- */
- public function testMethodExecuteCommandRetriesReadOnlyCommandOnNextSlaveOnFailure()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('slaves', 'svc')
- ))
- ->will($this->returnValue(
- array(
- array(
- 'name', '127.0.0.1:6383',
- 'ip', '127.0.0.1',
- 'port', '6383',
- 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
- 'flags', 'slave',
- 'master-host', '127.0.0.1',
- 'master-port', '6381',
- ),
- )
- ));
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
- $slave1
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $slave1
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('key')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($slave1, 'Unknown connection error [127.0.0.1:6382]')
- ));
- $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
- $slave2
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $slave2
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('key')
- ))
- ->will($this->returnValue('value'));
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6383',
- 'alias' => 'slave-127.0.0.1:6383',
- ))
- ->will($this->returnValue($slave2));
- $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
- $replication->add($master);
- $replication->add($slave1);
- $this->assertSame('value', $replication->executeCommand(
- Command\RawCommand::create('get', 'key')
- ));
- }
- /**
- * @group disconnected
- */
- public function testMethodExecuteCommandRetriesWriteCommandOnNewMasterOnFailure()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('get-master-addr-by-name', 'svc')
- ))
- ->will($this->returnValue(
- array('127.0.0.1', '6391')
- ));
- $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $masterOld
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $masterOld
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'DEL', array('key')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $masterNew = $this->getMockConnection('tcp://127.0.0.1:6391?alias=master');
- $masterNew
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $masterNew
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'DEL', array('key')
- ))
- ->will($this->returnValue(1));
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6391',
- 'alias' => 'master',
- ))
- ->will($this->returnValue($masterNew));
- $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
- $replication->add($masterOld);
- $this->assertSame(1, $replication->executeCommand(
- Command\RawCommand::create('del', 'key')
- ));
- }
- /**
- * @group disconnected
- * @expectedException Predis\Response\ServerException
- * @expectedExceptionMessage ERR No such master with that name
- */
- public function testMethodExecuteCommandThrowsExceptionOnUnknownServiceName()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('get-master-addr-by-name', 'svc')
- ))
- ->will($this->returnValue(null));
- $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $masterOld
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $masterOld
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'DEL', array('key')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($masterOld);
- $replication->executeCommand(
- Command\RawCommand::create('del', 'key')
- );
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage No sentinel server available for autodiscovery.
- */
- public function testMethodExecuteCommandThrowsExceptionOnConnectionFailureAndNoAvailableSentinels()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $sentinel1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'SENTINEL', array('get-master-addr-by-name', 'svc')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
- ));
- $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
- $master
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $master
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'DEL', array('key')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($master, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $replication = $this->getReplicationConnection('svc', array($sentinel1));
- $replication->add($master);
- $replication->executeCommand(
- Command\RawCommand::create('del', 'key')
- );
- }
- /**
- * @group disconnected
- */
- public function testMethodGetReplicationStrategyReturnsInstance()
- {
- $strategy = new Replication\ReplicationStrategy();
- $factory = new Connection\Factory();
- $replication = new SentinelReplication(
- 'svc', array('tcp://127.0.0.1:5381?alias=sentinel1'), $factory, $strategy
- );
- $this->assertSame($strategy, $replication->getReplicationStrategy());
- }
- /**
- * @group disconnected
- */
- public function testMethodSerializeCanSerializeWholeObject()
- {
- $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
- $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');
- $strategy = new Replication\ReplicationStrategy();
- $factory = new Connection\Factory();
- $replication = new SentinelReplication('svc', array($sentinel1), $factory, $strategy);
- $replication->add($master);
- $replication->add($slave1);
- $replication->add($slave2);
- $unserialized = unserialize(serialize($replication));
- $this->assertEquals($master, $unserialized->getConnectionById('master'));
- $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
- $this->assertEquals($master, $unserialized->getConnectionById('slave2'));
- $this->assertEquals($strategy, $unserialized->getReplicationStrategy());
- }
- // ******************************************************************** //
- // ---- HELPER METHODS ------------------------------------------------ //
- // ******************************************************************** //
- /**
- * Creates a new instance of replication connection.
- *
- * @param string $service Name of the service
- * @param array $sentinels Array of sentinels
- * @param ConnectionFactoryInterface|null $factory Optional connection factory instance.
- *
- * @return SentinelReplication
- */
- protected function getReplicationConnection($service, $sentinels, Connection\FactoryInterface $factory = null)
- {
- $factory = $factory ?: new Connection\Factory();
- $replication = new SentinelReplication($service, $sentinels, $factory);
- $replication->setRetryWait(0);
- return $replication;
- }
- /**
- * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
- *
- * @param mixed $parameters Optional parameters.
- *
- * @return mixed
- */
- protected function getMockSentinelConnection($parameters = null)
- {
- $connection = $this->getMockConnection($parameters);
- return $connection;
- }
- }
|