RedisClusterTest.php 28 KB

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