RedisClusterTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 testSupportsKeyHashTags()
  397. {
  398. $profile = Profile\Factory::getDefault();
  399. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  400. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  401. $cluster = new RedisCluster(new Connection\Factory());
  402. $cluster->add($connection1);
  403. $cluster->add($connection2);
  404. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  405. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  406. $this->assertSame($connection1, $cluster->getConnection($set));
  407. $this->assertSame($connection1, $cluster->getConnection($get));
  408. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  409. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  410. $this->assertSame($connection1, $cluster->getConnection($set));
  411. $this->assertSame($connection1, $cluster->getConnection($get));
  412. }
  413. /**
  414. * @group disconnected
  415. */
  416. public function testAskResponseWithConnectionInPool()
  417. {
  418. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6380');
  419. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  420. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  421. $connection1->expects($this->exactly(2))
  422. ->method('executeCommand')
  423. ->with($command)
  424. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  425. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  426. $connection2->expects($this->at(2))
  427. ->method('executeCommand')
  428. ->with($this->isRedisCommand('ASKING'));
  429. $connection2->expects($this->at(3))
  430. ->method('executeCommand')
  431. ->with($command)
  432. ->will($this->returnValue('foobar'));
  433. $factory = $this->getMock('Predis\Connection\Factory');
  434. $factory->expects($this->never())->method('create');
  435. $cluster = new RedisCluster($factory);
  436. $cluster->useClusterSlots(false);
  437. $cluster->add($connection1);
  438. $cluster->add($connection2);
  439. $this->assertSame('foobar', $cluster->executeCommand($command));
  440. $this->assertSame('foobar', $cluster->executeCommand($command));
  441. $this->assertSame(2, count($cluster));
  442. }
  443. /**
  444. * @group disconnected
  445. */
  446. public function testAskResponseWithConnectionNotInPool()
  447. {
  448. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6381');
  449. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  450. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  451. $connection1->expects($this->exactly(2))
  452. ->method('executeCommand')
  453. ->with($command)
  454. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  455. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  456. $connection2->expects($this->never())
  457. ->method('executeCommand');
  458. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  459. $connection3->expects($this->at(0))
  460. ->method('executeCommand')
  461. ->with($this->isRedisCommand('ASKING'));
  462. $connection3->expects($this->at(1))
  463. ->method('executeCommand')
  464. ->with($command)
  465. ->will($this->returnValue('foobar'));
  466. $factory = $this->getMock('Predis\Connection\Factory');
  467. $factory->expects($this->once())
  468. ->method('create')
  469. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  470. ->will($this->returnValue($connection3));
  471. $cluster = new RedisCluster($factory);
  472. $cluster->useClusterSlots(false);
  473. $cluster->add($connection1);
  474. $cluster->add($connection2);
  475. $this->assertSame('foobar', $cluster->executeCommand($command));
  476. $this->assertSame('foobar', $cluster->executeCommand($command));
  477. $this->assertSame(2, count($cluster));
  478. }
  479. /**
  480. * @group disconnected
  481. */
  482. public function testMovedResponseWithConnectionInPool()
  483. {
  484. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6380');
  485. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  486. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  487. $connection1->expects($this->exactly(1))
  488. ->method('executeCommand')
  489. ->with($command)
  490. ->will($this->returnValue($movedResponse));
  491. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  492. $connection2->expects($this->exactly(2))
  493. ->method('executeCommand')
  494. ->with($command)
  495. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  496. $factory = $this->getMock('Predis\Connection\Factory');
  497. $factory->expects($this->never())->method('create');
  498. $cluster = new RedisCluster($factory);
  499. $cluster->useClusterSlots(false);
  500. $cluster->add($connection1);
  501. $cluster->add($connection2);
  502. $this->assertSame('foobar', $cluster->executeCommand($command));
  503. $this->assertSame('foobar', $cluster->executeCommand($command));
  504. $this->assertSame(2, count($cluster));
  505. }
  506. /**
  507. * @group disconnected
  508. */
  509. public function testMovedResponseWithConnectionNotInPool()
  510. {
  511. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6381');
  512. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  513. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  514. $connection1->expects($this->once())
  515. ->method('executeCommand')
  516. ->with($command)
  517. ->will($this->returnValue($movedResponse));
  518. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  519. $connection2->expects($this->never())
  520. ->method('executeCommand');
  521. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  522. $connection3->expects($this->exactly(2))
  523. ->method('executeCommand')
  524. ->with($command)
  525. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  526. $factory = $this->getMock('Predis\Connection\Factory');
  527. $factory->expects($this->once())
  528. ->method('create')
  529. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  530. ->will($this->returnValue($connection3));
  531. $cluster = new RedisCluster($factory);
  532. $cluster->useClusterSlots(false);
  533. $cluster->add($connection1);
  534. $cluster->add($connection2);
  535. $this->assertSame('foobar', $cluster->executeCommand($command));
  536. $this->assertSame('foobar', $cluster->executeCommand($command));
  537. $this->assertSame(3, count($cluster));
  538. }
  539. /**
  540. * @group disconnected
  541. */
  542. public function testParseIPv6AddresseAndPortPairInRedirectionPayload()
  543. {
  544. $movedResponse = new Response\Error('MOVED 1970 2001:db8:0:f101::2:6379');
  545. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  546. $connection1 = $this->getMockConnection('tcp://[2001:db8:0:f101::1]:6379');
  547. $connection1->expects($this->once())
  548. ->method('executeCommand')
  549. ->with($command)
  550. ->will($this->returnValue($movedResponse));
  551. $connection2 = $this->getMockConnection('tcp://[2001:db8:0:f101::2]:6379');
  552. $connection2->expects($this->once())
  553. ->method('executeCommand')
  554. ->with($command)
  555. ->will($this->returnValue('foobar'));
  556. $factory = $this->getMock('Predis\Connection\Factory');
  557. $factory->expects($this->once())
  558. ->method('create')
  559. ->with(array('host' => '2001:db8:0:f101::2', 'port' => '6379'))
  560. ->will($this->returnValue($connection2));
  561. $cluster = new RedisCluster($factory);
  562. $cluster->useClusterSlots(false);
  563. $cluster->add($connection1);
  564. $cluster->executeCommand($command);
  565. }
  566. /**
  567. * @group disconnected
  568. */
  569. public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
  570. {
  571. $response = array(
  572. array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
  573. array(3072, 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
  574. array(6144, 7167, array('', 6384), array('10.1.0.52', 6384)),
  575. array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
  576. array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
  577. array(1024, 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
  578. array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
  579. array(5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
  580. array(0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
  581. array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
  582. array(4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
  583. array(9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
  584. array(8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
  585. array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
  586. array(2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
  587. array(7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
  588. );
  589. $command = Command\RawCommand::create('CLUSTER', 'SLOTS');
  590. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  591. $connection1->expects($this->once())
  592. ->method('executeCommand')
  593. ->with($command)
  594. ->will($this->returnValue($response));
  595. $factory = $this->getMock('Predis\Connection\Factory');
  596. $cluster = new RedisCluster($factory);
  597. $cluster->add($connection1);
  598. $cluster->askSlotsMap();
  599. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  600. }
  601. /**
  602. * @group disconnected
  603. */
  604. public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault()
  605. {
  606. $cmdGET = Command\RawCommand::create('GET', 'node:1001');
  607. $rspMOVED = new Response\Error('MOVED 1970 127.0.0.1:6380');
  608. $rspSlotsArray = array(
  609. array(0, 8191, array('127.0.0.1', 6379)),
  610. array(8192, 16383, array('127.0.0.1', 6380)),
  611. );
  612. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  613. $connection1->expects($this->once())
  614. ->method('executeCommand')
  615. ->with($cmdGET)
  616. ->will($this->returnValue($rspMOVED));
  617. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  618. $connection2->expects($this->at(0))
  619. ->method('executeCommand')
  620. ->with($this->isRedisCommand('CLUSTER', array('SLOTS')))
  621. ->will($this->returnValue($rspSlotsArray));
  622. $connection2->expects($this->at(2))
  623. ->method('executeCommand')
  624. ->with($cmdGET)
  625. ->will($this->returnValue('foobar'));
  626. $factory = $this->getMock('Predis\Connection\Factory');
  627. $factory->expects($this->once())
  628. ->method('create')
  629. ->with(array('host' => '127.0.0.1', 'port' => '6380'))
  630. ->will($this->returnValue($connection2));
  631. $cluster = new RedisCluster($factory);
  632. $cluster->add($connection1);
  633. $this->assertSame('foobar', $cluster->executeCommand($cmdGET));
  634. $this->assertSame(2, count($cluster));
  635. }
  636. /**
  637. * @group disconnected
  638. * @expectedException \Predis\NotSupportedException
  639. * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  640. */
  641. public function testThrowsExceptionOnNonSupportedCommand()
  642. {
  643. $ping = Profile\Factory::getDefault()->createCommand('ping');
  644. $cluster = new RedisCluster(new Connection\Factory());
  645. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  646. $cluster->getConnection($ping);
  647. }
  648. /**
  649. * @medium
  650. * @group disconnected
  651. */
  652. public function testCanBeSerialized()
  653. {
  654. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  655. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  656. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  657. $cluster = new RedisCluster(new Connection\Factory());
  658. $cluster->add($connection1);
  659. $cluster->add($connection2);
  660. $cluster->add($connection3);
  661. $cluster->buildSlotsMap();
  662. $unserialized = unserialize(serialize($cluster));
  663. $this->assertEquals($cluster, $unserialized);
  664. }
  665. // ******************************************************************** //
  666. // ---- HELPER METHODS ------------------------------------------------ //
  667. // ******************************************************************** //
  668. /**
  669. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  670. *
  671. * @param mixed $parameters Optional parameters.
  672. *
  673. * @return mixed
  674. */
  675. protected function getMockConnection($parameters = null)
  676. {
  677. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  678. if ($parameters) {
  679. $parameters = Connection\Parameters::create($parameters);
  680. $hash = "{$parameters->host}:{$parameters->port}";
  681. $connection->expects($this->any())
  682. ->method('getParameters')
  683. ->will($this->returnValue($parameters));
  684. $connection->expects($this->any())
  685. ->method('__toString')
  686. ->will($this->returnValue($hash));
  687. }
  688. return $connection;
  689. }
  690. }