RedisClusterTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 Predis\Command;
  12. use Predis\Connection;
  13. use Predis\Profile;
  14. use Predis\Response;
  15. use PredisTestCase;
  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 testCanAssignConnectionsToRangeOfSlotsFromParameters()
  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. $cluster->buildSlotsMap();
  282. $expectedMap = array_merge(
  283. array_fill(0, 5461, '127.0.0.1:6379'),
  284. array_fill(5460, 5461, '127.0.0.1:6380'),
  285. array_fill(10921, 5462, '127.0.0.1:6381')
  286. );
  287. $actualMap = $cluster->getSlotsMap();
  288. ksort($actualMap);
  289. $this->assertSame($expectedMap, $actualMap);
  290. }
  291. /**
  292. * @group disconnected
  293. */
  294. public function testCanAssignConnectionsToSingleSlotOrRangesOfSlotsFromParameters()
  295. {
  296. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460,5500-5600,11000');
  297. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-5499,5600-10921');
  298. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-10999,11001-16383');
  299. $cluster = new RedisCluster(new Connection\Factory());
  300. $cluster->add($connection1);
  301. $cluster->add($connection2);
  302. $cluster->add($connection3);
  303. $cluster->buildSlotsMap();
  304. $expectedMap = array_merge(
  305. array_fill(0, 5461, '127.0.0.1:6379'),
  306. array_fill(5460, 39, '127.0.0.1:6380'),
  307. array_fill(5499, 101, '127.0.0.1:6379'),
  308. array_fill(5599, 5321, '127.0.0.1:6380'),
  309. array_fill(10921, 78, '127.0.0.1:6381'),
  310. array_fill(11000, 1, '127.0.0.1:6379'),
  311. array_fill(11000, 5383, '127.0.0.1:6381')
  312. );
  313. $actualMap = $cluster->getSlotsMap();
  314. ksort($actualMap);
  315. $this->assertSame($expectedMap, $actualMap);
  316. }
  317. /**
  318. * @group disconnected
  319. */
  320. public function testReturnsCorrectConnectionUsingSlotID()
  321. {
  322. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  323. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  324. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  325. $cluster = new RedisCluster(new Connection\Factory());
  326. $cluster->add($connection1);
  327. $cluster->add($connection2);
  328. $cluster->add($connection3);
  329. $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
  330. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  331. $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
  332. $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
  333. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  334. }
  335. /**
  336. * @group disconnected
  337. */
  338. public function testReturnsCorrectConnectionUsingCommandInstance()
  339. {
  340. $profile = Profile\Factory::getDefault();
  341. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  342. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  343. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  344. $cluster = new RedisCluster(new Connection\Factory());
  345. $cluster->add($connection1);
  346. $cluster->add($connection2);
  347. $cluster->add($connection3);
  348. $set = $profile->createCommand('set', array('node:1001', 'foobar'));
  349. $get = $profile->createCommand('get', array('node:1001'));
  350. $this->assertSame($connection1, $cluster->getConnection($set));
  351. $this->assertSame($connection1, $cluster->getConnection($get));
  352. $set = $profile->createCommand('set', array('node:1048', 'foobar'));
  353. $get = $profile->createCommand('get', array('node:1048'));
  354. $this->assertSame($connection2, $cluster->getConnection($set));
  355. $this->assertSame($connection2, $cluster->getConnection($get));
  356. $set = $profile->createCommand('set', array('node:1082', 'foobar'));
  357. $get = $profile->createCommand('get', array('node:1082'));
  358. $this->assertSame($connection3, $cluster->getConnection($set));
  359. $this->assertSame($connection3, $cluster->getConnection($get));
  360. }
  361. /**
  362. * @group disconnected
  363. */
  364. public function testWritesCommandToCorrectConnection()
  365. {
  366. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  367. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  368. $connection1->expects($this->once())->method('writeRequest')->with($command);
  369. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  370. $connection2->expects($this->never())->method('writeRequest');
  371. $cluster = new RedisCluster(new Connection\Factory());
  372. $cluster->useClusterSlots(false);
  373. $cluster->add($connection1);
  374. $cluster->add($connection2);
  375. $cluster->writeRequest($command);
  376. }
  377. /**
  378. * @group disconnected
  379. */
  380. public function testReadsCommandFromCorrectConnection()
  381. {
  382. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1050'));
  383. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  384. $connection1->expects($this->never())->method('readResponse');
  385. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  386. $connection2->expects($this->once())->method('readResponse')->with($command);
  387. $cluster = new RedisCluster(new Connection\Factory());
  388. $cluster->useClusterSlots(false);
  389. $cluster->add($connection1);
  390. $cluster->add($connection2);
  391. $cluster->readResponse($command);
  392. }
  393. /**
  394. * @group disconnected
  395. */
  396. public function testRetriesExecutingCommandAfterFetchingNewSlotsMapOnConnectionFailure()
  397. {
  398. $slotsmap = array(
  399. array(0, 5500, array('127.0.0.1', 9381), array()),
  400. array(5501, 11000, array('127.0.0.1', 6382), array()),
  401. array(1101, 16383, array('127.0.0.1', 6383), array()),
  402. );
  403. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  404. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  405. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  406. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381');
  407. $connection1->expects($this->once())
  408. ->method('executeCommand')
  409. ->with($this->isRedisCommand(
  410. 'GET', array('node:1001')
  411. ))
  412. ->will($this->throwException(
  413. new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6382]')
  414. ));
  415. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382');
  416. $connection2->expects($this->any())
  417. ->method('executeCommand')
  418. ->with($this->isRedisCommand(
  419. 'CLUSTER', array('SLOTS')
  420. ))
  421. ->will($this->returnValue($slotsmap));
  422. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383');
  423. $connection3->expects($this->any())
  424. ->method('executeCommand')
  425. ->with($this->isRedisCommand(
  426. 'CLUSTER', array('SLOTS')
  427. ))
  428. ->will($this->returnValue($slotsmap));
  429. $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381');
  430. $connection4->expects($this->at(0))
  431. ->method('executeCommand')
  432. ->with($this->isRedisCommand(
  433. 'GET', array('node:1001')
  434. ))
  435. ->will($this->returnValue('value:1001'));
  436. $connection4->expects($this->at(1))
  437. ->method('executeCommand')
  438. ->with($this->isRedisCommand(
  439. 'GET', array('node:5001')
  440. ))
  441. ->will($this->returnValue('value:5001'));
  442. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  443. $factory->expects($this->once())
  444. ->method('create')
  445. ->with(array(
  446. 'host' => '127.0.0.1',
  447. 'port' => '9381',
  448. ))
  449. ->will($this->returnValue($connection4));
  450. $cluster = new RedisCluster($factory);
  451. $cluster->add($connection1);
  452. $cluster->add($connection2);
  453. $cluster->add($connection3);
  454. $this->assertSame('value:1001', $cluster->executeCommand(
  455. Command\RawCommand::create('get', 'node:1001')
  456. ));
  457. $this->assertSame('value:5001', $cluster->executeCommand(
  458. Command\RawCommand::create('get', 'node:5001')
  459. ));
  460. }
  461. /**
  462. * @group disconnected
  463. */
  464. public function testSupportsKeyHashTags()
  465. {
  466. $profile = Profile\Factory::getDefault();
  467. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  468. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  469. $cluster = new RedisCluster(new Connection\Factory());
  470. $cluster->add($connection1);
  471. $cluster->add($connection2);
  472. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  473. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  474. $this->assertSame($connection1, $cluster->getConnection($set));
  475. $this->assertSame($connection1, $cluster->getConnection($get));
  476. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  477. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  478. $this->assertSame($connection1, $cluster->getConnection($set));
  479. $this->assertSame($connection1, $cluster->getConnection($get));
  480. }
  481. /**
  482. * @group disconnected
  483. */
  484. public function testAskResponseWithConnectionInPool()
  485. {
  486. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6380');
  487. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  488. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  489. $connection1->expects($this->exactly(2))
  490. ->method('executeCommand')
  491. ->with($command)
  492. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  493. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  494. $connection2->expects($this->at(2))
  495. ->method('executeCommand')
  496. ->with($this->isRedisCommand('ASKING'));
  497. $connection2->expects($this->at(3))
  498. ->method('executeCommand')
  499. ->with($command)
  500. ->will($this->returnValue('foobar'));
  501. $factory = $this->getMock('Predis\Connection\Factory');
  502. $factory->expects($this->never())->method('create');
  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(2, count($cluster));
  510. }
  511. /**
  512. * @group disconnected
  513. */
  514. public function testAskResponseWithConnectionNotInPool()
  515. {
  516. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6381');
  517. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  518. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  519. $connection1->expects($this->exactly(2))
  520. ->method('executeCommand')
  521. ->with($command)
  522. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  523. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  524. $connection2->expects($this->never())
  525. ->method('executeCommand');
  526. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  527. $connection3->expects($this->at(0))
  528. ->method('executeCommand')
  529. ->with($this->isRedisCommand('ASKING'));
  530. $connection3->expects($this->at(1))
  531. ->method('executeCommand')
  532. ->with($command)
  533. ->will($this->returnValue('foobar'));
  534. $factory = $this->getMock('Predis\Connection\Factory');
  535. $factory->expects($this->once())
  536. ->method('create')
  537. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  538. ->will($this->returnValue($connection3));
  539. $cluster = new RedisCluster($factory);
  540. $cluster->useClusterSlots(false);
  541. $cluster->add($connection1);
  542. $cluster->add($connection2);
  543. $this->assertSame('foobar', $cluster->executeCommand($command));
  544. $this->assertSame('foobar', $cluster->executeCommand($command));
  545. $this->assertSame(2, count($cluster));
  546. }
  547. /**
  548. * @group disconnected
  549. */
  550. public function testMovedResponseWithConnectionInPool()
  551. {
  552. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6380');
  553. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  554. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  555. $connection1->expects($this->exactly(1))
  556. ->method('executeCommand')
  557. ->with($command)
  558. ->will($this->returnValue($movedResponse));
  559. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  560. $connection2->expects($this->exactly(2))
  561. ->method('executeCommand')
  562. ->with($command)
  563. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  564. $factory = $this->getMock('Predis\Connection\Factory');
  565. $factory->expects($this->never())->method('create');
  566. $cluster = new RedisCluster($factory);
  567. $cluster->useClusterSlots(false);
  568. $cluster->add($connection1);
  569. $cluster->add($connection2);
  570. $this->assertSame('foobar', $cluster->executeCommand($command));
  571. $this->assertSame('foobar', $cluster->executeCommand($command));
  572. $this->assertSame(2, count($cluster));
  573. }
  574. /**
  575. * @group disconnected
  576. */
  577. public function testMovedResponseWithConnectionNotInPool()
  578. {
  579. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6381');
  580. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  581. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  582. $connection1->expects($this->once())
  583. ->method('executeCommand')
  584. ->with($command)
  585. ->will($this->returnValue($movedResponse));
  586. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  587. $connection2->expects($this->never())
  588. ->method('executeCommand');
  589. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  590. $connection3->expects($this->exactly(2))
  591. ->method('executeCommand')
  592. ->with($command)
  593. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  594. $factory = $this->getMock('Predis\Connection\Factory');
  595. $factory->expects($this->once())
  596. ->method('create')
  597. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  598. ->will($this->returnValue($connection3));
  599. $cluster = new RedisCluster($factory);
  600. $cluster->useClusterSlots(false);
  601. $cluster->add($connection1);
  602. $cluster->add($connection2);
  603. $this->assertSame('foobar', $cluster->executeCommand($command));
  604. $this->assertSame('foobar', $cluster->executeCommand($command));
  605. $this->assertSame(3, count($cluster));
  606. }
  607. /**
  608. * @group disconnected
  609. */
  610. public function testParseIPv6AddresseAndPortPairInRedirectionPayload()
  611. {
  612. $movedResponse = new Response\Error('MOVED 1970 2001:db8:0:f101::2:6379');
  613. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  614. $connection1 = $this->getMockConnection('tcp://[2001:db8:0:f101::1]:6379');
  615. $connection1->expects($this->once())
  616. ->method('executeCommand')
  617. ->with($command)
  618. ->will($this->returnValue($movedResponse));
  619. $connection2 = $this->getMockConnection('tcp://[2001:db8:0:f101::2]:6379');
  620. $connection2->expects($this->once())
  621. ->method('executeCommand')
  622. ->with($command)
  623. ->will($this->returnValue('foobar'));
  624. $factory = $this->getMock('Predis\Connection\Factory');
  625. $factory->expects($this->once())
  626. ->method('create')
  627. ->with(array('host' => '2001:db8:0:f101::2', 'port' => '6379'))
  628. ->will($this->returnValue($connection2));
  629. $cluster = new RedisCluster($factory);
  630. $cluster->useClusterSlots(false);
  631. $cluster->add($connection1);
  632. $cluster->executeCommand($command);
  633. }
  634. /**
  635. * @group disconnected
  636. */
  637. public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
  638. {
  639. $response = array(
  640. array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
  641. array(3072, 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
  642. array(6144, 7167, array('', 6384), array('10.1.0.52', 6384)),
  643. array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
  644. array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
  645. array(1024, 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
  646. array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
  647. array(5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
  648. array(0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
  649. array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
  650. array(4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
  651. array(9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
  652. array(8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
  653. array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
  654. array(2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
  655. array(7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
  656. );
  657. $command = Command\RawCommand::create('CLUSTER', 'SLOTS');
  658. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  659. $connection1->expects($this->once())
  660. ->method('executeCommand')
  661. ->with($command)
  662. ->will($this->returnValue($response));
  663. $factory = $this->getMock('Predis\Connection\Factory');
  664. $cluster = new RedisCluster($factory);
  665. $cluster->add($connection1);
  666. $cluster->askSlotsMap();
  667. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  668. }
  669. /**
  670. * @group disconnected
  671. */
  672. public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault()
  673. {
  674. $cmdGET = Command\RawCommand::create('GET', 'node:1001');
  675. $rspMOVED = new Response\Error('MOVED 1970 127.0.0.1:6380');
  676. $rspSlotsArray = array(
  677. array(0, 8191, array('127.0.0.1', 6379)),
  678. array(8192, 16383, array('127.0.0.1', 6380)),
  679. );
  680. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  681. $connection1->expects($this->once())
  682. ->method('executeCommand')
  683. ->with($cmdGET)
  684. ->will($this->returnValue($rspMOVED));
  685. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  686. $connection2->expects($this->at(0))
  687. ->method('executeCommand')
  688. ->with($this->isRedisCommand('CLUSTER', array('SLOTS')))
  689. ->will($this->returnValue($rspSlotsArray));
  690. $connection2->expects($this->at(2))
  691. ->method('executeCommand')
  692. ->with($cmdGET)
  693. ->will($this->returnValue('foobar'));
  694. $factory = $this->getMock('Predis\Connection\Factory');
  695. $factory->expects($this->once())
  696. ->method('create')
  697. ->with(array('host' => '127.0.0.1', 'port' => '6380'))
  698. ->will($this->returnValue($connection2));
  699. $cluster = new RedisCluster($factory);
  700. $cluster->add($connection1);
  701. $this->assertSame('foobar', $cluster->executeCommand($cmdGET));
  702. $this->assertSame(2, count($cluster));
  703. }
  704. /**
  705. * @group disconnected
  706. * @expectedException \Predis\NotSupportedException
  707. * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  708. */
  709. public function testThrowsExceptionOnNonSupportedCommand()
  710. {
  711. $ping = Profile\Factory::getDefault()->createCommand('ping');
  712. $cluster = new RedisCluster(new Connection\Factory());
  713. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  714. $cluster->getConnection($ping);
  715. }
  716. /**
  717. * @medium
  718. * @group disconnected
  719. */
  720. public function testCanBeSerialized()
  721. {
  722. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  723. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  724. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  725. $cluster = new RedisCluster(new Connection\Factory());
  726. $cluster->add($connection1);
  727. $cluster->add($connection2);
  728. $cluster->add($connection3);
  729. $cluster->buildSlotsMap();
  730. $unserialized = unserialize(serialize($cluster));
  731. $this->assertEquals($cluster, $unserialized);
  732. }
  733. // ******************************************************************** //
  734. // ---- HELPER METHODS ------------------------------------------------ //
  735. // ******************************************************************** //
  736. /**
  737. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  738. *
  739. * @param mixed $parameters Optional parameters.
  740. *
  741. * @return mixed
  742. */
  743. protected function getMockConnection($parameters = null)
  744. {
  745. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  746. if ($parameters) {
  747. $parameters = Connection\Parameters::create($parameters);
  748. $hash = "{$parameters->host}:{$parameters->port}";
  749. $connection->expects($this->any())
  750. ->method('getParameters')
  751. ->will($this->returnValue($parameters));
  752. $connection->expects($this->any())
  753. ->method('__toString')
  754. ->will($this->returnValue($hash));
  755. }
  756. return $connection;
  757. }
  758. }