MasterSlaveReplicationTest.php 26 KB

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