PredisClusterTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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\Aggregate;
  11. use Predis\Connection;
  12. use Predis\Profile;
  13. use PredisTestCase;
  14. /**
  15. *
  16. */
  17. class PredisClusterTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testExposesCommandHashStrategy()
  23. {
  24. $cluster = new PredisCluster();
  25. $this->assertInstanceOf('Predis\Cluster\PredisStrategy', $cluster->getClusterStrategy());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testAddingConnectionsToCluster()
  31. {
  32. $connection1 = $this->getMockConnection();
  33. $connection2 = $this->getMockConnection();
  34. $cluster = new PredisCluster();
  35. $cluster->add($connection1);
  36. $cluster->add($connection2);
  37. $this->assertSame(2, count($cluster));
  38. $this->assertSame($connection1, $cluster->getConnectionById(0));
  39. $this->assertSame($connection2, $cluster->getConnectionById(1));
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testAddingConnectionsToClusterUsesConnectionAlias()
  45. {
  46. $connection1 = $this->getMockConnection('tcp://host1:7001?alias=node1');
  47. $connection2 = $this->getMockConnection('tcp://host1:7002?alias=node2');
  48. $cluster = new PredisCluster();
  49. $cluster->add($connection1);
  50. $cluster->add($connection2);
  51. $this->assertSame(2, count($cluster));
  52. $this->assertSame($connection1, $cluster->getConnectionById('node1'));
  53. $this->assertSame($connection2, $cluster->getConnectionById('node2'));
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testRemovingConnectionsFromCluster()
  59. {
  60. $connection1 = $this->getMockConnection();
  61. $connection2 = $this->getMockConnection();
  62. $connection3 = $this->getMockConnection();
  63. $cluster = new PredisCluster();
  64. $cluster->add($connection1);
  65. $cluster->add($connection2);
  66. $this->assertTrue($cluster->remove($connection1));
  67. $this->assertFalse($cluster->remove($connection3));
  68. $this->assertSame(1, count($cluster));
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testRemovingConnectionsFromClusterByAlias()
  74. {
  75. $connection1 = $this->getMockConnection();
  76. $connection2 = $this->getMockConnection('tcp://host1:7001?alias=node2');
  77. $connection3 = $this->getMockConnection('tcp://host1:7002?alias=node3');
  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 = Profile\Factory::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' over clusters of connections.
  213. */
  214. public function testThrowsExceptionOnNonShardableCommand()
  215. {
  216. $ping = Profile\Factory::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 testSupportsKeyHashTags()
  225. {
  226. $profile = Profile\Factory::getDefault();
  227. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  228. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  229. $cluster = new PredisCluster();
  230. $cluster->add($connection1);
  231. $cluster->add($connection2);
  232. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  233. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  234. $this->assertSame($connection1, $cluster->getConnection($set));
  235. $this->assertSame($connection1, $cluster->getConnection($get));
  236. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  237. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  238. $this->assertSame($connection1, $cluster->getConnection($set));
  239. $this->assertSame($connection1, $cluster->getConnection($get));
  240. }
  241. /**
  242. * @group disconnected
  243. */
  244. public function testWritesCommandToCorrectConnection()
  245. {
  246. $command = Profile\Factory::getDefault()->createCommand('get', array('node01:5431'));
  247. $connection1 = $this->getMockConnection('tcp://host1:7001');
  248. $connection1->expects($this->once())->method('writeRequest')->with($command);
  249. $connection2 = $this->getMockConnection('tcp://host1:7002');
  250. $connection2->expects($this->never())->method('writeRequest');
  251. $cluster = new PredisCluster();
  252. $cluster->add($connection1);
  253. $cluster->add($connection2);
  254. $cluster->writeRequest($command);
  255. }
  256. /**
  257. * @group disconnected
  258. */
  259. public function testReadsCommandFromCorrectConnection()
  260. {
  261. $command = Profile\Factory::getDefault()->createCommand('get', array('node02:3212'));
  262. $connection1 = $this->getMockConnection('tcp://host1:7001');
  263. $connection1->expects($this->never())->method('readResponse');
  264. $connection2 = $this->getMockConnection('tcp://host1:7002');
  265. $connection2->expects($this->once())->method('readResponse')->with($command);
  266. $cluster = new PredisCluster();
  267. $cluster->add($connection1);
  268. $cluster->add($connection2);
  269. $cluster->readResponse($command);
  270. }
  271. /**
  272. * @group disconnected
  273. */
  274. public function testExecutesCommandOnCorrectConnection()
  275. {
  276. $command = Profile\Factory::getDefault()->createCommand('get', array('node01:5431'));
  277. $connection1 = $this->getMockConnection('tcp://host1:7001');
  278. $connection1->expects($this->once())->method('executeCommand')->with($command);
  279. $connection2 = $this->getMockConnection('tcp://host1:7002');
  280. $connection2->expects($this->never())->method('executeCommand');
  281. $cluster = new PredisCluster();
  282. $cluster->add($connection1);
  283. $cluster->add($connection2);
  284. $cluster->executeCommand($command);
  285. }
  286. /**
  287. * @group disconnected
  288. */
  289. public function testExecuteCommandOnEachNode()
  290. {
  291. $ping = Profile\Factory::getDefault()->createCommand('ping', array());
  292. $connection1 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  293. $connection1->expects($this->once())
  294. ->method('executeCommand')
  295. ->with($ping)
  296. ->will($this->returnValue(true));
  297. $connection2 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  298. $connection2->expects($this->once())
  299. ->method('executeCommand')
  300. ->with($ping)
  301. ->will($this->returnValue(false));
  302. $cluster = new PredisCluster();
  303. $cluster->add($connection1);
  304. $cluster->add($connection2);
  305. $this->assertSame(array(true, false), $cluster->executeCommandOnNodes($ping));
  306. }
  307. /**
  308. * @group disconnected
  309. */
  310. public function testCanBeSerialized()
  311. {
  312. $connection1 = $this->getMockConnection('tcp://host1?alias=first');
  313. $connection2 = $this->getMockConnection('tcp://host2?alias=second');
  314. $cluster = new PredisCluster();
  315. $cluster->add($connection1);
  316. $cluster->add($connection2);
  317. // We use the following line to initialize the underlying hashring.
  318. $cluster->getConnectionByKey('foo');
  319. $unserialized = unserialize(serialize($cluster));
  320. $this->assertEquals($cluster, $unserialized);
  321. }
  322. // ******************************************************************** //
  323. // ---- HELPER METHODS ------------------------------------------------ //
  324. // ******************************************************************** //
  325. /**
  326. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  327. *
  328. * @param mixed $parameters Optional parameters.
  329. *
  330. * @return mixed
  331. */
  332. protected function getMockConnection($parameters = null)
  333. {
  334. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  335. if ($parameters) {
  336. $parameters = Connection\Parameters::create($parameters);
  337. $hash = "{$parameters->host}:{$parameters->port}";
  338. $connection->expects($this->any())
  339. ->method('getParameters')
  340. ->will($this->returnValue($parameters));
  341. $connection->expects($this->any())
  342. ->method('__toString')
  343. ->will($this->returnValue($hash));
  344. }
  345. return $connection;
  346. }
  347. }