MasterSlaveReplicationTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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\Connection;
  12. use Predis\Profile;
  13. use Predis\Replication\ReplicationStrategy;
  14. use PredisTestCase;
  15. /**
  16. *
  17. */
  18. class MasterSlaveReplicationTest extends PredisTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testAddingConnectionsToReplication()
  24. {
  25. $master = $this->getMockConnection('tcp://host1?alias=master');
  26. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  27. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  28. $replication = new MasterSlaveReplication();
  29. $replication->add($master);
  30. $replication->add($slave1);
  31. $replication->add($slave2);
  32. $this->assertSame($master, $replication->getConnectionById('master'));
  33. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  34. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  35. $this->assertSame($master, $replication->getMaster());
  36. $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testRemovingConnectionsFromReplication()
  42. {
  43. $master = $this->getMockConnection('tcp://host1?alias=master');
  44. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  45. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  46. $replication = new MasterSlaveReplication();
  47. $replication->add($master);
  48. $replication->add($slave1);
  49. $this->assertTrue($replication->remove($slave1));
  50. $this->assertFalse($replication->remove($slave2));
  51. $this->assertSame($master, $replication->getMaster());
  52. $this->assertSame(array(), $replication->getSlaves());
  53. }
  54. /**
  55. * @group disconnected
  56. * @expectedException \Predis\ClientException
  57. * @expectedExceptionMessage No available connection for replication
  58. */
  59. public function testThrowsExceptionOnEmptyReplication()
  60. {
  61. $replication = new MasterSlaveReplication();
  62. $replication->connect();
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testConnectsToOneOfSlaves()
  68. {
  69. $master = $this->getMockConnection('tcp://host1?alias=master');
  70. $master->expects($this->never())->method('connect');
  71. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  72. $slave->expects($this->once())->method('connect');
  73. $replication = new MasterSlaveReplication();
  74. $replication->add($master);
  75. $replication->add($slave);
  76. $replication->connect();
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testConnectsToMasterOnMissingSlaves()
  82. {
  83. $master = $this->getMockConnection('tcp://host1?alias=master');
  84. $replication = new MasterSlaveReplication();
  85. $replication->add($master);
  86. $replication->connect();
  87. $this->assertSame($master, $replication->getCurrent());
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  93. {
  94. $master = $this->getMockConnection('tcp://host1?alias=master');
  95. $master->expects($this->never())->method('isConnected')->will($this->returnValue(false));
  96. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  97. $slave->expects($this->once())->method('isConnected')->will($this->returnValue(true));
  98. $replication = new MasterSlaveReplication();
  99. $replication->add($master);
  100. $replication->add($slave);
  101. $replication->connect();
  102. $this->assertTrue($replication->isConnected());
  103. }
  104. /**
  105. * @group disconnected
  106. */
  107. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  108. {
  109. $master = $this->getMockConnection('tcp://host1?alias=master');
  110. $master->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  111. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  112. $slave->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  113. $replication = new MasterSlaveReplication();
  114. $replication->add($master);
  115. $replication->add($slave);
  116. $this->assertFalse($replication->isConnected());
  117. $replication->connect();
  118. $replication->disconnect();
  119. $this->assertFalse($replication->isConnected());
  120. }
  121. /**
  122. * @group disconnected
  123. */
  124. public function testDisconnectForcesCurrentConnectionToDisconnect()
  125. {
  126. $master = $this->getMockConnection('tcp://host1?alias=master');
  127. $master->expects($this->once())->method('disconnect');
  128. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  129. $slave->expects($this->once())->method('disconnect');
  130. $replication = new MasterSlaveReplication();
  131. $replication->add($master);
  132. $replication->add($slave);
  133. $replication->disconnect();
  134. }
  135. /**
  136. * @group disconnected
  137. */
  138. public function testCanSwitchConnectionByAlias()
  139. {
  140. $master = $this->getMockConnection('tcp://host1?alias=master');
  141. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  142. $replication = new MasterSlaveReplication();
  143. $replication->add($master);
  144. $replication->add($slave1);
  145. $this->assertNull($replication->getCurrent());
  146. $replication->switchTo('master');
  147. $this->assertSame($master, $replication->getCurrent());
  148. $replication->switchTo('slave1');
  149. $this->assertSame($slave1, $replication->getCurrent());
  150. }
  151. /**
  152. * @group disconnected
  153. * @expectedException \InvalidArgumentException
  154. * @expectedExceptionMessage Invalid connection or connection not found.
  155. */
  156. public function testThrowsErrorWhenSwitchingToUnknownConnectionByAlias()
  157. {
  158. $replication = new MasterSlaveReplication();
  159. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  160. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  161. $replication->switchTo('unknown');
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testCanSwitchConnectionByInstance()
  167. {
  168. $master = $this->getMockConnection('tcp://host1?alias=master');
  169. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  170. $replication = new MasterSlaveReplication();
  171. $replication->add($master);
  172. $replication->add($slave1);
  173. $this->assertNull($replication->getCurrent());
  174. $replication->switchTo($master);
  175. $this->assertSame($master, $replication->getCurrent());
  176. $replication->switchTo($slave1);
  177. $this->assertSame($slave1, $replication->getCurrent());
  178. }
  179. /**
  180. * @group disconnected
  181. * @expectedException \InvalidArgumentException
  182. * @expectedExceptionMessage Invalid connection or connection not found.
  183. */
  184. public function testThrowsErrorWhenSwitchingToUnknownConnectionByInstance()
  185. {
  186. $replication = new MasterSlaveReplication();
  187. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  188. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  189. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  190. $replication->switchTo($slave2);
  191. }
  192. /**
  193. * @group disconnected
  194. */
  195. public function testCanSwitchToMaster()
  196. {
  197. $master = $this->getMockConnection('tcp://host1?alias=master');
  198. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  199. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  200. $replication = new MasterSlaveReplication();
  201. $replication->add($master);
  202. $replication->add($slave1);
  203. $replication->add($slave2);
  204. $this->assertNull($replication->getCurrent());
  205. $replication->switchToMaster();
  206. $this->assertSame($master, $replication->getCurrent());
  207. }
  208. /**
  209. * @group disconnected
  210. * @expectedException \InvalidArgumentException
  211. * @expectedExceptionMessage Invalid connection or connection not found.
  212. */
  213. public function testThrowsErrorOnSwitchToMasterWithNoMasterDefined()
  214. {
  215. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  216. $replication = new MasterSlaveReplication();
  217. $replication->add($slave1);
  218. $replication->switchToMaster();
  219. }
  220. /**
  221. * @group disconnected
  222. * @todo We should find a way to test that the slave is indeed randomly selected.
  223. */
  224. public function testCanSwitchToRandomSlave()
  225. {
  226. $master = $this->getMockConnection('tcp://host1?alias=master');
  227. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  228. $replication = new MasterSlaveReplication();
  229. $replication->add($master);
  230. $replication->add($slave1);
  231. $this->assertNull($replication->getCurrent());
  232. $replication->switchToSlave();
  233. $this->assertSame($slave1, $replication->getCurrent());
  234. }
  235. /**
  236. * @group disconnected
  237. * @expectedException \InvalidArgumentException
  238. * @expectedExceptionMessage Invalid connection or connection not found.
  239. */
  240. public function testThrowsErrorOnSwitchToRandomSlaveWithNoSlavesDefined()
  241. {
  242. $master = $this->getMockConnection('tcp://host1?alias=master');
  243. $replication = new MasterSlaveReplication();
  244. $replication->add($master);
  245. $replication->switchToSlave();
  246. }
  247. /**
  248. * @group disconnected
  249. */
  250. public function testUsesSlavesOnReadOnlyCommands()
  251. {
  252. $profile = Profile\Factory::getDefault();
  253. $master = $this->getMockConnection('tcp://host1?alias=master');
  254. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  255. $replication = new MasterSlaveReplication();
  256. $replication->add($master);
  257. $replication->add($slave1);
  258. $cmd = $profile->createCommand('exists', array('foo'));
  259. $this->assertSame($slave1, $replication->getConnection($cmd));
  260. $cmd = $profile->createCommand('get', array('foo'));
  261. $this->assertSame($slave1, $replication->getConnection($cmd));
  262. }
  263. /**
  264. * @group disconnected
  265. */
  266. public function testUsesMasterOnWriteRequests()
  267. {
  268. $profile = Profile\Factory::getDefault();
  269. $master = $this->getMockConnection('tcp://host1?alias=master');
  270. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  271. $replication = new MasterSlaveReplication();
  272. $replication->add($master);
  273. $replication->add($slave1);
  274. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  275. $this->assertSame($master, $replication->getConnection($cmd));
  276. $cmd = $profile->createCommand('get', array('foo'));
  277. $this->assertSame($master, $replication->getConnection($cmd));
  278. }
  279. /**
  280. * @group disconnected
  281. */
  282. public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
  283. {
  284. $profile = Profile\Factory::getDefault();
  285. $master = $this->getMockConnection('tcp://host1?alias=master');
  286. $replication = new MasterSlaveReplication();
  287. $replication->add($master);
  288. $cmd = $profile->createCommand('exists', array('foo'));
  289. $this->assertSame($master, $replication->getConnection($cmd));
  290. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  291. $this->assertSame($master, $replication->getConnection($cmd));
  292. }
  293. /**
  294. * @group disconnected
  295. */
  296. public function testSwitchesFromSlaveToMasterOnWriteRequestss()
  297. {
  298. $profile = Profile\Factory::getDefault();
  299. $master = $this->getMockConnection('tcp://host1?alias=master');
  300. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  301. $replication = new MasterSlaveReplication();
  302. $replication->add($master);
  303. $replication->add($slave1);
  304. $cmd = $profile->createCommand('exists', array('foo'));
  305. $this->assertSame($slave1, $replication->getConnection($cmd));
  306. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  307. $this->assertSame($master, $replication->getConnection($cmd));
  308. $cmd = $profile->createCommand('exists', array('foo'));
  309. $this->assertSame($master, $replication->getConnection($cmd));
  310. }
  311. /**
  312. * @group disconnected
  313. */
  314. public function testWritesCommandToCorrectConnection()
  315. {
  316. $profile = Profile\Factory::getDefault();
  317. $cmdExists = $profile->createCommand('exists', array('foo'));
  318. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  319. $master = $this->getMockConnection('tcp://host1?alias=master');
  320. $master->expects($this->once())->method('writeRequest')->with($cmdSet);
  321. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  322. $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
  323. $replication = new MasterSlaveReplication();
  324. $replication->add($master);
  325. $replication->add($slave1);
  326. $replication->writeRequest($cmdExists);
  327. $replication->writeRequest($cmdSet);
  328. }
  329. /**
  330. * @group disconnected
  331. */
  332. public function testReadsCommandFromCorrectConnection()
  333. {
  334. $profile = Profile\Factory::getDefault();
  335. $cmdExists = $profile->createCommand('exists', array('foo'));
  336. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  337. $master = $this->getMockConnection('tcp://host1?alias=master');
  338. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  339. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  340. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  341. $replication = new MasterSlaveReplication();
  342. $replication->add($master);
  343. $replication->add($slave1);
  344. $replication->readResponse($cmdExists);
  345. $replication->readResponse($cmdSet);
  346. }
  347. /**
  348. * @group disconnected
  349. */
  350. public function testExecutesCommandOnCorrectConnection()
  351. {
  352. $profile = Profile\Factory::getDefault();
  353. $cmdExists = $profile->createCommand('exists', array('foo'));
  354. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  355. $master = $this->getMockConnection('tcp://host1?alias=master');
  356. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  357. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  358. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  359. $replication = new MasterSlaveReplication();
  360. $replication->add($master);
  361. $replication->add($slave1);
  362. $replication->executeCommand($cmdExists);
  363. $replication->executeCommand($cmdSet);
  364. }
  365. /**
  366. * @group disconnected
  367. */
  368. public function testWatchTriggersSwitchToMasterConnection()
  369. {
  370. $profile = Profile\Factory::getDefault();
  371. $cmdWatch = $profile->createCommand('watch', array('foo'));
  372. $master = $this->getMockConnection('tcp://host1?alias=master');
  373. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  374. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  375. $slave1->expects($this->never())->method('executeCommand');
  376. $replication = new MasterSlaveReplication();
  377. $replication->add($master);
  378. $replication->add($slave1);
  379. $replication->executeCommand($cmdWatch);
  380. }
  381. /**
  382. * @group disconnected
  383. */
  384. public function testMultiTriggersSwitchToMasterConnection()
  385. {
  386. $profile = Profile\Factory::getDefault();
  387. $cmdMulti = $profile->createCommand('multi');
  388. $master = $this->getMockConnection('tcp://host1?alias=master');
  389. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  390. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  391. $slave1->expects($this->never())->method('executeCommand');
  392. $replication = new MasterSlaveReplication();
  393. $replication->add($master);
  394. $replication->add($slave1);
  395. $replication->executeCommand($cmdMulti);
  396. }
  397. /**
  398. * @group disconnected
  399. */
  400. public function testEvalTriggersSwitchToMasterConnection()
  401. {
  402. $profile = Profile\Factory::get('dev');
  403. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  404. $master = $this->getMockConnection('tcp://host1?alias=master');
  405. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  406. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  407. $slave1->expects($this->never())->method('executeCommand');
  408. $replication = new MasterSlaveReplication();
  409. $replication->add($master);
  410. $replication->add($slave1);
  411. $replication->executeCommand($cmdEval);
  412. }
  413. /**
  414. * @group disconnected
  415. */
  416. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  417. {
  418. $profile = Profile\Factory::get('dev');
  419. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  420. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  421. $master = $this->getMockConnection('tcp://host1?alias=master');
  422. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  423. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  424. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  425. $replication = new MasterSlaveReplication();
  426. $replication->add($master);
  427. $replication->add($slave1);
  428. $replication->executeCommand($cmdSortNormal);
  429. $replication->executeCommand($cmdSortStore);
  430. }
  431. /**
  432. * @group disconnected
  433. */
  434. public function testDiscardsUnreachableSlaveAndExecutesReadOnlyCommandOnNextSlave()
  435. {
  436. $profile = Profile\Factory::getDefault();
  437. $cmdExists = $profile->createCommand('exists', array('key'));
  438. $master = $this->getMockConnection('tcp://host1?alias=master');
  439. $master->expects($this->never())->method('executeCommand');
  440. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  441. $slave1->expects($this->once())
  442. ->method('executeCommand')
  443. ->with($cmdExists)
  444. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  445. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  446. $slave2->expects($this->once())
  447. ->method('executeCommand')
  448. ->with($cmdExists)
  449. ->will($this->returnValue(1));
  450. $replication = new MasterSlaveReplication();
  451. $replication->add($master);
  452. $replication->add($slave1);
  453. $replication->add($slave2);
  454. $replication->switchTo($slave1);
  455. $response = $replication->executeCommand($cmdExists);
  456. $this->assertSame(1, $response);
  457. $this->assertNull($replication->getConnectionById('slave1'));
  458. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  459. }
  460. /**
  461. * @group disconnected
  462. */
  463. public function testDiscardsUnreachableSlavesAndExecutesReadOnlyCommandOnMaster()
  464. {
  465. $profile = Profile\Factory::getDefault();
  466. $cmdExists = $profile->createCommand('exists', array('key'));
  467. $master = $this->getMockConnection('tcp://host1?alias=master');
  468. $master->expects($this->once())
  469. ->method('executeCommand')
  470. ->with($cmdExists)
  471. ->will($this->returnValue(1));
  472. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  473. $slave1->expects($this->once())
  474. ->method('executeCommand')
  475. ->with($cmdExists)
  476. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  477. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  478. $slave2->expects($this->once())
  479. ->method('executeCommand')
  480. ->with($cmdExists)
  481. ->will($this->throwException(new Connection\ConnectionException($slave2)));
  482. $replication = new MasterSlaveReplication();
  483. $replication->add($master);
  484. $replication->add($slave1);
  485. $replication->add($slave2);
  486. $replication->switchTo($slave1);
  487. $response = $replication->executeCommand($cmdExists);
  488. $this->assertSame(1, $response);
  489. $this->assertNull($replication->getConnectionById('slave1'));
  490. $this->assertNull($replication->getConnectionById('slave2'));
  491. }
  492. /**
  493. * @group disconnected
  494. */
  495. public function testSucceedOnReadOnlyCommandAndNoConnectionSetAsMaster()
  496. {
  497. $profile = Profile\Factory::getDefault();
  498. $cmdExists = $profile->createCommand('exists', array('key'));
  499. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  500. $slave1->expects($this->once())
  501. ->method('executeCommand')
  502. ->with($cmdExists)
  503. ->will($this->returnValue(1));
  504. $replication = new MasterSlaveReplication();
  505. $replication->add($slave1);
  506. $response = $replication->executeCommand($cmdExists);
  507. $this->assertSame(1, $response);
  508. }
  509. /**
  510. * @group disconnected
  511. * @expectedException \Predis\ClientException
  512. * @expectedMessage No master server available for replication
  513. */
  514. public function testFailsOnWriteCommandAndNoConnectionSetAsMaster()
  515. {
  516. $profile = Profile\Factory::getDefault();
  517. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  518. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  519. $slave1->expects($this->never())->method('executeCommand');
  520. $replication = new MasterSlaveReplication();
  521. $replication->add($slave1);
  522. $replication->executeCommand($cmdSet);
  523. }
  524. /**
  525. * @group disconnected
  526. * @expectedException \Predis\Connection\ConnectionException
  527. */
  528. public function testFailsOnUnreachableMaster()
  529. {
  530. $profile = Profile\Factory::getDefault();
  531. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  532. $master = $this->getMockConnection('tcp://host1?alias=master');
  533. $master->expects($this->once())
  534. ->method('executeCommand')
  535. ->with($cmdSet)
  536. ->will($this->throwException(new Connection\ConnectionException($master)));
  537. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  538. $slave1->expects($this->never())
  539. ->method('executeCommand');
  540. $replication = new MasterSlaveReplication();
  541. $replication->add($master);
  542. $replication->add($slave1);
  543. $replication->executeCommand($cmdSet);
  544. }
  545. /**
  546. * @group disconnected
  547. * @expectedException \Predis\NotSupportedException
  548. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  549. */
  550. public function testThrowsExceptionOnNonSupportedCommand()
  551. {
  552. $cmd = Profile\Factory::getDefault()->createCommand('info');
  553. $replication = new MasterSlaveReplication();
  554. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  555. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  556. $replication->getConnection($cmd);
  557. }
  558. /**
  559. * @group disconnected
  560. */
  561. public function testCanOverrideReadOnlyFlagForCommands()
  562. {
  563. $profile = Profile\Factory::getDefault();
  564. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  565. $cmdGet = $profile->createCommand('get', array('foo'));
  566. $master = $this->getMockConnection('tcp://host1?alias=master');
  567. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  568. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  569. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  570. $replication = new MasterSlaveReplication();
  571. $replication->add($master);
  572. $replication->add($slave1);
  573. $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
  574. $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
  575. $replication->executeCommand($cmdSet);
  576. $replication->executeCommand($cmdGet);
  577. }
  578. /**
  579. * @group disconnected
  580. */
  581. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  582. {
  583. $profile = Profile\Factory::getDefault();
  584. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  585. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  586. $master = $this->getMockConnection('tcp://host1?alias=master');
  587. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  588. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  589. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  590. $replication = new MasterSlaveReplication();
  591. $replication->add($master);
  592. $replication->add($slave1);
  593. $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
  594. list($arg1) = $cmd->getArguments();
  595. return $arg1 === 'foo';
  596. });
  597. $replication->executeCommand($cmdExistsFoo);
  598. $replication->executeCommand($cmdExistsBar);
  599. }
  600. /**
  601. * @group disconnected
  602. */
  603. public function testCanSetReadOnlyFlagForEvalScripts()
  604. {
  605. $profile = Profile\Factory::get('dev');
  606. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  607. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  608. $master = $this->getMockConnection('tcp://host1?alias=master');
  609. $master->expects($this->never())->method('executeCommand');
  610. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  611. $slave1->expects($this->exactly(2))
  612. ->method('executeCommand')
  613. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  614. $replication = new MasterSlaveReplication();
  615. $replication->add($master);
  616. $replication->add($slave1);
  617. $replication->getReplicationStrategy()->setScriptReadOnly($script);
  618. $replication->executeCommand($cmdEval);
  619. $replication->executeCommand($cmdEvalSha);
  620. }
  621. /**
  622. * @group disconnected
  623. */
  624. public function testExposesReplicationStrategy()
  625. {
  626. $replication = new MasterSlaveReplication();
  627. $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
  628. $strategy = new ReplicationStrategy();
  629. $replication = new MasterSlaveReplication($strategy);
  630. $this->assertSame($strategy, $replication->getReplicationStrategy());
  631. }
  632. /**
  633. * @group disconnected
  634. */
  635. public function testCanBeSerialized()
  636. {
  637. $master = $this->getMockConnection('tcp://host1?alias=master');
  638. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  639. $replication = new MasterSlaveReplication();
  640. $replication->add($master);
  641. $replication->add($slave1);
  642. $unserialized = unserialize(serialize($replication));
  643. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  644. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  645. }
  646. // ******************************************************************** //
  647. // ---- HELPER METHODS ------------------------------------------------ //
  648. // ******************************************************************** //
  649. /**
  650. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  651. *
  652. * @param mixed $parameters Optional parameters.
  653. *
  654. * @return mixed
  655. */
  656. protected function getMockConnection($parameters = null)
  657. {
  658. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  659. if ($parameters) {
  660. $parameters = Connection\Parameters::create($parameters);
  661. $hash = "{$parameters->host}:{$parameters->port}";
  662. $connection->expects($this->any())
  663. ->method('getParameters')
  664. ->will($this->returnValue($parameters));
  665. $connection->expects($this->any())
  666. ->method('__toString')
  667. ->will($this->returnValue($hash));
  668. }
  669. return $connection;
  670. }
  671. }