RedisClusterTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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->enableClusterNodes(false);
  326. $cluster->add($connection1);
  327. $cluster->add($connection2);
  328. $cluster->writeCommand($command);
  329. }
  330. /**
  331. * @group disconnected
  332. */
  333. public function testReadsCommandFromCorrectConnection()
  334. {
  335. $command = ServerProfile::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->enableClusterNodes(false);
  342. $cluster->add($connection1);
  343. $cluster->add($connection2);
  344. $cluster->readResponse($command);
  345. }
  346. /**
  347. * @group disconnected
  348. */
  349. public function testSupportsKeyTags()
  350. {
  351. $profile = ServerProfile::getDefault();
  352. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  353. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  354. $cluster = new RedisCluster();
  355. $cluster->add($connection1);
  356. $cluster->add($connection2);
  357. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  358. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  359. $this->assertSame($connection1, $cluster->getConnection($set));
  360. $this->assertSame($connection1, $cluster->getConnection($get));
  361. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  362. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  363. $this->assertSame($connection1, $cluster->getConnection($set));
  364. $this->assertSame($connection1, $cluster->getConnection($get));
  365. }
  366. /**
  367. * @group disconnected
  368. */
  369. public function testAskResponseWithConnectionInPool()
  370. {
  371. $askResponse = new ResponseError('ASK 1970 127.0.0.1:6380');
  372. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  373. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  374. $connection1->expects($this->exactly(2))
  375. ->method('executeCommand')
  376. ->with($command)
  377. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  378. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  379. $connection2->expects($this->at(2))
  380. ->method('executeCommand')
  381. ->with($this->isRedisCommand('ASKING'));
  382. $connection2->expects($this->at(3))
  383. ->method('executeCommand')
  384. ->with($command)
  385. ->will($this->returnValue('foobar'));
  386. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  387. $factory->expects($this->never())->method('create');
  388. $cluster = new RedisCluster($factory);
  389. $cluster->enableClusterNodes(false);
  390. $cluster->add($connection1);
  391. $cluster->add($connection2);
  392. $this->assertSame('foobar', $cluster->executeCommand($command));
  393. $this->assertSame('foobar', $cluster->executeCommand($command));
  394. $this->assertSame(2, count($cluster));
  395. }
  396. /**
  397. * @group disconnected
  398. */
  399. public function testAskResponseWithConnectionNotInPool()
  400. {
  401. $askResponse = new ResponseError('ASK 1970 127.0.0.1:6381');
  402. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  403. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  404. $connection1->expects($this->exactly(2))
  405. ->method('executeCommand')
  406. ->with($command)
  407. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  408. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  409. $connection2->expects($this->never())
  410. ->method('executeCommand');
  411. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  412. $connection3->expects($this->at(0))
  413. ->method('executeCommand')
  414. ->with($this->isRedisCommand('ASKING'));
  415. $connection3->expects($this->at(1))
  416. ->method('executeCommand')
  417. ->with($command)
  418. ->will($this->returnValue('foobar'));
  419. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  420. $factory->expects($this->once())
  421. ->method('create')
  422. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  423. ->will($this->returnValue($connection3));
  424. $cluster = new RedisCluster($factory);
  425. $cluster->enableClusterNodes(false);
  426. $cluster->add($connection1);
  427. $cluster->add($connection2);
  428. $this->assertSame('foobar', $cluster->executeCommand($command));
  429. $this->assertSame('foobar', $cluster->executeCommand($command));
  430. $this->assertSame(2, count($cluster));
  431. }
  432. /**
  433. * @group disconnected
  434. */
  435. public function testMovedResponseWithConnectionInPool()
  436. {
  437. $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6380');
  438. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  439. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  440. $connection1->expects($this->exactly(1))
  441. ->method('executeCommand')
  442. ->with($command)
  443. ->will($this->returnValue($movedResponse));
  444. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  445. $connection2->expects($this->exactly(2))
  446. ->method('executeCommand')
  447. ->with($command)
  448. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  449. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  450. $factory->expects($this->never())->method('create');
  451. $cluster = new RedisCluster($factory);
  452. $cluster->enableClusterNodes(false);
  453. $cluster->add($connection1);
  454. $cluster->add($connection2);
  455. $this->assertSame('foobar', $cluster->executeCommand($command));
  456. $this->assertSame('foobar', $cluster->executeCommand($command));
  457. $this->assertSame(2, count($cluster));
  458. }
  459. /**
  460. * @group disconnected
  461. */
  462. public function testMovedResponseWithConnectionNotInPool()
  463. {
  464. $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6381');
  465. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  466. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  467. $connection1->expects($this->once())
  468. ->method('executeCommand')
  469. ->with($command)
  470. ->will($this->returnValue($movedResponse));
  471. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  472. $connection2->expects($this->never())
  473. ->method('executeCommand');
  474. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  475. $connection3->expects($this->exactly(2))
  476. ->method('executeCommand')
  477. ->with($command)
  478. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  479. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  480. $factory->expects($this->once())
  481. ->method('create')
  482. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  483. ->will($this->returnValue($connection3));
  484. $cluster = new RedisCluster($factory);
  485. $cluster->enableClusterNodes(false);
  486. $cluster->add($connection1);
  487. $cluster->add($connection2);
  488. $this->assertSame('foobar', $cluster->executeCommand($command));
  489. $this->assertSame('foobar', $cluster->executeCommand($command));
  490. $this->assertSame(3, count($cluster));
  491. }
  492. /**
  493. * @group disconnected
  494. */
  495. public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
  496. {
  497. $response = array(
  498. array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
  499. array(3072 , 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
  500. array(6144 , 7167, array('', 6384), array('10.1.0.52', 6384)),
  501. array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
  502. array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
  503. array(1024 , 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
  504. array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
  505. array( 5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
  506. array( 0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
  507. array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
  508. array( 4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
  509. array( 9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
  510. array( 8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
  511. array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
  512. array( 2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
  513. array( 7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
  514. );
  515. $command = RawCommand::create('CLUSTER', 'SLOTS');
  516. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  517. $connection1->expects($this->once())
  518. ->method('executeCommand')
  519. ->with($command)
  520. ->will($this->returnValue($response));
  521. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  522. $cluster = new RedisCluster($factory);
  523. $cluster->add($connection1);
  524. $cluster->askClusterNodes();
  525. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  526. }
  527. /**
  528. * @group disconnected
  529. */
  530. public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault()
  531. {
  532. $cmdGET = RawCommand::create('GET', 'node:1001');
  533. $rspMOVED = new ResponseError('MOVED 1970 127.0.0.1:6380');
  534. $rspSlotsArray = array(
  535. array(0 , 8191, array('127.0.0.1', 6379)),
  536. array(8192, 16383, array('127.0.0.1', 6380)),
  537. );
  538. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  539. $connection1->expects($this->once())
  540. ->method('executeCommand')
  541. ->with($cmdGET)
  542. ->will($this->returnValue($rspMOVED));
  543. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  544. $connection2->expects($this->at(0))
  545. ->method('executeCommand')
  546. ->with($this->isRedisCommand('CLUSTER', array('SLOTS')))
  547. ->will($this->returnValue($rspSlotsArray));
  548. $connection2->expects($this->at(2))
  549. ->method('executeCommand')
  550. ->with($cmdGET)
  551. ->will($this->returnValue('foobar'));
  552. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  553. $factory->expects($this->once())
  554. ->method('create')
  555. ->with(array('host' => '127.0.0.1', 'port' => '6380'))
  556. ->will($this->returnValue($connection2));
  557. $cluster = new RedisCluster($factory);
  558. $cluster->add($connection1);
  559. $this->assertSame('foobar', $cluster->executeCommand($cmdGET));
  560. $this->assertSame(2, count($cluster));
  561. }
  562. /**
  563. * @group disconnected
  564. * @expectedException Predis\NotSupportedException
  565. * @expectedExceptionMessage Cannot use PING with redis-cluster
  566. */
  567. public function testThrowsExceptionOnNonSupportedCommand()
  568. {
  569. $ping = ServerProfile::getDefault()->createCommand('ping');
  570. $cluster = new RedisCluster();
  571. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  572. $cluster->getConnection($ping);
  573. }
  574. /**
  575. * @group disconnected
  576. */
  577. public function testCanBeSerialized()
  578. {
  579. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  580. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  581. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  582. $cluster = new RedisCluster();
  583. $cluster->add($connection1);
  584. $cluster->add($connection2);
  585. $cluster->add($connection3);
  586. $cluster->buildSlotsMap();
  587. $unserialized = unserialize(serialize($cluster));
  588. $this->assertEquals($cluster, $unserialized);
  589. }
  590. // ******************************************************************** //
  591. // ---- HELPER METHODS ------------------------------------------------ //
  592. // ******************************************************************** //
  593. /**
  594. * Returns a base mocked connection from Predis\Connection\SingleConnectionInterface.
  595. *
  596. * @param mixed $parameters Optional parameters.
  597. * @return mixed
  598. */
  599. protected function getMockConnection($parameters = null)
  600. {
  601. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  602. if ($parameters) {
  603. $parameters = new ConnectionParameters($parameters);
  604. $hash = "{$parameters->host}:{$parameters->port}";
  605. $connection->expects($this->any())
  606. ->method('getParameters')
  607. ->will($this->returnValue($parameters));
  608. $connection->expects($this->any())
  609. ->method('__toString')
  610. ->will($this->returnValue($hash));
  611. }
  612. return $connection;
  613. }
  614. }