RedisClusterTest.php 25 KB

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