123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?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\Network;
- use \PHPUnit_Framework_TestCase as StandardTestCase;
- use Predis\ConnectionParameters;
- use Predis\Profiles\ServerProfile;
- /**
- *
- */
- class PredisClusterTest extends StandardTestCase
- {
- /**
- * @group disconnected
- */
- public function testAddingConnectionsToCluster()
- {
- $connection1 = $this->getMockConnection();
- $connection2 = $this->getMockConnection();
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame(2, count($cluster));
- $this->assertSame($connection1, $cluster->getConnectionById(0));
- $this->assertSame($connection2, $cluster->getConnectionById(1));
- }
- /**
- * @group disconnected
- */
- public function testAddingConnectionsToClusterUsesConnectionAlias()
- {
- $connection1 = $this->getMockConnection('tcp://host1:7001?alias=node1');
- $connection2 = $this->getMockConnection('tcp://host1:7002?alias=node2');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame(2, count($cluster));
- $this->assertSame($connection1, $cluster->getConnectionById('node1'));
- $this->assertSame($connection2, $cluster->getConnectionById('node2'));
- }
- /**
- * @group disconnected
- */
- public function testRemovingConnectionsFromCluster()
- {
- $connection1 = $this->getMockConnection();
- $connection2 = $this->getMockConnection();
- $connection3 = $this->getMockConnection();
- $cluster = new PredisCluster();
- $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();
- $connection2 = $this->getMockConnection('tcp://host1:7001?alias=node2');
- $connection3 = $this->getMockConnection('tcp://host1:7002?alias=node3');
- $connection4 = $this->getMockConnection('tcp://host1:7003?alias=node4');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->add($connection3);
- $this->assertTrue($cluster->removeById(0));
- $this->assertTrue($cluster->removeById('node2'));
- $this->assertFalse($cluster->removeById('node4'));
- $this->assertSame(1, count($cluster));
- }
- /**
- * @group disconnected
- */
- public function testConnectForcesAllConnectionsToConnect()
- {
- $connection1 = $this->getMockConnection();
- $connection1->expects($this->once())->method('connect');
- $connection2 = $this->getMockConnection();
- $connection2->expects($this->once())->method('connect');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->connect();
- }
- /**
- * @group disconnected
- */
- public function testDisconnectForcesAllConnectionsToDisconnect()
- {
- $connection1 = $this->getMockConnection();
- $connection1->expects($this->once())->method('disconnect');
- $connection2 = $this->getMockConnection();
- $connection2->expects($this->once())->method('disconnect');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->disconnect();
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
- {
- $connection1 = $this->getMockConnection();
- $connection1->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $connection2 = $this->getMockConnection();
- $connection2->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(true));
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertTrue($cluster->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
- {
- $connection1 = $this->getMockConnection();
- $connection1->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $connection2 = $this->getMockConnection();
- $connection2->expects($this->once())
- ->method('isConnected')
- ->will($this->returnValue(false));
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertFalse($cluster->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testCanReturnAnIteratorForConnections()
- {
- $connection1 = $this->getMockConnection();
- $connection2 = $this->getMockConnection();
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
- $connections = iterator_to_array($iterator);
- $this->assertSame($connection1, $connections[0]);
- $this->assertSame($connection2, $connections[1]);
- }
- /**
- * @group disconnected
- */
- public function testReturnsCorrectConnectionUsingKey()
- {
- $connection1 = $this->getMockConnection('tcp://host1:7001');
- $connection2 = $this->getMockConnection('tcp://host1:7002');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $this->assertSame($connection1, $cluster->getConnectionByKey('node01:5431'));
- $this->assertSame($connection2, $cluster->getConnectionByKey('node02:3212'));
- $this->assertSame($connection1, $cluster->getConnectionByKey('prefix:{node01:5431}'));
- $this->assertSame($connection2, $cluster->getConnectionByKey('prefix:{node02:3212}'));
- }
- /**
- * @group disconnected
- */
- public function testReturnsCorrectConnectionUsingCommandInstance()
- {
- $profile = ServerProfile::getDefault();
- $connection1 = $this->getMockConnection('tcp://host1:7001');
- $connection2 = $this->getMockConnection('tcp://host1:7002');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $set = $profile->createCommand('set', array('node01:5431', 'foobar'));
- $get = $profile->createCommand('get', array('node01:5431'));
- $this->assertSame($connection1, $cluster->getConnection($set));
- $this->assertSame($connection1, $cluster->getConnection($get));
- $set = $profile->createCommand('set', array('prefix:{node01:5431}', 'foobar'));
- $get = $profile->createCommand('get', array('prefix:{node01:5431}'));
- $this->assertSame($connection1, $cluster->getConnection($set));
- $this->assertSame($connection1, $cluster->getConnection($get));
- $set = $profile->createCommand('set', array('node02:3212', 'foobar'));
- $get = $profile->createCommand('get', array('node02:3212'));
- $this->assertSame($connection2, $cluster->getConnection($set));
- $this->assertSame($connection2, $cluster->getConnection($get));
- $set = $profile->createCommand('set', array('prefix:{node02:3212}', 'foobar'));
- $get = $profile->createCommand('get', array('prefix:{node02:3212}'));
- $this->assertSame($connection2, $cluster->getConnection($set));
- $this->assertSame($connection2, $cluster->getConnection($get));
- }
- /**
- * @group disconnected
- * @expectedException Predis\NotSupportedException
- * @expectedExceptionMessage Cannot send 'PING' commands to a cluster of connections
- */
- public function testThrowsExceptionOnNonShardableCommand()
- {
- $ping = ServerProfile::getDefault()->createCommand('ping');
- $cluster = new PredisCluster();
- $cluster->add($this->getMockConnection());
- $cluster->getConnection($ping);
- }
- /**
- * @group disconnected
- */
- public function testWritesCommandToCorrectConnection()
- {
- $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
- $connection1 = $this->getMockConnection('tcp://host1:7001');
- $connection1->expects($this->once())->method('writeCommand')->with($command);
- $connection2 = $this->getMockConnection('tcp://host1:7002');
- $connection2->expects($this->never())->method('writeCommand');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->writeCommand($command);
- }
- /**
- * @group disconnected
- */
- public function testReadsCommandFromCorrectConnection()
- {
- $command = ServerProfile::getDefault()->createCommand('get', array('node02:3212'));
- $connection1 = $this->getMockConnection('tcp://host1:7001');
- $connection1->expects($this->never())->method('readResponse');
- $connection2 = $this->getMockConnection('tcp://host1:7002');
- $connection2->expects($this->once())->method('readResponse')->with($command);
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->readResponse($command);
- }
- /**
- * @group disconnected
- */
- public function testExecutesCommandOnCorrectConnection()
- {
- $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
- $connection1 = $this->getMockConnection('tcp://host1:7001');
- $connection1->expects($this->once())->method('executeCommand')->with($command);
- $connection2 = $this->getMockConnection('tcp://host1:7002');
- $connection2->expects($this->never())->method('executeCommand');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- $cluster->executeCommand($command);
- }
- /**
- * @group disconnected
- */
- public function testCanBeSerialized()
- {
- $connection1 = $this->getMockConnection('tcp://host1?alias=first');
- $connection2 = $this->getMockConnection('tcp://host2?alias=second');
- $cluster = new PredisCluster();
- $cluster->add($connection1);
- $cluster->add($connection2);
- // We use the following line to initialize the underlying hashring.
- $cluster->getConnectionByKey('foo');
- $unserialized = unserialize(serialize($cluster));
- $this->assertEquals($cluster, $unserialized);
- }
- // ******************************************************************** //
- // ---- HELPER METHODS ------------------------------------------------ //
- // ******************************************************************** //
- /**
- * Returns a base mocked connection from Predis\Network\IConnectionSingle.
- *
- * @param mixed $parameters Optional parameters.
- * @return mixed
- */
- protected function getMockConnection($parameters = null)
- {
- $connection = $this->getMock('Predis\Network\IConnectionSingle');
- if ($parameters) {
- $parameters = new ConnectionParameters($parameters);
- $hash = "{$parameters->host}:{$parameters->port}";
- $connection->expects($this->any())
- ->method('getParameters')
- ->will($this->returnValue($parameters));
- $connection->expects($this->any())
- ->method('__toString')
- ->will($this->returnValue($hash));
- }
- return $connection;
- }
- }
|