123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- <?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\Cluster;
- use Predis\Command;
- use Predis\Connection;
- use Predis\Response;
- use PredisTestCase;
- /**
- *
- */
- class RedisClusterTest extends PredisTestCase
- {
- /**
- * @group disconnected
- */
- public function testAcceptsCustomConnectionFactory()
- {
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $cluster = new RedisCluster($factory);
- $this->assertSame($factory, $cluster->getConnectionFactory());
- }
- /**
- * @group disconnected
- */
- public function testUsesRedisClusterStrategyByDefault()
- {
- $cluster = new RedisCluster(new Connection\Factory());
- $this->assertInstanceOf('Predis\Cluster\RedisStrategy', $cluster->getClusterStrategy());
- }
- /**
- * @group disconnected
- */
- public function testAcceptsCustomClusterStrategy()
- {
- $strategy = $this->getMock('Predis\Cluster\StrategyInterface');
- $cluster = new RedisCluster(new Connection\Factory(), $strategy);
- $this->assertSame($strategy, $cluster->getClusterStrategy());
- }
- /**
- * @group disconnected
- */
- public function testAddingConnectionsToCluster()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame(2, count($cluster));
- $this->assertSame($connection1, $cluster->getConnectionById('127.0.0.1:6379'));
- $this->assertSame($connection2, $cluster->getConnectionById('127.0.0.1:6380'));
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionsFromCluster()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6371');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertTrue($cluster->remove($connection1));
- $this->assertFalse($cluster->remove($connection3));
- $this->assertSame(1, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionsFromClusterByAlias()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertTrue($cluster->removeById('127.0.0.1:6380'));
- $this->assertFalse($cluster->removeById('127.0.0.1:6390'));
- $this->assertSame(1, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testCountReturnsNumberOfConnectionsInPool()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertSame(3, count($cluster));
- $cluster->remove($connection3);
- $this->assertSame(2, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testConnectPicksRandomConnection()
- {
- $connect1 = false;
- $connect2 = false;
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->any())
- ->method('connect')
- ->will($this->returnCallback(function () use (&$connect1) {
- $connect1 = true;
- }));
- $connection1
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnCallback(function () use (&$connect1) {
- return $connect1;
- }));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->any())
- ->method('connect')
- ->will($this->returnCallback(function () use (&$connect2) {
- $connect2 = true;
- }));
- $connection2
- ->expects($this->any())
- ->method('isConnected')
- ->will($this->returnCallback(function () use (&$connect2) {
- return $connect2;
- }));
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->connect();
- $this->assertTrue($cluster->isConnected());
- if ($connect1) {
- $this->assertTrue($connect1);
- $this->assertFalse($connect2);
- } else {
- $this->assertFalse($connect1);
- $this->assertTrue($connect2);
- }
- }
- /**
- * @group disconnected
- */
- public function testDisconnectForcesAllConnectionsToDisconnect()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('disconnect');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->once())
- ->method('disconnect');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->disconnect();
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertTrue($cluster->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertFalse($cluster->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testGetIteratorReturnsConnectionsMappedInSlotsMapWhenUseClusterSlotsIsDisabled()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
- $connection4 = $this->getMockConnection('tcp://127.0.0.1:6384');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->add($connection4);
- $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
- $connections = iterator_to_array($iterator);
- $this->assertCount(3, $connections);
- $this->assertSame($connection1, $connections[0]);
- $this->assertSame($connection2, $connections[1]);
- $this->assertSame($connection3, $connections[2]);
- }
- /**
- * @group disconnected
- */
- public function testGetIteratorReturnsConnectionsMappedInSlotsMapFetchedFromRedisCluster()
- {
- $slotsmap = array(
- array(0, 5460, array('127.0.0.1', 6381), array()),
- array(5461, 10921, array('127.0.0.1', 6383), array()),
- array(10922, 16383, array('127.0.0.1', 6384), array()),
- );
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($slotsmap));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383');
- $connection4 = $this->getMockConnection('tcp://127.0.0.1:6384');
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->at(0))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6383',
- ))
- ->will($this->returnValue($connection3));
- $factory
- ->expects($this->at(1))
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6384',
- ))
- ->will($this->returnValue($connection4));
- // TODO: I'm not sure about mocking a protected method, but it'll do for now
- $cluster = $this->getMock('Predis\Connection\Cluster\RedisCluster', array('getRandomConnection'), array($factory));
- $cluster
- ->expects($this->once())
- ->method('getRandomConnection')
- ->will($this->returnValue($connection1));
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->useClusterSlots(true);
- $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
- $connections = iterator_to_array($iterator);
- $this->assertCount(3, $connections);
- $this->assertSame($connection1, $connections[0]);
- $this->assertSame($connection3, $connections[1]);
- $this->assertSame($connection4, $connections[2]);
- }
- /**
- * @group disconnected
- */
- public function testCanAssignConnectionsToCustomSlots()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->setSlots(0, 1364, '127.0.0.1:6379');
- $cluster->setSlots(1365, 2729, '127.0.0.1:6380');
- $cluster->setSlots(2730, 4095, '127.0.0.1:6381');
- $expectedMap = array_merge(
- array_fill(0, 1365, '127.0.0.1:6379'),
- array_fill(1364, 1365, '127.0.0.1:6380'),
- array_fill(2729, 1366, '127.0.0.1:6381')
- );
- $this->assertSame($expectedMap, $cluster->getSlotsMap());
- }
- /**
- * @group disconnected
- */
- public function testAddingConnectionResetsSlotsMap()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->setSlots(0, 4095, '127.0.0.1:6379');
- $this->assertSame(array_fill(0, 4096, '127.0.0.1:6379'), $cluster->getSlotsMap());
- $cluster->add($connection2);
- $this->assertEmpty($cluster->getSlotsMap());
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionResetsSlotsMap()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->setSlots(0, 2047, '127.0.0.1:6379');
- $cluster->setSlots(2048, 4095, '127.0.0.1:6380');
- $expectedMap = array_merge(
- array_fill(0, 2048, '127.0.0.1:6379'),
- array_fill(2048, 2048, '127.0.0.1:6380')
- );
- $this->assertSame($expectedMap, $cluster->getSlotsMap());
- $cluster->remove($connection1);
- $this->assertEmpty($cluster->getSlotsMap());
- }
- /**
- * @group disconnected
- */
- public function testCanAssignConnectionsToRangeOfSlotsFromParameters()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-10921');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-16383');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->buildSlotsMap();
- $expectedMap = array_merge(
- array_fill(0, 5461, '127.0.0.1:6379'),
- array_fill(5460, 5461, '127.0.0.1:6380'),
- array_fill(10921, 5462, '127.0.0.1:6381')
- );
- $actualMap = $cluster->getSlotsMap();
- ksort($actualMap);
- $this->assertSame($expectedMap, $actualMap);
- }
- /**
- * @group disconnected
- */
- public function testCanAssignConnectionsToSingleSlotOrRangesOfSlotsFromParameters()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460,5500-5600,11000');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-5499,5600-10921');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-10999,11001-16383');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->buildSlotsMap();
- $expectedMap = array_merge(
- array_fill(0, 5461, '127.0.0.1:6379'),
- array_fill(5460, 39, '127.0.0.1:6380'),
- array_fill(5499, 101, '127.0.0.1:6379'),
- array_fill(5599, 5321, '127.0.0.1:6380'),
- array_fill(10921, 78, '127.0.0.1:6381'),
- array_fill(11000, 1, '127.0.0.1:6379'),
- array_fill(11000, 5383, '127.0.0.1:6381')
- );
- $actualMap = $cluster->getSlotsMap();
- ksort($actualMap);
- $this->assertSame($expectedMap, $actualMap);
- }
- /**
- * @group disconnected
- */
- public function testReturnsCorrectConnectionUsingSlotID()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
- $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
- $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
- $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
- $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
- }
- /**
- * @group disconnected
- */
- public function testReturnsCorrectConnectionUsingCommandInstance()
- {
- $commands = $this->getCommandFactory();
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $set = $commands->createCommand('set', array('node:1001', 'foobar'));
- $get = $commands->createCommand('get', array('node:1001'));
- $this->assertSame($connection1, $cluster->getConnection($set));
- $this->assertSame($connection1, $cluster->getConnection($get));
- $set = $commands->createCommand('set', array('node:1048', 'foobar'));
- $get = $commands->createCommand('get', array('node:1048'));
- $this->assertSame($connection2, $cluster->getConnection($set));
- $this->assertSame($connection2, $cluster->getConnection($get));
- $set = $commands->createCommand('set', array('node:1082', 'foobar'));
- $get = $commands->createCommand('get', array('node:1082'));
- $this->assertSame($connection3, $cluster->getConnection($set));
- $this->assertSame($connection3, $cluster->getConnection($get));
- }
- /**
- * @group disconnected
- */
- public function testWritesCommandToCorrectConnection()
- {
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('writeRequest')
- ->with($command);
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->never())
- ->method('writeRequest');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->writeRequest($command);
- }
- /**
- * @group disconnected
- */
- public function testReadsCommandFromCorrectConnection()
- {
- $command = $this->getCommandFactory()->createCommand('get', array('node:1050'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->never())
- ->method('readResponse');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->once())
- ->method('readResponse')
- ->with($command);
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->readResponse($command);
- }
- /**
- * @group disconnected
- */
- public function testRetriesExecutingCommandOnConnectionFailureOnlyAfterFetchingNewSlotsMap()
- {
- $slotsmap = array(
- array(0, 5460, array('127.0.0.1', 9381), array()),
- array(5461, 10921, array('127.0.0.1', 6382), array()),
- array(10922, 16383, array('127.0.0.1', 6383), array()),
- );
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:1001')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
- $connection2
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($slotsmap));
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
- $connection3
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($slotsmap));
- $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381');
- $connection4
- ->expects($this->at(0))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:1001')
- ))
- ->will($this->returnValue('value:1001'));
- $connection4
- ->expects($this->at(1))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:5001')
- ))
- ->will($this->returnValue('value:5001'));
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '9381',
- ))
- ->will($this->returnValue($connection4));
- $cluster = new RedisCluster($factory);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertSame('value:1001', $cluster->executeCommand(
- Command\RawCommand::create('get', 'node:1001')
- ));
- $this->assertSame('value:5001', $cluster->executeCommand(
- Command\RawCommand::create('get', 'node:5001')
- ));
- }
- /**
- * @group disconnected
- */
- public function testRetriesExecutingCommandOnConnectionFailureButDoNotAskSlotsMapWhenDisabled()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5500');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:1001')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5501-11000');
- $connection2
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:1001')
- ))
- ->will($this->returnValue(
- new Response\Error('MOVED 1970 127.0.0.1:9381')
- ));
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=11101-16383');
- $connection3
- ->expects($this->never())
- ->method('executeCommand');
- $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381');
- $connection4
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'GET', array('node:1001')
- ))
- ->will($this->returnValue('value:1001'));
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '9381',
- ))
- ->will($this->returnValue($connection4));
- // TODO: I'm not sure about mocking a protected method, but it'll do for now
- $cluster = $this->getMock('Predis\Connection\Cluster\RedisCluster', array('getRandomConnection'), array($factory));
- $cluster
- ->expects($this->never())
- ->method('getRandomConnection');
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertSame('value:1001', $cluster->executeCommand(
- Command\RawCommand::create('get', 'node:1001')
- ));
- }
- /**
- * @group disconnected
- * @expectedException \Predis\ClientException
- * @expectedExceptionMessage No connections available in the pool
- */
- public function testThrowsClientExceptionWhenExecutingCommandWithEmptyPool()
- {
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->never())
- ->method('create');
- $cluster = new RedisCluster($factory);
- $cluster->executeCommand(
- Command\RawCommand::create('get', 'node:1001')
- );
- }
- /**
- * @group disconnected
- */
- public function testAskSlotsMapReturnEmptyArrayOnEmptyConnectionsPool()
- {
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->never())
- ->method('create');
- $cluster = new RedisCluster($factory);
- $this->assertEmpty($cluster->askSlotsMap());
- }
- /**
- * @group disconnected
- */
- public function testAskSlotsMapRetriesOnDifferentNodeOnConnectionFailure()
- {
- $slotsmap = array(
- array(0, 5460, array('127.0.0.1', 9381), array()),
- array(5461, 10921, array('127.0.0.1', 6382), array()),
- array(10922, 16383, array('127.0.0.1', 6383), array()),
- );
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
- $connection2
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection2, 'Unknown connection error [127.0.0.1:6383]')
- ));
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
- $connection3
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($slotsmap));
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->never())
- ->method('create');
- // TODO: I'm not sure about mocking a protected method, but it'll do for now
- $cluster = $this->getMock('Predis\Connection\Cluster\RedisCluster', array('getRandomConnection'), array($factory));
- $cluster
- ->expects($this->exactly(3))
- ->method('getRandomConnection')
- ->will($this->onConsecutiveCalls($connection1, $connection2, $connection3));
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertCount(16384, $cluster->askSlotsMap());
- }
- /**
- * @group disconnected
- * @expectedException \Predis\Connection\ConnectionException
- * @expectedExceptionMessage Unknown connection error [127.0.0.1:6382]
- */
- public function testAskSlotsMapHonorsRetryLimitOnMultipleConnectionFailures()
- {
- $slotsmap = array(
- array(0, 5460, array('127.0.0.1', 9381), array()),
- array(5461, 10921, array('127.0.0.1', 6382), array()),
- array(10922, 16383, array('127.0.0.1', 6383), array()),
- );
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
- $connection1
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
- ));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
- $connection2
- ->expects($this->any())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->throwException(
- new Connection\ConnectionException($connection2, 'Unknown connection error [127.0.0.1:6382]')
- ));
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
- $connection3
- ->expects($this->never())
- ->method('executeCommand');
- $factory = $this->getMock('Predis\Connection\FactoryInterface');
- $factory
- ->expects($this->never())
- ->method('create');
- // TODO: I'm not sure about mocking a protected method, but it'll do for now
- $cluster = $this->getMock('Predis\Connection\Cluster\RedisCluster', array('getRandomConnection'), array($factory));
- $cluster
- ->expects($this->exactly(2))
- ->method('getRandomConnection')
- ->will($this->onConsecutiveCalls($connection1, $connection2));
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->setRetryLimit(1);
- $cluster->askSlotsMap();
- }
- /**
- * @group disconnected
- */
- public function testSupportsKeyHashTags()
- {
- $commands = $this->getCommandFactory();
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $set = $commands->createCommand('set', array('{node:1001}:foo', 'foobar'));
- $get = $commands->createCommand('get', array('{node:1001}:foo'));
- $this->assertSame($connection1, $cluster->getConnection($set));
- $this->assertSame($connection1, $cluster->getConnection($get));
- $set = $commands->createCommand('set', array('{node:1001}:bar', 'foobar'));
- $get = $commands->createCommand('get', array('{node:1001}:bar'));
- $this->assertSame($connection1, $cluster->getConnection($set));
- $this->assertSame($connection1, $cluster->getConnection($get));
- }
- /**
- * @group disconnected
- */
- public function testAskResponseWithConnectionInPool()
- {
- $askResponse = new Response\Error('ASK 1970 127.0.0.1:6380');
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->exactly(2))
- ->method('executeCommand')
- ->with($command)
- ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($this->isRedisCommand('ASKING'));
- $connection2
- ->expects($this->at(3))
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue('foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory
- ->expects($this->never())
- ->method('create');
- $cluster = new RedisCluster($factory);
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame(2, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testAskResponseWithConnectionNotInPool()
- {
- $askResponse = new Response\Error('ASK 1970 127.0.0.1:6381');
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->exactly(2))
- ->method('executeCommand')
- ->with($command)
- ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->never())
- ->method('executeCommand');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $connection3
- ->expects($this->at(0))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'ASKING'
- ));
- $connection3
- ->expects($this->at(1))
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue('foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6381',
- ))
- ->will($this->returnValue($connection3));
- $cluster = new RedisCluster($factory);
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame(2, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testMovedResponseWithConnectionInPool()
- {
- $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6380');
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->exactly(1))
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue($movedResponse));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->exactly(2))
- ->method('executeCommand')
- ->with($command)
- ->will($this->onConsecutiveCalls('foobar', 'foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory->expects($this->never())->method('create');
- $cluster = new RedisCluster($factory);
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame(2, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testMovedResponseWithConnectionNotInPool()
- {
- $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6381');
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue($movedResponse));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->never())
- ->method('executeCommand');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
- $connection3
- ->expects($this->exactly(2))
- ->method('executeCommand')
- ->with($command)
- ->will($this->onConsecutiveCalls('foobar', 'foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6381',
- ))
- ->will($this->returnValue($connection3));
- $cluster = new RedisCluster($factory);
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame('foobar', $cluster->executeCommand($command));
- $this->assertSame(3, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testParseIPv6AddresseAndPortPairInRedirectionPayload()
- {
- $movedResponse = new Response\Error('MOVED 1970 2001:db8:0:f101::2:6379');
- $command = $this->getCommandFactory()->createCommand('get', array('node:1001'));
- $connection1 = $this->getMockConnection('tcp://[2001:db8:0:f101::1]:6379');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue($movedResponse));
- $connection2 = $this->getMockConnection('tcp://[2001:db8:0:f101::2]:6379');
- $connection2
- ->expects($this->once())
- ->method('executeCommand')
- ->with($command)
- ->will($this->returnValue('foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '2001:db8:0:f101::2',
- 'port' => '6379',
- ))
- ->will($this->returnValue($connection2));
- $cluster = new RedisCluster($factory);
- $cluster->useClusterSlots(false);
- $cluster->add($connection1);
- $cluster->executeCommand($command);
- }
- /**
- * @group disconnected
- */
- public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
- {
- $response = array(
- array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
- array(3072, 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
- array(6144, 7167, array('', 6384), array('10.1.0.52', 6384)),
- array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
- array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
- array(1024, 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
- array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
- array(5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
- array(0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
- array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
- array(4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
- array(9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
- array(8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
- array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
- array(2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
- array(7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
- );
- $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($response));
- $factory = $this->getMock('Predis\Connection\Factory');
- $cluster = new RedisCluster($factory);
- $cluster->add($connection1);
- $cluster->askSlotsMap();
- $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
- }
- /**
- * @group disconnected
- */
- public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault()
- {
- $cmdGET = Command\RawCommand::create('GET', 'node:1001');
- $rspMOVED = new Response\Error('MOVED 1970 127.0.0.1:6380');
- $rspSlotsArray = array(
- array(0, 8191, array('127.0.0.1', 6379)),
- array(8192, 16383, array('127.0.0.1', 6380)),
- );
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
- $connection1
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdGET)
- ->will($this->returnValue($rspMOVED));
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
- $connection2
- ->expects($this->at(0))
- ->method('executeCommand')
- ->with($this->isRedisCommand(
- 'CLUSTER', array('SLOTS')
- ))
- ->will($this->returnValue($rspSlotsArray));
- $connection2
- ->expects($this->at(2))
- ->method('executeCommand')
- ->with($cmdGET)
- ->will($this->returnValue('foobar'));
- $factory = $this->getMock('Predis\Connection\Factory');
- $factory
- ->expects($this->once())
- ->method('create')
- ->with(array(
- 'host' => '127.0.0.1',
- 'port' => '6380',
- ))
- ->will($this->returnValue($connection2));
- $cluster = new RedisCluster($factory);
- $cluster->add($connection1);
- $this->assertSame('foobar', $cluster->executeCommand($cmdGET));
- $this->assertSame(2, count($cluster));
- }
- /**
- * @group disconnected
- * @expectedException \Predis\NotSupportedException
- * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
- */
- public function testThrowsExceptionOnNonSupportedCommand()
- {
- $ping = $this->getCommandFactory()->createCommand('ping');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
- $cluster->getConnection($ping);
- }
- /**
- * @medium
- * @group disconnected
- */
- public function testCanBeSerialized()
- {
- $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
- $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
- $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
- $cluster = new RedisCluster(new Connection\Factory());
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $cluster->buildSlotsMap();
- $unserialized = unserialize(serialize($cluster));
- $this->assertEquals($cluster, $unserialized);
- }
- }
|