PredisClusterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Network;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\ConnectionParameters;
  13. use Predis\Profiles\ServerProfile;
  14. /**
  15. *
  16. */
  17. class PredisClusterTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testAddingConnectionsToCluster()
  23. {
  24. $connection1 = $this->getMockConnection();
  25. $connection2 = $this->getMockConnection();
  26. $cluster = new PredisCluster();
  27. $cluster->add($connection1);
  28. $cluster->add($connection2);
  29. $this->assertSame($connection1, $cluster->getConnectionById(0));
  30. $this->assertSame($connection2, $cluster->getConnectionById(1));
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testAddingConnectionsToClusterUsesConnectionAlias()
  36. {
  37. $connection1 = $this->getMockConnection('tcp://host1:7001?alias=node1');
  38. $connection2 = $this->getMockConnection('tcp://host1:7002?alias=node2');
  39. $cluster = new PredisCluster();
  40. $cluster->add($connection1);
  41. $cluster->add($connection2);
  42. $this->assertSame($connection1, $cluster->getConnectionById('node1'));
  43. $this->assertSame($connection2, $cluster->getConnectionById('node2'));
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testConnectForcesAllConnectionsToConnect()
  49. {
  50. $connection1 = $this->getMockConnection();
  51. $connection1->expects($this->once())->method('connect');
  52. $connection2 = $this->getMockConnection();
  53. $connection2->expects($this->once())->method('connect');
  54. $cluster = new PredisCluster();
  55. $cluster->add($connection1);
  56. $cluster->add($connection2);
  57. $cluster->connect();
  58. }
  59. /**
  60. * @group disconnected
  61. */
  62. public function testDisconnectForcesAllConnectionsToDisconnect()
  63. {
  64. $connection1 = $this->getMockConnection();
  65. $connection1->expects($this->once())->method('disconnect');
  66. $connection2 = $this->getMockConnection();
  67. $connection2->expects($this->once())->method('disconnect');
  68. $cluster = new PredisCluster();
  69. $cluster->add($connection1);
  70. $cluster->add($connection2);
  71. $cluster->disconnect();
  72. }
  73. /**
  74. * @group disconnected
  75. */
  76. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  77. {
  78. $connection1 = $this->getMockConnection();
  79. $connection1->expects($this->once())
  80. ->method('isConnected')
  81. ->will($this->returnValue(false));
  82. $connection2 = $this->getMockConnection();
  83. $connection2->expects($this->once())
  84. ->method('isConnected')
  85. ->will($this->returnValue(true));
  86. $cluster = new PredisCluster();
  87. $cluster->add($connection1);
  88. $cluster->add($connection2);
  89. $this->assertTrue($cluster->isConnected());
  90. }
  91. /**
  92. * @group disconnected
  93. */
  94. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  95. {
  96. $connection1 = $this->getMockConnection();
  97. $connection1->expects($this->once())
  98. ->method('isConnected')
  99. ->will($this->returnValue(false));
  100. $connection2 = $this->getMockConnection();
  101. $connection2->expects($this->once())
  102. ->method('isConnected')
  103. ->will($this->returnValue(false));
  104. $cluster = new PredisCluster();
  105. $cluster->add($connection1);
  106. $cluster->add($connection2);
  107. $this->assertFalse($cluster->isConnected());
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testCanReturnAnIteratorForConnections()
  113. {
  114. $connection1 = $this->getMockConnection();
  115. $connection2 = $this->getMockConnection();
  116. $cluster = new PredisCluster();
  117. $cluster->add($connection1);
  118. $cluster->add($connection2);
  119. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  120. $connections = iterator_to_array($iterator);
  121. $this->assertSame($connection1, $connections[0]);
  122. $this->assertSame($connection2, $connections[1]);
  123. }
  124. /**
  125. * @group disconnected
  126. */
  127. public function testReturnsCorrectConnectionUsingKey()
  128. {
  129. $connection1 = $this->getMockConnection('tcp://host1:7001');
  130. $connection2 = $this->getMockConnection('tcp://host1:7002');
  131. $cluster = new PredisCluster();
  132. $cluster->add($connection1);
  133. $cluster->add($connection2);
  134. $this->assertSame($connection1, $cluster->getConnectionByKey('node01:5431'));
  135. $this->assertSame($connection2, $cluster->getConnectionByKey('node02:3212'));
  136. $this->assertSame($connection1, $cluster->getConnectionByKey('prefix:{node01:5431}'));
  137. $this->assertSame($connection2, $cluster->getConnectionByKey('prefix:{node02:3212}'));
  138. }
  139. /**
  140. * @group disconnected
  141. */
  142. public function testReturnsCorrectConnectionUsingCommandInstance()
  143. {
  144. $profile = ServerProfile::getDefault();
  145. $connection1 = $this->getMockConnection('tcp://host1:7001');
  146. $connection2 = $this->getMockConnection('tcp://host1:7002');
  147. $cluster = new PredisCluster();
  148. $cluster->add($connection1);
  149. $cluster->add($connection2);
  150. $set = $profile->createCommand('set', array('node01:5431', 'foobar'));
  151. $get = $profile->createCommand('get', array('node01:5431'));
  152. $this->assertSame($connection1, $cluster->getConnection($set));
  153. $this->assertSame($connection1, $cluster->getConnection($get));
  154. $set = $profile->createCommand('set', array('prefix:{node01:5431}', 'foobar'));
  155. $get = $profile->createCommand('get', array('prefix:{node01:5431}'));
  156. $this->assertSame($connection1, $cluster->getConnection($set));
  157. $this->assertSame($connection1, $cluster->getConnection($get));
  158. $set = $profile->createCommand('set', array('node02:3212', 'foobar'));
  159. $get = $profile->createCommand('get', array('node02:3212'));
  160. $this->assertSame($connection2, $cluster->getConnection($set));
  161. $this->assertSame($connection2, $cluster->getConnection($get));
  162. $set = $profile->createCommand('set', array('prefix:{node02:3212}', 'foobar'));
  163. $get = $profile->createCommand('get', array('prefix:{node02:3212}'));
  164. $this->assertSame($connection2, $cluster->getConnection($set));
  165. $this->assertSame($connection2, $cluster->getConnection($get));
  166. }
  167. /**
  168. * @group disconnected
  169. * @expectedException Predis\NotSupportedException
  170. * @expectedExceptionMessage Cannot send 'PING' commands to a cluster of connections
  171. */
  172. public function testThrowsExceptionOnNonShardableCommand()
  173. {
  174. $ping = ServerProfile::getDefault()->createCommand('ping');
  175. $cluster = new PredisCluster();
  176. $cluster->add($this->getMockConnection());
  177. $cluster->getConnection($ping);
  178. }
  179. /**
  180. * @group disconnected
  181. */
  182. public function testWritesCommandToCorrectConnection()
  183. {
  184. $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
  185. $connection1 = $this->getMockConnection('tcp://host1:7001');
  186. $connection1->expects($this->once())->method('writeCommand')->with($command);
  187. $connection2 = $this->getMockConnection('tcp://host1:7002');
  188. $connection2->expects($this->never())->method('writeCommand');
  189. $cluster = new PredisCluster();
  190. $cluster->add($connection1);
  191. $cluster->add($connection2);
  192. $cluster->writeCommand($command);
  193. }
  194. /**
  195. * @group disconnected
  196. */
  197. public function testReadsCommandFromCorrectConnection()
  198. {
  199. $command = ServerProfile::getDefault()->createCommand('get', array('node02:3212'));
  200. $connection1 = $this->getMockConnection('tcp://host1:7001');
  201. $connection1->expects($this->never())->method('readResponse');
  202. $connection2 = $this->getMockConnection('tcp://host1:7002');
  203. $connection2->expects($this->once())->method('readResponse')->with($command);
  204. $cluster = new PredisCluster();
  205. $cluster->add($connection1);
  206. $cluster->add($connection2);
  207. $cluster->readResponse($command);
  208. }
  209. /**
  210. * @group disconnected
  211. */
  212. public function testExecutesCommandOnCorrectConnection()
  213. {
  214. $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
  215. $connection1 = $this->getMockConnection('tcp://host1:7001');
  216. $connection1->expects($this->once())->method('executeCommand')->with($command);
  217. $connection2 = $this->getMockConnection('tcp://host1:7002');
  218. $connection2->expects($this->never())->method('executeCommand');
  219. $cluster = new PredisCluster();
  220. $cluster->add($connection1);
  221. $cluster->add($connection2);
  222. $cluster->executeCommand($command);
  223. }
  224. // ******************************************************************** //
  225. // ---- HELPER METHODS ------------------------------------------------ //
  226. // ******************************************************************** //
  227. /**
  228. * Returns a base mocked connection from Predis\Network\IConnectionSingle.
  229. *
  230. * @param mixed $parameters Optional parameters.
  231. * @return mixed
  232. */
  233. protected function getMockConnection($parameters = null)
  234. {
  235. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  236. if ($parameters) {
  237. $parameters = new ConnectionParameters($parameters);
  238. $hash = "{$parameters->host}:{$parameters->port}";
  239. $connection->expects($this->any())
  240. ->method('getParameters')
  241. ->will($this->returnValue($parameters));
  242. $connection->expects($this->any())
  243. ->method('__toString')
  244. ->will($this->returnValue($hash));
  245. }
  246. return $connection;
  247. }
  248. }