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 PredisTestCase;
  12. use Predis\Connection;
  13. use Predis\Profile;
  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. $connection4 = $this->getMockConnection('tcp://host1:7003?alias=node4');
  79. $cluster = new PredisCluster();
  80. $cluster->add($connection1);
  81. $cluster->add($connection2);
  82. $cluster->add($connection3);
  83. $this->assertTrue($cluster->removeById(0));
  84. $this->assertTrue($cluster->removeById('node2'));
  85. $this->assertFalse($cluster->removeById('node4'));
  86. $this->assertSame(1, count($cluster));
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testConnectForcesAllConnectionsToConnect()
  92. {
  93. $connection1 = $this->getMockConnection();
  94. $connection1->expects($this->once())->method('connect');
  95. $connection2 = $this->getMockConnection();
  96. $connection2->expects($this->once())->method('connect');
  97. $cluster = new PredisCluster();
  98. $cluster->add($connection1);
  99. $cluster->add($connection2);
  100. $cluster->connect();
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testDisconnectForcesAllConnectionsToDisconnect()
  106. {
  107. $connection1 = $this->getMockConnection();
  108. $connection1->expects($this->once())->method('disconnect');
  109. $connection2 = $this->getMockConnection();
  110. $connection2->expects($this->once())->method('disconnect');
  111. $cluster = new PredisCluster();
  112. $cluster->add($connection1);
  113. $cluster->add($connection2);
  114. $cluster->disconnect();
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  120. {
  121. $connection1 = $this->getMockConnection();
  122. $connection1->expects($this->once())
  123. ->method('isConnected')
  124. ->will($this->returnValue(false));
  125. $connection2 = $this->getMockConnection();
  126. $connection2->expects($this->once())
  127. ->method('isConnected')
  128. ->will($this->returnValue(true));
  129. $cluster = new PredisCluster();
  130. $cluster->add($connection1);
  131. $cluster->add($connection2);
  132. $this->assertTrue($cluster->isConnected());
  133. }
  134. /**
  135. * @group disconnected
  136. */
  137. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  138. {
  139. $connection1 = $this->getMockConnection();
  140. $connection1->expects($this->once())
  141. ->method('isConnected')
  142. ->will($this->returnValue(false));
  143. $connection2 = $this->getMockConnection();
  144. $connection2->expects($this->once())
  145. ->method('isConnected')
  146. ->will($this->returnValue(false));
  147. $cluster = new PredisCluster();
  148. $cluster->add($connection1);
  149. $cluster->add($connection2);
  150. $this->assertFalse($cluster->isConnected());
  151. }
  152. /**
  153. * @group disconnected
  154. */
  155. public function testCanReturnAnIteratorForConnections()
  156. {
  157. $connection1 = $this->getMockConnection();
  158. $connection2 = $this->getMockConnection();
  159. $cluster = new PredisCluster();
  160. $cluster->add($connection1);
  161. $cluster->add($connection2);
  162. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  163. $connections = iterator_to_array($iterator);
  164. $this->assertSame($connection1, $connections[0]);
  165. $this->assertSame($connection2, $connections[1]);
  166. }
  167. /**
  168. * @group disconnected
  169. */
  170. public function testReturnsCorrectConnectionUsingKey()
  171. {
  172. $connection1 = $this->getMockConnection('tcp://host1:7001');
  173. $connection2 = $this->getMockConnection('tcp://host1:7002');
  174. $cluster = new PredisCluster();
  175. $cluster->add($connection1);
  176. $cluster->add($connection2);
  177. $this->assertSame($connection1, $cluster->getConnectionByKey('node01:5431'));
  178. $this->assertSame($connection2, $cluster->getConnectionByKey('node02:3212'));
  179. $this->assertSame($connection1, $cluster->getConnectionByKey('prefix:{node01:5431}'));
  180. $this->assertSame($connection2, $cluster->getConnectionByKey('prefix:{node02:3212}'));
  181. }
  182. /**
  183. * @group disconnected
  184. */
  185. public function testReturnsCorrectConnectionUsingCommandInstance()
  186. {
  187. $profile = Profile\Factory::getDefault();
  188. $connection1 = $this->getMockConnection('tcp://host1:7001');
  189. $connection2 = $this->getMockConnection('tcp://host1:7002');
  190. $cluster = new PredisCluster();
  191. $cluster->add($connection1);
  192. $cluster->add($connection2);
  193. $set = $profile->createCommand('set', array('node01:5431', 'foobar'));
  194. $get = $profile->createCommand('get', array('node01:5431'));
  195. $this->assertSame($connection1, $cluster->getConnection($set));
  196. $this->assertSame($connection1, $cluster->getConnection($get));
  197. $set = $profile->createCommand('set', array('prefix:{node01:5431}', 'foobar'));
  198. $get = $profile->createCommand('get', array('prefix:{node01:5431}'));
  199. $this->assertSame($connection1, $cluster->getConnection($set));
  200. $this->assertSame($connection1, $cluster->getConnection($get));
  201. $set = $profile->createCommand('set', array('node02:3212', 'foobar'));
  202. $get = $profile->createCommand('get', array('node02:3212'));
  203. $this->assertSame($connection2, $cluster->getConnection($set));
  204. $this->assertSame($connection2, $cluster->getConnection($get));
  205. $set = $profile->createCommand('set', array('prefix:{node02:3212}', 'foobar'));
  206. $get = $profile->createCommand('get', array('prefix:{node02:3212}'));
  207. $this->assertSame($connection2, $cluster->getConnection($set));
  208. $this->assertSame($connection2, $cluster->getConnection($get));
  209. }
  210. /**
  211. * @group disconnected
  212. * @expectedException Predis\NotSupportedException
  213. * @expectedExceptionMessage Cannot use 'PING' over clusters of connections.
  214. */
  215. public function testThrowsExceptionOnNonShardableCommand()
  216. {
  217. $ping = Profile\Factory::getDefault()->createCommand('ping');
  218. $cluster = new PredisCluster();
  219. $cluster->add($this->getMockConnection());
  220. $cluster->getConnection($ping);
  221. }
  222. /**
  223. * @group disconnected
  224. */
  225. public function testSupportsKeyHashTags()
  226. {
  227. $profile = Profile\Factory::getDefault();
  228. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  229. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  230. $cluster = new RedisCluster();
  231. $cluster->add($connection1);
  232. $cluster->add($connection2);
  233. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  234. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  235. $this->assertSame($connection1, $cluster->getConnection($set));
  236. $this->assertSame($connection1, $cluster->getConnection($get));
  237. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  238. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  239. $this->assertSame($connection1, $cluster->getConnection($set));
  240. $this->assertSame($connection1, $cluster->getConnection($get));
  241. }
  242. /**
  243. * @group disconnected
  244. */
  245. public function testWritesCommandToCorrectConnection()
  246. {
  247. $command = Profile\Factory::getDefault()->createCommand('get', array('node01:5431'));
  248. $connection1 = $this->getMockConnection('tcp://host1:7001');
  249. $connection1->expects($this->once())->method('writeRequest')->with($command);
  250. $connection2 = $this->getMockConnection('tcp://host1:7002');
  251. $connection2->expects($this->never())->method('writeRequest');
  252. $cluster = new PredisCluster();
  253. $cluster->add($connection1);
  254. $cluster->add($connection2);
  255. $cluster->writeRequest($command);
  256. }
  257. /**
  258. * @group disconnected
  259. */
  260. public function testReadsCommandFromCorrectConnection()
  261. {
  262. $command = Profile\Factory::getDefault()->createCommand('get', array('node02:3212'));
  263. $connection1 = $this->getMockConnection('tcp://host1:7001');
  264. $connection1->expects($this->never())->method('readResponse');
  265. $connection2 = $this->getMockConnection('tcp://host1:7002');
  266. $connection2->expects($this->once())->method('readResponse')->with($command);
  267. $cluster = new PredisCluster();
  268. $cluster->add($connection1);
  269. $cluster->add($connection2);
  270. $cluster->readResponse($command);
  271. }
  272. /**
  273. * @group disconnected
  274. */
  275. public function testExecutesCommandOnCorrectConnection()
  276. {
  277. $command = Profile\Factory::getDefault()->createCommand('get', array('node01:5431'));
  278. $connection1 = $this->getMockConnection('tcp://host1:7001');
  279. $connection1->expects($this->once())->method('executeCommand')->with($command);
  280. $connection2 = $this->getMockConnection('tcp://host1:7002');
  281. $connection2->expects($this->never())->method('executeCommand');
  282. $cluster = new PredisCluster();
  283. $cluster->add($connection1);
  284. $cluster->add($connection2);
  285. $cluster->executeCommand($command);
  286. }
  287. /**
  288. * @group disconnected
  289. */
  290. public function testExecuteCommandOnEachNode()
  291. {
  292. $ping = Profile\Factory::getDefault()->createCommand('ping', array());
  293. $connection1 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  294. $connection1->expects($this->once())
  295. ->method('executeCommand')
  296. ->with($ping)
  297. ->will($this->returnValue(true));
  298. $connection2 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  299. $connection2->expects($this->once())
  300. ->method('executeCommand')
  301. ->with($ping)
  302. ->will($this->returnValue(false));
  303. $cluster = new PredisCluster();
  304. $cluster->add($connection1);
  305. $cluster->add($connection2);
  306. $this->assertSame(array(true, false), $cluster->executeCommandOnNodes($ping));
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testCanBeSerialized()
  312. {
  313. $connection1 = $this->getMockConnection('tcp://host1?alias=first');
  314. $connection2 = $this->getMockConnection('tcp://host2?alias=second');
  315. $cluster = new PredisCluster();
  316. $cluster->add($connection1);
  317. $cluster->add($connection2);
  318. // We use the following line to initialize the underlying hashring.
  319. $cluster->getConnectionByKey('foo');
  320. $unserialized = unserialize(serialize($cluster));
  321. $this->assertEquals($cluster, $unserialized);
  322. }
  323. // ******************************************************************** //
  324. // ---- HELPER METHODS ------------------------------------------------ //
  325. // ******************************************************************** //
  326. /**
  327. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  328. *
  329. * @param mixed $parameters Optional parameters.
  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. }