RedisClusterTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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\Command;
  13. use Predis\Connection;
  14. use Predis\Profile;
  15. use Predis\Response;
  16. /**
  17. *
  18. */
  19. class RedisClusterTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. */
  24. public function testAcceptsCustomConnectionFactory()
  25. {
  26. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  27. $cluster = new RedisCluster($factory);
  28. $this->assertSame($factory, $cluster->getConnectionFactory());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testUsesRedisClusterStrategyByDefault()
  34. {
  35. $cluster = new RedisCluster();
  36. $this->assertInstanceOf('Predis\Cluster\RedisStrategy', $cluster->getClusterStrategy());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testAcceptsCustomClusterStrategy()
  42. {
  43. $strategy = $this->getMock('Predis\Cluster\StrategyInterface');
  44. $cluster = new RedisCluster(null, $strategy);
  45. $this->assertSame($strategy, $cluster->getClusterStrategy());
  46. }
  47. /**
  48. * @group disconnected
  49. */
  50. public function testAddingConnectionsToCluster()
  51. {
  52. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  53. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  54. $cluster = new RedisCluster();
  55. $cluster->add($connection1);
  56. $cluster->add($connection2);
  57. $this->assertSame(2, count($cluster));
  58. $this->assertSame($connection1, $cluster->getConnectionById('127.0.0.1:6379'));
  59. $this->assertSame($connection2, $cluster->getConnectionById('127.0.0.1:6380'));
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testRemovingConnectionsFromCluster()
  65. {
  66. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  67. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  68. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6371');
  69. $cluster = new RedisCluster();
  70. $cluster->add($connection1);
  71. $cluster->add($connection2);
  72. $this->assertTrue($cluster->remove($connection1));
  73. $this->assertFalse($cluster->remove($connection3));
  74. $this->assertSame(1, count($cluster));
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testRemovingConnectionsFromClusterByAlias()
  80. {
  81. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  82. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  83. $cluster = new RedisCluster();
  84. $cluster->add($connection1);
  85. $cluster->add($connection2);
  86. $this->assertTrue($cluster->removeById('127.0.0.1:6380'));
  87. $this->assertFalse($cluster->removeById('127.0.0.1:6390'));
  88. $this->assertSame(1, count($cluster));
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testCountReturnsNumberOfConnectionsInPool()
  94. {
  95. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  96. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  97. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  98. $cluster = new RedisCluster();
  99. $cluster->add($connection1);
  100. $cluster->add($connection2);
  101. $cluster->add($connection3);
  102. $this->assertSame(3, count($cluster));
  103. $cluster->remove($connection3);
  104. $this->assertSame(2, count($cluster));
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testConnectPicksRandomConnection()
  110. {
  111. $connect1 = false;
  112. $connect2 = false;
  113. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  114. $connection1->expects($this->any())
  115. ->method('connect')
  116. ->will($this->returnCallback(function () use (&$connect1) {
  117. $connect1 = true;
  118. }));
  119. $connection1->expects($this->any())
  120. ->method('isConnected')
  121. ->will($this->returnCallback(function () use (&$connect1) {
  122. return $connect1;
  123. }));
  124. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  125. $connection2->expects($this->any())
  126. ->method('connect')
  127. ->will($this->returnCallback(function () use (&$connect2) {
  128. $connect2 = true;
  129. }));
  130. $connection2->expects($this->any())
  131. ->method('isConnected')
  132. ->will($this->returnCallback(function () use (&$connect2) {
  133. return $connect2;
  134. }));
  135. $cluster = new RedisCluster();
  136. $cluster->add($connection1);
  137. $cluster->add($connection2);
  138. $cluster->connect();
  139. $this->assertTrue($cluster->isConnected());
  140. if ($connect1) {
  141. $this->assertTrue($connect1);
  142. $this->assertFalse($connect2);
  143. } else {
  144. $this->assertFalse($connect1);
  145. $this->assertTrue($connect2);
  146. }
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testDisconnectForcesAllConnectionsToDisconnect()
  152. {
  153. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  154. $connection1->expects($this->once())->method('disconnect');
  155. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  156. $connection2->expects($this->once())->method('disconnect');
  157. $cluster = new RedisCluster();
  158. $cluster->add($connection1);
  159. $cluster->add($connection2);
  160. $cluster->disconnect();
  161. }
  162. /**
  163. * @group disconnected
  164. */
  165. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  166. {
  167. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  168. $connection1->expects($this->once())
  169. ->method('isConnected')
  170. ->will($this->returnValue(false));
  171. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  172. $connection2->expects($this->once())
  173. ->method('isConnected')
  174. ->will($this->returnValue(true));
  175. $cluster = new RedisCluster();
  176. $cluster->add($connection1);
  177. $cluster->add($connection2);
  178. $this->assertTrue($cluster->isConnected());
  179. }
  180. /**
  181. * @group disconnected
  182. */
  183. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  184. {
  185. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  186. $connection1->expects($this->once())
  187. ->method('isConnected')
  188. ->will($this->returnValue(false));
  189. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  190. $connection2->expects($this->once())
  191. ->method('isConnected')
  192. ->will($this->returnValue(false));
  193. $cluster = new RedisCluster();
  194. $cluster->add($connection1);
  195. $cluster->add($connection2);
  196. $this->assertFalse($cluster->isConnected());
  197. }
  198. /**
  199. * @group disconnected
  200. */
  201. public function testCanReturnAnIteratorForConnections()
  202. {
  203. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  204. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  205. $cluster = new RedisCluster();
  206. $cluster->add($connection1);
  207. $cluster->add($connection2);
  208. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  209. $connections = iterator_to_array($iterator);
  210. $this->assertSame($connection1, $connections[0]);
  211. $this->assertSame($connection2, $connections[1]);
  212. }
  213. /**
  214. * @group disconnected
  215. */
  216. public function testCanAssignConnectionsToCustomSlots()
  217. {
  218. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  219. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  220. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  221. $cluster = new RedisCluster();
  222. $cluster->add($connection1);
  223. $cluster->add($connection2);
  224. $cluster->add($connection3);
  225. $cluster->setSlots(0, 1364, '127.0.0.1:6379');
  226. $cluster->setSlots(1365, 2729, '127.0.0.1:6380');
  227. $cluster->setSlots(2730, 4095, '127.0.0.1:6381');
  228. $expectedMap = array_merge(
  229. array_fill(0, 1365, '127.0.0.1:6379'),
  230. array_fill(1364, 1365, '127.0.0.1:6380'),
  231. array_fill(2729, 1366, '127.0.0.1:6381')
  232. );
  233. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  234. }
  235. /**
  236. * @group disconnected
  237. */
  238. public function testAddingConnectionResetsSlotsMap()
  239. {
  240. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  241. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  242. $cluster = new RedisCluster();
  243. $cluster->add($connection1);
  244. $cluster->setSlots(0, 4095, '127.0.0.1:6379');
  245. $this->assertSame(array_fill(0, 4096, '127.0.0.1:6379'), $cluster->getSlotsMap());
  246. $cluster->add($connection2);
  247. $this->assertEmpty($cluster->getSlotsMap());
  248. }
  249. /**
  250. * @group disconnected
  251. */
  252. public function testRemovingConnectionResetsSlotsMap()
  253. {
  254. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  255. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  256. $cluster = new RedisCluster();
  257. $cluster->add($connection1);
  258. $cluster->add($connection2);
  259. $cluster->setSlots(0, 2047, '127.0.0.1:6379');
  260. $cluster->setSlots(2048, 4095, '127.0.0.1:6380');
  261. $expectedMap = array_merge(
  262. array_fill(0, 2048, '127.0.0.1:6379'),
  263. array_fill(2048, 2048, '127.0.0.1:6380')
  264. );
  265. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  266. $cluster->remove($connection1);
  267. $this->assertEmpty($cluster->getSlotsMap());
  268. }
  269. /**
  270. * @group disconnected
  271. */
  272. public function testCanAssignConnectionsToCustomSlotsFromParameters()
  273. {
  274. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460');
  275. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-10921');
  276. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-16383');
  277. $cluster = new RedisCluster();
  278. $cluster->add($connection1);
  279. $cluster->add($connection2);
  280. $cluster->add($connection3);
  281. $expectedMap = array_merge(
  282. array_fill(0, 5461, '127.0.0.1:6379'),
  283. array_fill(5460, 5461, '127.0.0.1:6380'),
  284. array_fill(10921, 5462, '127.0.0.1:6381')
  285. );
  286. $cluster->buildSlotsMap();
  287. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  288. }
  289. /**
  290. * @group disconnected
  291. */
  292. public function testReturnsCorrectConnectionUsingSlotID()
  293. {
  294. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  295. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  296. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  297. $cluster = new RedisCluster();
  298. $cluster->add($connection1);
  299. $cluster->add($connection2);
  300. $cluster->add($connection3);
  301. $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
  302. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  303. $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
  304. $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
  305. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  306. }
  307. /**
  308. * @group disconnected
  309. */
  310. public function testReturnsCorrectConnectionUsingCommandInstance()
  311. {
  312. $profile = Profile\Factory::getDefault();
  313. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  314. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  315. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  316. $cluster = new RedisCluster();
  317. $cluster->add($connection1);
  318. $cluster->add($connection2);
  319. $cluster->add($connection3);
  320. $set = $profile->createCommand('set', array('node:1001', 'foobar'));
  321. $get = $profile->createCommand('get', array('node:1001'));
  322. $this->assertSame($connection1, $cluster->getConnection($set));
  323. $this->assertSame($connection1, $cluster->getConnection($get));
  324. $set = $profile->createCommand('set', array('node:1048', 'foobar'));
  325. $get = $profile->createCommand('get', array('node:1048'));
  326. $this->assertSame($connection2, $cluster->getConnection($set));
  327. $this->assertSame($connection2, $cluster->getConnection($get));
  328. $set = $profile->createCommand('set', array('node:1082', 'foobar'));
  329. $get = $profile->createCommand('get', array('node:1082'));
  330. $this->assertSame($connection3, $cluster->getConnection($set));
  331. $this->assertSame($connection3, $cluster->getConnection($get));
  332. }
  333. /**
  334. * @group disconnected
  335. */
  336. public function testWritesCommandToCorrectConnection()
  337. {
  338. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  339. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  340. $connection1->expects($this->once())->method('writeRequest')->with($command);
  341. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  342. $connection2->expects($this->never())->method('writeRequest');
  343. $cluster = new RedisCluster();
  344. $cluster->add($connection1);
  345. $cluster->add($connection2);
  346. $cluster->writeRequest($command);
  347. }
  348. /**
  349. * @group disconnected
  350. */
  351. public function testReadsCommandFromCorrectConnection()
  352. {
  353. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1050'));
  354. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  355. $connection1->expects($this->never())->method('readResponse');
  356. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  357. $connection2->expects($this->once())->method('readResponse')->with($command);
  358. $cluster = new RedisCluster();
  359. $cluster->add($connection1);
  360. $cluster->add($connection2);
  361. $cluster->readResponse($command);
  362. }
  363. /**
  364. * @group disconnected
  365. */
  366. public function testSupportsKeyHashTags()
  367. {
  368. $profile = Profile\Factory::getDefault();
  369. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  370. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  371. $cluster = new RedisCluster();
  372. $cluster->add($connection1);
  373. $cluster->add($connection2);
  374. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  375. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  376. $this->assertSame($connection1, $cluster->getConnection($set));
  377. $this->assertSame($connection1, $cluster->getConnection($get));
  378. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  379. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  380. $this->assertSame($connection1, $cluster->getConnection($set));
  381. $this->assertSame($connection1, $cluster->getConnection($get));
  382. }
  383. /**
  384. * @group disconnected
  385. */
  386. public function testAskResponseWithConnectionInPool()
  387. {
  388. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6380');
  389. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  390. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  391. $connection1->expects($this->exactly(2))
  392. ->method('executeCommand')
  393. ->with($command)
  394. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  395. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  396. $connection2->expects($this->at(2))
  397. ->method('executeCommand')
  398. ->with($this->isRedisCommand('ASKING'));
  399. $connection2->expects($this->at(3))
  400. ->method('executeCommand')
  401. ->with($command)
  402. ->will($this->returnValue('foobar'));
  403. $factory = $this->getMock('Predis\Connection\Factory');
  404. $factory->expects($this->never())->method('create');
  405. $cluster = new RedisCluster($factory);
  406. $cluster->add($connection1);
  407. $cluster->add($connection2);
  408. $this->assertSame('foobar', $cluster->executeCommand($command));
  409. $this->assertSame('foobar', $cluster->executeCommand($command));
  410. $this->assertSame(2, count($cluster));
  411. }
  412. /**
  413. * @group disconnected
  414. */
  415. public function testAskResponseWithConnectionNotInPool()
  416. {
  417. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6381');
  418. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  419. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  420. $connection1->expects($this->exactly(2))
  421. ->method('executeCommand')
  422. ->with($command)
  423. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  424. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  425. $connection2->expects($this->never())
  426. ->method('executeCommand');
  427. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  428. $connection3->expects($this->at(0))
  429. ->method('executeCommand')
  430. ->with($this->isRedisCommand('ASKING'));
  431. $connection3->expects($this->at(1))
  432. ->method('executeCommand')
  433. ->with($command)
  434. ->will($this->returnValue('foobar'));
  435. $factory = $this->getMock('Predis\Connection\Factory');
  436. $factory->expects($this->once())
  437. ->method('create')
  438. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  439. ->will($this->returnValue($connection3));
  440. $cluster = new RedisCluster($factory);
  441. $cluster->add($connection1);
  442. $cluster->add($connection2);
  443. $this->assertSame('foobar', $cluster->executeCommand($command));
  444. $this->assertSame('foobar', $cluster->executeCommand($command));
  445. $this->assertSame(2, count($cluster));
  446. }
  447. /**
  448. * @group disconnected
  449. */
  450. public function testMovedResponseWithConnectionInPool()
  451. {
  452. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6380');
  453. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  454. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  455. $connection1->expects($this->exactly(1))
  456. ->method('executeCommand')
  457. ->with($command)
  458. ->will($this->returnValue($movedResponse));
  459. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  460. $connection2->expects($this->exactly(2))
  461. ->method('executeCommand')
  462. ->with($command)
  463. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  464. $factory = $this->getMock('Predis\Connection\Factory');
  465. $factory->expects($this->never())->method('create');
  466. $cluster = new RedisCluster($factory);
  467. $cluster->add($connection1);
  468. $cluster->add($connection2);
  469. $this->assertSame('foobar', $cluster->executeCommand($command));
  470. $this->assertSame('foobar', $cluster->executeCommand($command));
  471. $this->assertSame(2, count($cluster));
  472. }
  473. /**
  474. * @group disconnected
  475. */
  476. public function testMovedResponseWithConnectionNotInPool()
  477. {
  478. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6381');
  479. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  480. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  481. $connection1->expects($this->once())
  482. ->method('executeCommand')
  483. ->with($command)
  484. ->will($this->returnValue($movedResponse));
  485. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  486. $connection2->expects($this->never())
  487. ->method('executeCommand');
  488. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  489. $connection3->expects($this->exactly(2))
  490. ->method('executeCommand')
  491. ->with($command)
  492. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  493. $factory = $this->getMock('Predis\Connection\Factory');
  494. $factory->expects($this->once())
  495. ->method('create')
  496. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  497. ->will($this->returnValue($connection3));
  498. $cluster = new RedisCluster($factory);
  499. $cluster->add($connection1);
  500. $cluster->add($connection2);
  501. $this->assertSame('foobar', $cluster->executeCommand($command));
  502. $this->assertSame('foobar', $cluster->executeCommand($command));
  503. $this->assertSame(3, count($cluster));
  504. }
  505. /**
  506. * @group disconnected
  507. */
  508. public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
  509. {
  510. $response = array(
  511. array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
  512. array(3072 , 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
  513. array(6144 , 7167, array('', 6384), array('10.1.0.52', 6384)),
  514. array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
  515. array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
  516. array(1024 , 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
  517. array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
  518. array( 5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
  519. array( 0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
  520. array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
  521. array( 4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
  522. array( 9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
  523. array( 8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
  524. array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
  525. array( 2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
  526. array( 7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
  527. );
  528. $command = Command\RawCommand::create('CLUSTER', 'SLOTS');
  529. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  530. $connection1->expects($this->once())
  531. ->method('executeCommand')
  532. ->with($command)
  533. ->will($this->returnValue($response));
  534. $factory = $this->getMock('Predis\Connection\Factory');
  535. $cluster = new RedisCluster($factory);
  536. $cluster->add($connection1);
  537. $cluster->askSlotsMap();
  538. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  539. }
  540. /**
  541. * @group disconnected
  542. * @expectedException Predis\NotSupportedException
  543. * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  544. */
  545. public function testThrowsExceptionOnNonSupportedCommand()
  546. {
  547. $ping = Profile\Factory::getDefault()->createCommand('ping');
  548. $cluster = new RedisCluster();
  549. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  550. $cluster->getConnection($ping);
  551. }
  552. /**
  553. * @group disconnected
  554. */
  555. public function testCanBeSerialized()
  556. {
  557. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  558. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  559. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  560. $cluster = new RedisCluster();
  561. $cluster->add($connection1);
  562. $cluster->add($connection2);
  563. $cluster->add($connection3);
  564. $cluster->buildSlotsMap();
  565. $unserialized = unserialize(serialize($cluster));
  566. $this->assertEquals($cluster, $unserialized);
  567. }
  568. // ******************************************************************** //
  569. // ---- HELPER METHODS ------------------------------------------------ //
  570. // ******************************************************************** //
  571. /**
  572. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  573. *
  574. * @param mixed $parameters Optional parameters.
  575. * @return mixed
  576. */
  577. protected function getMockConnection($parameters = null)
  578. {
  579. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  580. if ($parameters) {
  581. $parameters = Connection\Parameters::create($parameters);
  582. $hash = "{$parameters->host}:{$parameters->port}";
  583. $connection->expects($this->any())
  584. ->method('getParameters')
  585. ->will($this->returnValue($parameters));
  586. $connection->expects($this->any())
  587. ->method('__toString')
  588. ->will($this->returnValue($hash));
  589. }
  590. return $connection;
  591. }
  592. }