RedisClusterTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\ResponseError;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class RedisClusterTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testExposesCommandHashStrategy()
  23. {
  24. $cluster = new RedisCluster();
  25. $this->assertInstanceOf('Predis\Cluster\RedisClusterHashStrategy', $cluster->getCommandHashStrategy());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testAddingConnectionsToCluster()
  31. {
  32. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  33. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  34. $cluster = new RedisCluster();
  35. $cluster->add($connection1);
  36. $cluster->add($connection2);
  37. $this->assertSame(2, count($cluster));
  38. $this->assertSame($connection1, $cluster->getConnectionById('127.0.0.1:6379'));
  39. $this->assertSame($connection2, $cluster->getConnectionById('127.0.0.1:6380'));
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testRemovingConnectionsFromCluster()
  45. {
  46. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  47. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  48. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6371');
  49. $cluster = new RedisCluster();
  50. $cluster->add($connection1);
  51. $cluster->add($connection2);
  52. $this->assertTrue($cluster->remove($connection1));
  53. $this->assertFalse($cluster->remove($connection3));
  54. $this->assertSame(1, count($cluster));
  55. }
  56. /**
  57. * @group disconnected
  58. */
  59. public function testRemovingConnectionsFromClusterByAlias()
  60. {
  61. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  62. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  63. $cluster = new RedisCluster();
  64. $cluster->add($connection1);
  65. $cluster->add($connection2);
  66. $this->assertTrue($cluster->removeById('127.0.0.1:6380'));
  67. $this->assertFalse($cluster->removeById('127.0.0.1:6390'));
  68. $this->assertSame(1, count($cluster));
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testCountReturnsNumberOfConnectionsInPool()
  74. {
  75. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  76. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  77. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  78. $cluster = new RedisCluster();
  79. $cluster->add($connection1);
  80. $cluster->add($connection2);
  81. $cluster->add($connection3);
  82. $this->assertSame(3, count($cluster));
  83. $cluster->remove($connection3);
  84. $this->assertSame(2, count($cluster));
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testConnectForcesAllConnectionsToConnect()
  90. {
  91. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  92. $connection1->expects($this->once())->method('connect');
  93. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  94. $connection2->expects($this->once())->method('connect');
  95. $cluster = new RedisCluster();
  96. $cluster->add($connection1);
  97. $cluster->add($connection2);
  98. $cluster->connect();
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testDisconnectForcesAllConnectionsToDisconnect()
  104. {
  105. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  106. $connection1->expects($this->once())->method('disconnect');
  107. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  108. $connection2->expects($this->once())->method('disconnect');
  109. $cluster = new RedisCluster();
  110. $cluster->add($connection1);
  111. $cluster->add($connection2);
  112. $cluster->disconnect();
  113. }
  114. /**
  115. * @group disconnected
  116. */
  117. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  118. {
  119. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  120. $connection1->expects($this->once())
  121. ->method('isConnected')
  122. ->will($this->returnValue(false));
  123. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  124. $connection2->expects($this->once())
  125. ->method('isConnected')
  126. ->will($this->returnValue(true));
  127. $cluster = new RedisCluster();
  128. $cluster->add($connection1);
  129. $cluster->add($connection2);
  130. $this->assertTrue($cluster->isConnected());
  131. }
  132. /**
  133. * @group disconnected
  134. */
  135. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  136. {
  137. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  138. $connection1->expects($this->once())
  139. ->method('isConnected')
  140. ->will($this->returnValue(false));
  141. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  142. $connection2->expects($this->once())
  143. ->method('isConnected')
  144. ->will($this->returnValue(false));
  145. $cluster = new RedisCluster();
  146. $cluster->add($connection1);
  147. $cluster->add($connection2);
  148. $this->assertFalse($cluster->isConnected());
  149. }
  150. /**
  151. * @group disconnected
  152. */
  153. public function testCanReturnAnIteratorForConnections()
  154. {
  155. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  156. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  157. $cluster = new RedisCluster();
  158. $cluster->add($connection1);
  159. $cluster->add($connection2);
  160. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  161. $connections = iterator_to_array($iterator);
  162. $this->assertSame($connection1, $connections[0]);
  163. $this->assertSame($connection2, $connections[1]);
  164. }
  165. /**
  166. * @group disconnected
  167. */
  168. public function testCanAssignConnectionsToCustomSlots()
  169. {
  170. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  171. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  172. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  173. $cluster = new RedisCluster();
  174. $cluster->add($connection1);
  175. $cluster->add($connection2);
  176. $cluster->add($connection3);
  177. $cluster->setSlots(0, 1364, '127.0.0.1:6379');
  178. $cluster->setSlots(1365, 2729, '127.0.0.1:6380');
  179. $cluster->setSlots(2730, 4095, '127.0.0.1:6381');
  180. $expectedMap = array_merge(
  181. array_fill(0, 1365, '127.0.0.1:6379'),
  182. array_fill(1364, 1365, '127.0.0.1:6380'),
  183. array_fill(2729, 1366, '127.0.0.1:6381')
  184. );
  185. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  186. }
  187. /**
  188. * @group disconnected
  189. */
  190. public function testAddingConnectionResetsSlotsMap()
  191. {
  192. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  193. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  194. $cluster = new RedisCluster();
  195. $cluster->add($connection1);
  196. $cluster->setSlots(0, 4095, '127.0.0.1:6379');
  197. $this->assertSame(array_fill(0, 4096, '127.0.0.1:6379'), $cluster->getSlotsMap());
  198. $cluster->add($connection2);
  199. $this->assertEmpty($cluster->getSlotsMap());
  200. }
  201. /**
  202. * @group disconnected
  203. */
  204. public function testRemovingConnectionResetsSlotsMap()
  205. {
  206. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  207. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  208. $cluster = new RedisCluster();
  209. $cluster->add($connection1);
  210. $cluster->add($connection2);
  211. $cluster->setSlots(0, 2047, '127.0.0.1:6379');
  212. $cluster->setSlots(2048, 4095, '127.0.0.1:6380');
  213. $expectedMap = array_merge(
  214. array_fill(0, 2048, '127.0.0.1:6379'),
  215. array_fill(2048, 2048, '127.0.0.1:6380')
  216. );
  217. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  218. $cluster->remove($connection1);
  219. $this->assertEmpty($cluster->getSlotsMap());
  220. }
  221. /**
  222. * @group disconnected
  223. */
  224. public function testCanAssignConnectionsToCustomSlotsFromParameters()
  225. {
  226. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  227. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  228. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  229. $cluster = new RedisCluster();
  230. $cluster->add($connection1);
  231. $cluster->add($connection2);
  232. $cluster->add($connection3);
  233. $expectedMap = array_merge(
  234. array_fill(0, 1365, '127.0.0.1:6379'),
  235. array_fill(1364, 1365, '127.0.0.1:6380'),
  236. array_fill(2729, 1366, '127.0.0.1:6381')
  237. );
  238. $cluster->buildSlotsMap();
  239. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  240. }
  241. /**
  242. * @group disconnected
  243. */
  244. public function testReturnsCorrectConnectionUsingSlotID()
  245. {
  246. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  247. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  248. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  249. $cluster = new RedisCluster();
  250. $cluster->add($connection1);
  251. $cluster->add($connection2);
  252. $cluster->add($connection3);
  253. $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
  254. $this->assertSame($connection2, $cluster->getConnectionBySlot(1365));
  255. $this->assertSame($connection3, $cluster->getConnectionBySlot(2730));
  256. $cluster->setSlots(1365, 3000, '127.0.0.1:6380');
  257. $this->assertSame($connection2, $cluster->getConnectionBySlot(2730));
  258. }
  259. /**
  260. * @group disconnected
  261. */
  262. public function testReturnsCorrectConnectionUsingCommandInstance()
  263. {
  264. $profile = ServerProfile::getDefault();
  265. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  266. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  267. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  268. $cluster = new RedisCluster();
  269. $cluster->add($connection1);
  270. $cluster->add($connection2);
  271. $cluster->add($connection3);
  272. $set = $profile->createCommand('set', array('node:1024', 'foobar'));
  273. $get = $profile->createCommand('get', array('node:1024'));
  274. $this->assertSame($connection1, $cluster->getConnection($set));
  275. $this->assertSame($connection1, $cluster->getConnection($get));
  276. $set = $profile->createCommand('set', array('node:1048', 'foobar'));
  277. $get = $profile->createCommand('get', array('node:1048'));
  278. $this->assertSame($connection2, $cluster->getConnection($set));
  279. $this->assertSame($connection2, $cluster->getConnection($get));
  280. $set = $profile->createCommand('set', array('node:1082', 'foobar'));
  281. $get = $profile->createCommand('get', array('node:1082'));
  282. $this->assertSame($connection3, $cluster->getConnection($set));
  283. $this->assertSame($connection3, $cluster->getConnection($get));
  284. }
  285. /**
  286. * @group disconnected
  287. */
  288. public function testWritesCommandToCorrectConnection()
  289. {
  290. $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
  291. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  292. $connection1->expects($this->once())->method('writeCommand')->with($command);
  293. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  294. $connection2->expects($this->never())->method('writeCommand');
  295. $cluster = new RedisCluster();
  296. $cluster->add($connection1);
  297. $cluster->add($connection2);
  298. $cluster->writeCommand($command);
  299. }
  300. /**
  301. * @group disconnected
  302. */
  303. public function testReadsCommandFromCorrectConnection()
  304. {
  305. $command = ServerProfile::getDefault()->createCommand('get', array('node:1048'));
  306. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  307. $connection1->expects($this->never())->method('readResponse');
  308. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  309. $connection2->expects($this->once())->method('readResponse')->with($command);
  310. $cluster = new RedisCluster();
  311. $cluster->add($connection1);
  312. $cluster->add($connection2);
  313. $cluster->readResponse($command);
  314. }
  315. /**
  316. * @group disconnected
  317. */
  318. public function testDoesNotSupportKeyTags()
  319. {
  320. $profile = ServerProfile::getDefault();
  321. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  322. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  323. $cluster = new RedisCluster();
  324. $cluster->add($connection1);
  325. $cluster->add($connection2);
  326. $set = $profile->createCommand('set', array('{node:1024}:foo', 'foobar'));
  327. $get = $profile->createCommand('get', array('{node:1024}:foo'));
  328. $this->assertSame($connection1, $cluster->getConnection($set));
  329. $this->assertSame($connection1, $cluster->getConnection($get));
  330. $set = $profile->createCommand('set', array('{node:1024}:bar', 'foobar'));
  331. $get = $profile->createCommand('get', array('{node:1024}:bar'));
  332. $this->assertSame($connection2, $cluster->getConnection($set));
  333. $this->assertSame($connection2, $cluster->getConnection($get));
  334. }
  335. /**
  336. * @group disconnected
  337. */
  338. public function testAskResponseWithConnectionInPool()
  339. {
  340. $askResponse = new ResponseError('ASK 373 127.0.0.1:6380');
  341. $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
  342. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  343. $connection1->expects($this->exactly(2))
  344. ->method('executeCommand')
  345. ->with($command)
  346. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  347. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  348. $connection2->expects($this->exactly(1))
  349. ->method('executeCommand')
  350. ->with($command)
  351. ->will($this->returnValue('foobar'));
  352. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  353. $factory->expects($this->never())->method('create');
  354. $cluster = new RedisCluster($factory);
  355. $cluster->add($connection1);
  356. $cluster->add($connection2);
  357. $this->assertSame('foobar', $cluster->executeCommand($command));
  358. $this->assertSame('foobar', $cluster->executeCommand($command));
  359. $this->assertSame(2, count($cluster));
  360. }
  361. /**
  362. * @group disconnected
  363. */
  364. public function testAskResponseWithConnectionNotInPool()
  365. {
  366. $askResponse = new ResponseError('ASK 373 127.0.0.1:6381');
  367. $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
  368. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  369. $connection1->expects($this->exactly(2))
  370. ->method('executeCommand')
  371. ->with($command)
  372. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  373. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  374. $connection2->expects($this->never())
  375. ->method('executeCommand');
  376. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  377. $connection3->expects($this->once())
  378. ->method('executeCommand')
  379. ->with($command)
  380. ->will($this->returnValue('foobar'));
  381. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  382. $factory->expects($this->once())
  383. ->method('create')
  384. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  385. ->will($this->returnValue($connection3));
  386. $cluster = new RedisCluster($factory);
  387. $cluster->add($connection1);
  388. $cluster->add($connection2);
  389. $this->assertSame('foobar', $cluster->executeCommand($command));
  390. $this->assertSame('foobar', $cluster->executeCommand($command));
  391. $this->assertSame(2, count($cluster));
  392. }
  393. /**
  394. * @group disconnected
  395. */
  396. public function testMovedResponseWithConnectionInPool()
  397. {
  398. $movedResponse = new ResponseError('MOVED 373 127.0.0.1:6380');
  399. $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
  400. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  401. $connection1->expects($this->exactly(1))
  402. ->method('executeCommand')
  403. ->with($command)
  404. ->will($this->returnValue($movedResponse));
  405. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  406. $connection2->expects($this->exactly(2))
  407. ->method('executeCommand')
  408. ->with($command)
  409. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  410. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  411. $factory->expects($this->never())->method('create');
  412. $cluster = new RedisCluster($factory);
  413. $cluster->add($connection1);
  414. $cluster->add($connection2);
  415. $this->assertSame('foobar', $cluster->executeCommand($command));
  416. $this->assertSame('foobar', $cluster->executeCommand($command));
  417. $this->assertSame(2, count($cluster));
  418. }
  419. /**
  420. * @group disconnected
  421. */
  422. public function testMovedResponseWithConnectionNotInPool()
  423. {
  424. $movedResponse = new ResponseError('MOVED 373 127.0.0.1:6381');
  425. $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
  426. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  427. $connection1->expects($this->once())
  428. ->method('executeCommand')
  429. ->with($command)
  430. ->will($this->returnValue($movedResponse));
  431. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  432. $connection2->expects($this->never())
  433. ->method('executeCommand');
  434. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  435. $connection3->expects($this->exactly(2))
  436. ->method('executeCommand')
  437. ->with($command)
  438. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  439. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  440. $factory->expects($this->once())
  441. ->method('create')
  442. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  443. ->will($this->returnValue($connection3));
  444. $cluster = new RedisCluster($factory);
  445. $cluster->add($connection1);
  446. $cluster->add($connection2);
  447. $this->assertSame('foobar', $cluster->executeCommand($command));
  448. $this->assertSame('foobar', $cluster->executeCommand($command));
  449. $this->assertSame(3, count($cluster));
  450. }
  451. /**
  452. * @group disconnected
  453. * @expectedException Predis\NotSupportedException
  454. * @expectedExceptionMessage Cannot use PING with redis-cluster
  455. */
  456. public function testThrowsExceptionOnNonSupportedCommand()
  457. {
  458. $ping = ServerProfile::getDefault()->createCommand('ping');
  459. $cluster = new RedisCluster();
  460. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  461. $cluster->getConnection($ping);
  462. }
  463. /**
  464. * @group disconnected
  465. */
  466. public function testCanBeSerialized()
  467. {
  468. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  469. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  470. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  471. $cluster = new RedisCluster();
  472. $cluster->add($connection1);
  473. $cluster->add($connection2);
  474. $cluster->add($connection3);
  475. $cluster->buildSlotsMap();
  476. $unserialized = unserialize(serialize($cluster));
  477. $this->assertEquals($cluster, $unserialized);
  478. }
  479. // ******************************************************************** //
  480. // ---- HELPER METHODS ------------------------------------------------ //
  481. // ******************************************************************** //
  482. /**
  483. * Returns a base mocked connection from Predis\Connection\SingleConnectionInterface.
  484. *
  485. * @param mixed $parameters Optional parameters.
  486. * @return mixed
  487. */
  488. protected function getMockConnection($parameters = null)
  489. {
  490. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  491. if ($parameters) {
  492. $parameters = new ConnectionParameters($parameters);
  493. $hash = "{$parameters->host}:{$parameters->port}";
  494. $connection->expects($this->any())
  495. ->method('getParameters')
  496. ->will($this->returnValue($parameters));
  497. $connection->expects($this->any())
  498. ->method('__toString')
  499. ->will($this->returnValue($hash));
  500. }
  501. return $connection;
  502. }
  503. }