RedisClusterTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 testDoesNotSupportKeyTags()
  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($connection2, $cluster->getConnection($set));
  363. $this->assertSame($connection2, $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 testFetchSlotsMapFromClusterWithClusterNodesCommand()
  491. {
  492. $output =<<<EOS
  493. 1284a54dc3245d305ae5e3f5507579f9a3e4bd80 10.1.0.51:6387 master - 0 1400000953094 20 connected 12288-13311
  494. 1fb644aa255ef92db789a93cc834f597e9a3800b 10.1.0.52:6392 master - 0 1400000955139 15 connected 3072-4095
  495. fe814c66d4171db548b6dce537a88c3994cd7197 10.1.0.51:6391 slave 7ca7ed3780c405ee50757550e19ade5921097d25 0 1400000954111 5 connected
  496. b3a3b7564c75c096bdc72fc314500cdd666adf87 :0 myself,master - 0 0 18 connected 6144-7167
  497. 67b25b19a07b56a155e25145957dce82a6605f26 10.1.0.51:6388 master - 0 1400000954111 21 connected 14336-15359
  498. ba6e596ae5382ad05f1df5abaaa1489a0e0b2449 10.1.0.52:6398 master - 0 1400000953094 13 connected 15360-16383
  499. 7ca7ed3780c405ee50757550e19ade5921097d25 10.1.0.52:6391 master - 0 1400000954111 5 connected 1024-2047
  500. 4d90ab772120a49b177eb7bd4799b592f5048e16 10.1.0.51:6394 slave fa9b9fae41e3d53d2a901dbfe6fd1527c874768c 0 1400000955139 26 connected
  501. 5731adc383f5f7afbfcca2ee75d17609e436b146 10.1.0.51:6398 slave ba6e596ae5382ad05f1df5abaaa1489a0e0b2449 0 1400000953094 21 connected
  502. fd0bd0cd8461bd44b759fefaabaf541788ec65cf 10.1.0.52:6396 master - 0 1400000955139 7 connected 11264-12287
  503. d012ddf044d0ef5d72d171f8af1e700488b79241 10.1.0.52:6393 master - 0 1400000953604 10 connected 5120-6143
  504. 462ab20db112a007f3ffc4e7e105b0c963aa8981 10.1.0.52:6382 slave a1948d989b092bc96ff91019d9d3b74361edcdfa 0 1400000954111 18 connected
  505. f468a7018ff5a0701a1b614b8ed1b400cddcd578 10.1.0.52:6385 slave 2a1845f27d0f24904d67e2f2cd3e7380949e9d3a 0 1400000953604 25 connected
  506. a48e023927edcc0d001be0535130f41d8e93adde 10.1.0.52:6383 slave a547c6de8acfbf4a97a1c341bddc2efffea83032 0 1400000955139 12 connected
  507. 2530af9ee9b8d4e359cefedd7492ba777dd1e5aa 10.1.0.51:6392 slave 1fb644aa255ef92db789a93cc834f597e9a3800b 0 1400000953603 17 connected
  508. 2d3934ef56abbe25949063474d2e408ae2fb41af 10.1.0.51:6395 slave 0f70284073a6613343fd9a2da82a0549c1efaaae 0 1400000954627 27 connected
  509. 6a0430bf8e1beed8911aa69b002ac7a00ad60428 10.1.0.52:6387 slave 1284a54dc3245d305ae5e3f5507579f9a3e4bd80 0 1400000954111 20 connected
  510. 578ad5ff1970a22419a6afca0cc97bfb729f6b47 10.1.0.51:6397 slave fd637fc700d31395d1f324348b49ad38248ba8d4 0 1400000955139 18 connected
  511. c3083761437050f1cd80a5f95b3ff5aabba3bb97 10.1.0.51:6381 master - 0 1400000955139 11 connected 0-1023
  512. 846d3410ce8e7cea565617686f4685a35d376706 10.1.0.52:6384 slave b3a3b7564c75c096bdc72fc314500cdd666adf87 0 1400000953094 18 connected
  513. fd637fc700d31395d1f324348b49ad38248ba8d4 10.1.0.52:6397 master - 0 1400000954627 16 connected 13312-14335
  514. a547c6de8acfbf4a97a1c341bddc2efffea83032 10.1.0.51:6383 master - 0 1400000954627 8 connected 4096-5119
  515. 0f70284073a6613343fd9a2da82a0549c1efaaae 10.1.0.52:6395 master - 0 1400000954627 27 connected 9216-10239
  516. 2a1845f27d0f24904d67e2f2cd3e7380949e9d3a 10.1.0.51:6385 master - 0 1400000953603 25 connected 8192-9215
  517. ba018d6d154205a47b83de33efbbe846cb6f4b1f 10.1.0.52:6381 slave c3083761437050f1cd80a5f95b3ff5aabba3bb97 0 1400000955139 11 connected
  518. 5a830c9ef0d09eb25b521395466e803052ddbef4 10.1.0.51:6386 master - 0 1400000954627 9 connected 10240-11263
  519. 91d9840437794ab1ff77b59f831ce80ffaaa4c5a 10.1.0.51:6393 slave d012ddf044d0ef5d72d171f8af1e700488b79241 0 1400000954627 14 connected
  520. d6bdbb495fcb66ba3001c2a07c067d5aa354cf55 10.1.0.52:6388 slave 67b25b19a07b56a155e25145957dce82a6605f26 0 1400000954111 21 connected
  521. 2176380db50d687d37e37c3daeeea6e7f8feed0c 10.1.0.51:6396 slave fd0bd0cd8461bd44b759fefaabaf541788ec65cf 0 1400000953603 22 connected
  522. c93ffb4a2ff815f9029156383b00f106234b9546 10.1.0.52:6386 slave 5a830c9ef0d09eb25b521395466e803052ddbef4 0 1400000953604 9 connected
  523. a1948d989b092bc96ff91019d9d3b74361edcdfa 10.1.0.51:6382 master - 0 1400000953094 17 connected 2048-3071
  524. fa9b9fae41e3d53d2a901dbfe6fd1527c874768c 10.1.0.52:6394 master - 0 1400000953094 1 connected 7168-8191
  525. EOS;
  526. $command = Command\RawCommand::create('CLUSTER', 'NODES');
  527. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  528. $connection1->expects($this->once())
  529. ->method('executeCommand')
  530. ->with($command)
  531. ->will($this->returnValue($output));
  532. $factory = $this->getMock('Predis\Connection\Factory');
  533. $cluster = new RedisCluster($factory);
  534. $cluster->add($connection1);
  535. $cluster->askClusterNodes();
  536. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  537. }
  538. /**
  539. * @group disconnected
  540. * @expectedException Predis\NotSupportedException
  541. * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  542. */
  543. public function testThrowsExceptionOnNonSupportedCommand()
  544. {
  545. $ping = Profile\Factory::getDefault()->createCommand('ping');
  546. $cluster = new RedisCluster();
  547. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  548. $cluster->getConnection($ping);
  549. }
  550. /**
  551. * @group disconnected
  552. */
  553. public function testCanBeSerialized()
  554. {
  555. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  556. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  557. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  558. $cluster = new RedisCluster();
  559. $cluster->add($connection1);
  560. $cluster->add($connection2);
  561. $cluster->add($connection3);
  562. $cluster->buildSlotsMap();
  563. $unserialized = unserialize(serialize($cluster));
  564. $this->assertEquals($cluster, $unserialized);
  565. }
  566. // ******************************************************************** //
  567. // ---- HELPER METHODS ------------------------------------------------ //
  568. // ******************************************************************** //
  569. /**
  570. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  571. *
  572. * @param mixed $parameters Optional parameters.
  573. * @return mixed
  574. */
  575. protected function getMockConnection($parameters = null)
  576. {
  577. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  578. if ($parameters) {
  579. $parameters = Connection\Parameters::create($parameters);
  580. $hash = "{$parameters->host}:{$parameters->port}";
  581. $connection->expects($this->any())
  582. ->method('getParameters')
  583. ->will($this->returnValue($parameters));
  584. $connection->expects($this->any())
  585. ->method('__toString')
  586. ->will($this->returnValue($hash));
  587. }
  588. return $connection;
  589. }
  590. }