PredisClusterTest.php 13 KB

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