PredisClusterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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(2, count($cluster));
  30. $this->assertSame($connection1, $cluster->getConnectionById(0));
  31. $this->assertSame($connection2, $cluster->getConnectionById(1));
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testAddingConnectionsToClusterUsesConnectionAlias()
  37. {
  38. $connection1 = $this->getMockConnection('tcp://host1:7001?alias=node1');
  39. $connection2 = $this->getMockConnection('tcp://host1:7002?alias=node2');
  40. $cluster = new PredisCluster();
  41. $cluster->add($connection1);
  42. $cluster->add($connection2);
  43. $this->assertSame(2, count($cluster));
  44. $this->assertSame($connection1, $cluster->getConnectionById('node1'));
  45. $this->assertSame($connection2, $cluster->getConnectionById('node2'));
  46. }
  47. /**
  48. * @group disconnected
  49. */
  50. public function testRemovingConnectionsFromCluster()
  51. {
  52. $connection1 = $this->getMockConnection();
  53. $connection2 = $this->getMockConnection();
  54. $connection3 = $this->getMockConnection();
  55. $cluster = new PredisCluster();
  56. $cluster->add($connection1);
  57. $cluster->add($connection2);
  58. $this->assertTrue($cluster->remove($connection1));
  59. $this->assertFalse($cluster->remove($connection3));
  60. $this->assertSame(1, count($cluster));
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testRemovingConnectionsFromClusterByAlias()
  66. {
  67. $connection1 = $this->getMockConnection();
  68. $connection2 = $this->getMockConnection('tcp://host1:7001?alias=node2');
  69. $connection3 = $this->getMockConnection('tcp://host1:7002?alias=node3');
  70. $connection4 = $this->getMockConnection('tcp://host1:7003?alias=node4');
  71. $cluster = new PredisCluster();
  72. $cluster->add($connection1);
  73. $cluster->add($connection2);
  74. $cluster->add($connection3);
  75. $this->assertTrue($cluster->removeById(0));
  76. $this->assertTrue($cluster->removeById('node2'));
  77. $this->assertFalse($cluster->removeById('node4'));
  78. $this->assertSame(1, count($cluster));
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testConnectForcesAllConnectionsToConnect()
  84. {
  85. $connection1 = $this->getMockConnection();
  86. $connection1->expects($this->once())->method('connect');
  87. $connection2 = $this->getMockConnection();
  88. $connection2->expects($this->once())->method('connect');
  89. $cluster = new PredisCluster();
  90. $cluster->add($connection1);
  91. $cluster->add($connection2);
  92. $cluster->connect();
  93. }
  94. /**
  95. * @group disconnected
  96. */
  97. public function testDisconnectForcesAllConnectionsToDisconnect()
  98. {
  99. $connection1 = $this->getMockConnection();
  100. $connection1->expects($this->once())->method('disconnect');
  101. $connection2 = $this->getMockConnection();
  102. $connection2->expects($this->once())->method('disconnect');
  103. $cluster = new PredisCluster();
  104. $cluster->add($connection1);
  105. $cluster->add($connection2);
  106. $cluster->disconnect();
  107. }
  108. /**
  109. * @group disconnected
  110. */
  111. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  112. {
  113. $connection1 = $this->getMockConnection();
  114. $connection1->expects($this->once())
  115. ->method('isConnected')
  116. ->will($this->returnValue(false));
  117. $connection2 = $this->getMockConnection();
  118. $connection2->expects($this->once())
  119. ->method('isConnected')
  120. ->will($this->returnValue(true));
  121. $cluster = new PredisCluster();
  122. $cluster->add($connection1);
  123. $cluster->add($connection2);
  124. $this->assertTrue($cluster->isConnected());
  125. }
  126. /**
  127. * @group disconnected
  128. */
  129. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  130. {
  131. $connection1 = $this->getMockConnection();
  132. $connection1->expects($this->once())
  133. ->method('isConnected')
  134. ->will($this->returnValue(false));
  135. $connection2 = $this->getMockConnection();
  136. $connection2->expects($this->once())
  137. ->method('isConnected')
  138. ->will($this->returnValue(false));
  139. $cluster = new PredisCluster();
  140. $cluster->add($connection1);
  141. $cluster->add($connection2);
  142. $this->assertFalse($cluster->isConnected());
  143. }
  144. /**
  145. * @group disconnected
  146. */
  147. public function testCanReturnAnIteratorForConnections()
  148. {
  149. $connection1 = $this->getMockConnection();
  150. $connection2 = $this->getMockConnection();
  151. $cluster = new PredisCluster();
  152. $cluster->add($connection1);
  153. $cluster->add($connection2);
  154. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  155. $connections = iterator_to_array($iterator);
  156. $this->assertSame($connection1, $connections[0]);
  157. $this->assertSame($connection2, $connections[1]);
  158. }
  159. /**
  160. * @group disconnected
  161. */
  162. public function testReturnsCorrectConnectionUsingKey()
  163. {
  164. $connection1 = $this->getMockConnection('tcp://host1:7001');
  165. $connection2 = $this->getMockConnection('tcp://host1:7002');
  166. $cluster = new PredisCluster();
  167. $cluster->add($connection1);
  168. $cluster->add($connection2);
  169. $this->assertSame($connection1, $cluster->getConnectionByKey('node01:5431'));
  170. $this->assertSame($connection2, $cluster->getConnectionByKey('node02:3212'));
  171. $this->assertSame($connection1, $cluster->getConnectionByKey('prefix:{node01:5431}'));
  172. $this->assertSame($connection2, $cluster->getConnectionByKey('prefix:{node02:3212}'));
  173. }
  174. /**
  175. * @group disconnected
  176. */
  177. public function testReturnsCorrectConnectionUsingCommandInstance()
  178. {
  179. $profile = ServerProfile::getDefault();
  180. $connection1 = $this->getMockConnection('tcp://host1:7001');
  181. $connection2 = $this->getMockConnection('tcp://host1:7002');
  182. $cluster = new PredisCluster();
  183. $cluster->add($connection1);
  184. $cluster->add($connection2);
  185. $set = $profile->createCommand('set', array('node01:5431', 'foobar'));
  186. $get = $profile->createCommand('get', array('node01:5431'));
  187. $this->assertSame($connection1, $cluster->getConnection($set));
  188. $this->assertSame($connection1, $cluster->getConnection($get));
  189. $set = $profile->createCommand('set', array('prefix:{node01:5431}', 'foobar'));
  190. $get = $profile->createCommand('get', array('prefix:{node01:5431}'));
  191. $this->assertSame($connection1, $cluster->getConnection($set));
  192. $this->assertSame($connection1, $cluster->getConnection($get));
  193. $set = $profile->createCommand('set', array('node02:3212', 'foobar'));
  194. $get = $profile->createCommand('get', array('node02:3212'));
  195. $this->assertSame($connection2, $cluster->getConnection($set));
  196. $this->assertSame($connection2, $cluster->getConnection($get));
  197. $set = $profile->createCommand('set', array('prefix:{node02:3212}', 'foobar'));
  198. $get = $profile->createCommand('get', array('prefix:{node02:3212}'));
  199. $this->assertSame($connection2, $cluster->getConnection($set));
  200. $this->assertSame($connection2, $cluster->getConnection($get));
  201. }
  202. /**
  203. * @group disconnected
  204. * @expectedException Predis\NotSupportedException
  205. * @expectedExceptionMessage Cannot send 'PING' commands to a cluster of connections
  206. */
  207. public function testThrowsExceptionOnNonShardableCommand()
  208. {
  209. $ping = ServerProfile::getDefault()->createCommand('ping');
  210. $cluster = new PredisCluster();
  211. $cluster->add($this->getMockConnection());
  212. $cluster->getConnection($ping);
  213. }
  214. /**
  215. * @group disconnected
  216. */
  217. public function testWritesCommandToCorrectConnection()
  218. {
  219. $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
  220. $connection1 = $this->getMockConnection('tcp://host1:7001');
  221. $connection1->expects($this->once())->method('writeCommand')->with($command);
  222. $connection2 = $this->getMockConnection('tcp://host1:7002');
  223. $connection2->expects($this->never())->method('writeCommand');
  224. $cluster = new PredisCluster();
  225. $cluster->add($connection1);
  226. $cluster->add($connection2);
  227. $cluster->writeCommand($command);
  228. }
  229. /**
  230. * @group disconnected
  231. */
  232. public function testReadsCommandFromCorrectConnection()
  233. {
  234. $command = ServerProfile::getDefault()->createCommand('get', array('node02:3212'));
  235. $connection1 = $this->getMockConnection('tcp://host1:7001');
  236. $connection1->expects($this->never())->method('readResponse');
  237. $connection2 = $this->getMockConnection('tcp://host1:7002');
  238. $connection2->expects($this->once())->method('readResponse')->with($command);
  239. $cluster = new PredisCluster();
  240. $cluster->add($connection1);
  241. $cluster->add($connection2);
  242. $cluster->readResponse($command);
  243. }
  244. /**
  245. * @group disconnected
  246. */
  247. public function testExecutesCommandOnCorrectConnection()
  248. {
  249. $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
  250. $connection1 = $this->getMockConnection('tcp://host1:7001');
  251. $connection1->expects($this->once())->method('executeCommand')->with($command);
  252. $connection2 = $this->getMockConnection('tcp://host1:7002');
  253. $connection2->expects($this->never())->method('executeCommand');
  254. $cluster = new PredisCluster();
  255. $cluster->add($connection1);
  256. $cluster->add($connection2);
  257. $cluster->executeCommand($command);
  258. }
  259. /**
  260. * @group disconnected
  261. */
  262. public function testCanBeSerialized()
  263. {
  264. $connection1 = $this->getMockConnection('tcp://host1?alias=first');
  265. $connection2 = $this->getMockConnection('tcp://host2?alias=second');
  266. $cluster = new PredisCluster();
  267. $cluster->add($connection1);
  268. $cluster->add($connection2);
  269. // We use the following line to initialize the underlying hashring.
  270. $cluster->getConnectionByKey('foo');
  271. $unserialized = unserialize(serialize($cluster));
  272. $this->assertEquals($cluster, $unserialized);
  273. }
  274. // ******************************************************************** //
  275. // ---- HELPER METHODS ------------------------------------------------ //
  276. // ******************************************************************** //
  277. /**
  278. * Returns a base mocked connection from Predis\Network\IConnectionSingle.
  279. *
  280. * @param mixed $parameters Optional parameters.
  281. * @return mixed
  282. */
  283. protected function getMockConnection($parameters = null)
  284. {
  285. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  286. if ($parameters) {
  287. $parameters = new ConnectionParameters($parameters);
  288. $hash = "{$parameters->host}:{$parameters->port}";
  289. $connection->expects($this->any())
  290. ->method('getParameters')
  291. ->will($this->returnValue($parameters));
  292. $connection->expects($this->any())
  293. ->method('__toString')
  294. ->will($this->returnValue($hash));
  295. }
  296. return $connection;
  297. }
  298. }