MasterSlaveReplicationTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  229. $replication = new MasterSlaveReplication();
  230. $replication->add($master);
  231. $replication->add($slave1);
  232. $replication->add($slave2);
  233. $this->assertNull($replication->getCurrent());
  234. $replication->switchToSlave();
  235. $this->assertSame($slave1, $replication->getCurrent());
  236. }
  237. /**
  238. * @group disconnected
  239. * @expectedException \InvalidArgumentException
  240. * @expectedExceptionMessage Invalid connection or connection not found.
  241. */
  242. public function testThrowsErrorOnSwitchToRandomSlaveWithNoSlavesDefined()
  243. {
  244. $master = $this->getMockConnection('tcp://host1?alias=master');
  245. $replication = new MasterSlaveReplication();
  246. $replication->add($master);
  247. $replication->switchToSlave();
  248. }
  249. /**
  250. * @group disconnected
  251. */
  252. public function testUsesSlavesOnReadOnlyCommands()
  253. {
  254. $profile = Profile\Factory::getDefault();
  255. $master = $this->getMockConnection('tcp://host1?alias=master');
  256. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  257. $replication = new MasterSlaveReplication();
  258. $replication->add($master);
  259. $replication->add($slave1);
  260. $cmd = $profile->createCommand('exists', array('foo'));
  261. $this->assertSame($slave1, $replication->getConnection($cmd));
  262. $cmd = $profile->createCommand('get', array('foo'));
  263. $this->assertSame($slave1, $replication->getConnection($cmd));
  264. }
  265. /**
  266. * @group disconnected
  267. */
  268. public function testUsesMasterOnWriteRequests()
  269. {
  270. $profile = Profile\Factory::getDefault();
  271. $master = $this->getMockConnection('tcp://host1?alias=master');
  272. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  273. $replication = new MasterSlaveReplication();
  274. $replication->add($master);
  275. $replication->add($slave1);
  276. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  277. $this->assertSame($master, $replication->getConnection($cmd));
  278. $cmd = $profile->createCommand('get', array('foo'));
  279. $this->assertSame($master, $replication->getConnection($cmd));
  280. }
  281. /**
  282. * @group disconnected
  283. */
  284. public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
  285. {
  286. $profile = Profile\Factory::getDefault();
  287. $master = $this->getMockConnection('tcp://host1?alias=master');
  288. $replication = new MasterSlaveReplication();
  289. $replication->add($master);
  290. $cmd = $profile->createCommand('exists', array('foo'));
  291. $this->assertSame($master, $replication->getConnection($cmd));
  292. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  293. $this->assertSame($master, $replication->getConnection($cmd));
  294. }
  295. /**
  296. * @group disconnected
  297. */
  298. public function testSwitchesFromSlaveToMasterOnWriteRequestss()
  299. {
  300. $profile = Profile\Factory::getDefault();
  301. $master = $this->getMockConnection('tcp://host1?alias=master');
  302. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  303. $replication = new MasterSlaveReplication();
  304. $replication->add($master);
  305. $replication->add($slave1);
  306. $cmd = $profile->createCommand('exists', array('foo'));
  307. $this->assertSame($slave1, $replication->getConnection($cmd));
  308. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  309. $this->assertSame($master, $replication->getConnection($cmd));
  310. $cmd = $profile->createCommand('exists', array('foo'));
  311. $this->assertSame($master, $replication->getConnection($cmd));
  312. }
  313. /**
  314. * @group disconnected
  315. */
  316. public function testWritesCommandToCorrectConnection()
  317. {
  318. $profile = Profile\Factory::getDefault();
  319. $cmdExists = $profile->createCommand('exists', array('foo'));
  320. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  321. $master = $this->getMockConnection('tcp://host1?alias=master');
  322. $master->expects($this->once())->method('writeRequest')->with($cmdSet);
  323. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  324. $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
  325. $replication = new MasterSlaveReplication();
  326. $replication->add($master);
  327. $replication->add($slave1);
  328. $replication->writeRequest($cmdExists);
  329. $replication->writeRequest($cmdSet);
  330. }
  331. /**
  332. * @group disconnected
  333. */
  334. public function testReadsCommandFromCorrectConnection()
  335. {
  336. $profile = Profile\Factory::getDefault();
  337. $cmdExists = $profile->createCommand('exists', array('foo'));
  338. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  339. $master = $this->getMockConnection('tcp://host1?alias=master');
  340. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  341. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  342. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  343. $replication = new MasterSlaveReplication();
  344. $replication->add($master);
  345. $replication->add($slave1);
  346. $replication->readResponse($cmdExists);
  347. $replication->readResponse($cmdSet);
  348. }
  349. /**
  350. * @group disconnected
  351. */
  352. public function testExecutesCommandOnCorrectConnection()
  353. {
  354. $profile = Profile\Factory::getDefault();
  355. $cmdExists = $profile->createCommand('exists', array('foo'));
  356. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  357. $master = $this->getMockConnection('tcp://host1?alias=master');
  358. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  359. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  360. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  361. $replication = new MasterSlaveReplication();
  362. $replication->add($master);
  363. $replication->add($slave1);
  364. $replication->executeCommand($cmdExists);
  365. $replication->executeCommand($cmdSet);
  366. }
  367. /**
  368. * @group disconnected
  369. */
  370. public function testWatchTriggersSwitchToMasterConnection()
  371. {
  372. $profile = Profile\Factory::getDefault();
  373. $cmdWatch = $profile->createCommand('watch', array('foo'));
  374. $master = $this->getMockConnection('tcp://host1?alias=master');
  375. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  376. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  377. $slave1->expects($this->never())->method('executeCommand');
  378. $replication = new MasterSlaveReplication();
  379. $replication->add($master);
  380. $replication->add($slave1);
  381. $replication->executeCommand($cmdWatch);
  382. }
  383. /**
  384. * @group disconnected
  385. */
  386. public function testMultiTriggersSwitchToMasterConnection()
  387. {
  388. $profile = Profile\Factory::getDefault();
  389. $cmdMulti = $profile->createCommand('multi');
  390. $master = $this->getMockConnection('tcp://host1?alias=master');
  391. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  392. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  393. $slave1->expects($this->never())->method('executeCommand');
  394. $replication = new MasterSlaveReplication();
  395. $replication->add($master);
  396. $replication->add($slave1);
  397. $replication->executeCommand($cmdMulti);
  398. }
  399. /**
  400. * @group disconnected
  401. */
  402. public function testEvalTriggersSwitchToMasterConnection()
  403. {
  404. $profile = Profile\Factory::get('dev');
  405. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  406. $master = $this->getMockConnection('tcp://host1?alias=master');
  407. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  408. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  409. $slave1->expects($this->never())->method('executeCommand');
  410. $replication = new MasterSlaveReplication();
  411. $replication->add($master);
  412. $replication->add($slave1);
  413. $replication->executeCommand($cmdEval);
  414. }
  415. /**
  416. * @group disconnected
  417. */
  418. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  419. {
  420. $profile = Profile\Factory::get('dev');
  421. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  422. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  423. $master = $this->getMockConnection('tcp://host1?alias=master');
  424. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  425. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  426. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  427. $replication = new MasterSlaveReplication();
  428. $replication->add($master);
  429. $replication->add($slave1);
  430. $replication->executeCommand($cmdSortNormal);
  431. $replication->executeCommand($cmdSortStore);
  432. }
  433. /**
  434. * @group disconnected
  435. */
  436. public function testDiscardsUnreachableSlaveAndExecutesReadOnlyCommandOnNextSlave()
  437. {
  438. $profile = Profile\Factory::getDefault();
  439. $cmdExists = $profile->createCommand('exists', array('key'));
  440. $master = $this->getMockConnection('tcp://host1?alias=master');
  441. $master->expects($this->never())->method('executeCommand');
  442. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  443. $slave1->expects($this->once())
  444. ->method('executeCommand')
  445. ->with($cmdExists)
  446. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  447. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  448. $slave2->expects($this->once())
  449. ->method('executeCommand')
  450. ->with($cmdExists)
  451. ->will($this->returnValue(1));
  452. $replication = new MasterSlaveReplication();
  453. $replication->add($master);
  454. $replication->add($slave1);
  455. $replication->add($slave2);
  456. $replication->switchTo($slave1);
  457. $response = $replication->executeCommand($cmdExists);
  458. $this->assertSame(1, $response);
  459. $this->assertNull($replication->getConnectionById('slave1'));
  460. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  461. }
  462. /**
  463. * @group disconnected
  464. */
  465. public function testDiscardsUnreachableSlavesAndExecutesReadOnlyCommandOnMaster()
  466. {
  467. $profile = Profile\Factory::getDefault();
  468. $cmdExists = $profile->createCommand('exists', array('key'));
  469. $master = $this->getMockConnection('tcp://host1?alias=master');
  470. $master->expects($this->once())
  471. ->method('executeCommand')
  472. ->with($cmdExists)
  473. ->will($this->returnValue(1));
  474. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  475. $slave1->expects($this->once())
  476. ->method('executeCommand')
  477. ->with($cmdExists)
  478. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  479. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  480. $slave2->expects($this->once())
  481. ->method('executeCommand')
  482. ->with($cmdExists)
  483. ->will($this->throwException(new Connection\ConnectionException($slave2)));
  484. $replication = new MasterSlaveReplication();
  485. $replication->add($master);
  486. $replication->add($slave1);
  487. $replication->add($slave2);
  488. $replication->switchTo($slave1);
  489. $response = $replication->executeCommand($cmdExists);
  490. $this->assertSame(1, $response);
  491. $this->assertNull($replication->getConnectionById('slave1'));
  492. $this->assertNull($replication->getConnectionById('slave2'));
  493. }
  494. /**
  495. * @group disconnected
  496. */
  497. public function testSucceedOnReadOnlyCommandAndNoConnectionSetAsMaster()
  498. {
  499. $profile = Profile\Factory::getDefault();
  500. $cmdExists = $profile->createCommand('exists', array('key'));
  501. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  502. $slave1->expects($this->once())
  503. ->method('executeCommand')
  504. ->with($cmdExists)
  505. ->will($this->returnValue(1));
  506. $replication = new MasterSlaveReplication();
  507. $replication->add($slave1);
  508. $response = $replication->executeCommand($cmdExists);
  509. $this->assertSame(1, $response);
  510. }
  511. /**
  512. * @group disconnected
  513. * @expectedException \Predis\ClientException
  514. * @expectedMessage No master server available for replication
  515. */
  516. public function testFailsOnWriteCommandAndNoConnectionSetAsMaster()
  517. {
  518. $profile = Profile\Factory::getDefault();
  519. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  520. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  521. $slave1->expects($this->never())->method('executeCommand');
  522. $replication = new MasterSlaveReplication();
  523. $replication->add($slave1);
  524. $replication->executeCommand($cmdSet);
  525. }
  526. /**
  527. * @group disconnected
  528. * @expectedException \Predis\Connection\ConnectionException
  529. */
  530. public function testFailsOnUnreachableMaster()
  531. {
  532. $profile = Profile\Factory::getDefault();
  533. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  534. $master = $this->getMockConnection('tcp://host1?alias=master');
  535. $master->expects($this->once())
  536. ->method('executeCommand')
  537. ->with($cmdSet)
  538. ->will($this->throwException(new Connection\ConnectionException($master)));
  539. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  540. $slave1->expects($this->never())
  541. ->method('executeCommand');
  542. $replication = new MasterSlaveReplication();
  543. $replication->add($master);
  544. $replication->add($slave1);
  545. $replication->executeCommand($cmdSet);
  546. }
  547. /**
  548. * @group disconnected
  549. * @expectedException \Predis\NotSupportedException
  550. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  551. */
  552. public function testThrowsExceptionOnNonSupportedCommand()
  553. {
  554. $cmd = Profile\Factory::getDefault()->createCommand('info');
  555. $replication = new MasterSlaveReplication();
  556. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  557. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  558. $replication->getConnection($cmd);
  559. }
  560. /**
  561. * @group disconnected
  562. */
  563. public function testCanOverrideReadOnlyFlagForCommands()
  564. {
  565. $profile = Profile\Factory::getDefault();
  566. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  567. $cmdGet = $profile->createCommand('get', array('foo'));
  568. $master = $this->getMockConnection('tcp://host1?alias=master');
  569. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  570. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  571. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  572. $replication = new MasterSlaveReplication();
  573. $replication->add($master);
  574. $replication->add($slave1);
  575. $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
  576. $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
  577. $replication->executeCommand($cmdSet);
  578. $replication->executeCommand($cmdGet);
  579. }
  580. /**
  581. * @group disconnected
  582. */
  583. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  584. {
  585. $profile = Profile\Factory::getDefault();
  586. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  587. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  588. $master = $this->getMockConnection('tcp://host1?alias=master');
  589. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  590. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  591. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  592. $replication = new MasterSlaveReplication();
  593. $replication->add($master);
  594. $replication->add($slave1);
  595. $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
  596. list($arg1) = $cmd->getArguments();
  597. return $arg1 === 'foo';
  598. });
  599. $replication->executeCommand($cmdExistsFoo);
  600. $replication->executeCommand($cmdExistsBar);
  601. }
  602. /**
  603. * @group disconnected
  604. */
  605. public function testCanSetReadOnlyFlagForEvalScripts()
  606. {
  607. $profile = Profile\Factory::get('dev');
  608. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  609. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  610. $master = $this->getMockConnection('tcp://host1?alias=master');
  611. $master->expects($this->never())->method('executeCommand');
  612. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  613. $slave1->expects($this->exactly(2))
  614. ->method('executeCommand')
  615. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  616. $replication = new MasterSlaveReplication();
  617. $replication->add($master);
  618. $replication->add($slave1);
  619. $replication->getReplicationStrategy()->setScriptReadOnly($script);
  620. $replication->executeCommand($cmdEval);
  621. $replication->executeCommand($cmdEvalSha);
  622. }
  623. /**
  624. * @group disconnected
  625. */
  626. public function testExposesReplicationStrategy()
  627. {
  628. $replication = new MasterSlaveReplication();
  629. $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
  630. $strategy = new ReplicationStrategy();
  631. $replication = new MasterSlaveReplication($strategy);
  632. $this->assertSame($strategy, $replication->getReplicationStrategy());
  633. }
  634. /**
  635. * @group disconnected
  636. */
  637. public function testCanBeSerialized()
  638. {
  639. $master = $this->getMockConnection('tcp://host1?alias=master');
  640. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  641. $replication = new MasterSlaveReplication();
  642. $replication->add($master);
  643. $replication->add($slave1);
  644. $unserialized = unserialize(serialize($replication));
  645. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  646. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  647. }
  648. // ******************************************************************** //
  649. // ---- HELPER METHODS ------------------------------------------------ //
  650. // ******************************************************************** //
  651. /**
  652. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  653. *
  654. * @param mixed $parameters Optional parameters.
  655. *
  656. * @return mixed
  657. */
  658. protected function getMockConnection($parameters = null)
  659. {
  660. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  661. if ($parameters) {
  662. $parameters = Connection\Parameters::create($parameters);
  663. $hash = "{$parameters->host}:{$parameters->port}";
  664. $connection->expects($this->any())
  665. ->method('getParameters')
  666. ->will($this->returnValue($parameters));
  667. $connection->expects($this->any())
  668. ->method('__toString')
  669. ->will($this->returnValue($hash));
  670. }
  671. return $connection;
  672. }
  673. }