RedisClusterTest.php 27 KB

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