MasterSlaveReplicationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 testThrowsErrorWhenSwitchingToUnknownConnection()
  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 testUsesSlavesOnReadOnlyCommands()
  167. {
  168. $profile = Profile\Factory::getDefault();
  169. $master = $this->getMockConnection('tcp://host1?alias=master');
  170. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  171. $replication = new MasterSlaveReplication();
  172. $replication->add($master);
  173. $replication->add($slave1);
  174. $cmd = $profile->createCommand('exists', array('foo'));
  175. $this->assertSame($slave1, $replication->getConnection($cmd));
  176. $cmd = $profile->createCommand('get', array('foo'));
  177. $this->assertSame($slave1, $replication->getConnection($cmd));
  178. }
  179. /**
  180. * @group disconnected
  181. */
  182. public function testUsesMasterOnWriteRequests()
  183. {
  184. $profile = Profile\Factory::getDefault();
  185. $master = $this->getMockConnection('tcp://host1?alias=master');
  186. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  187. $replication = new MasterSlaveReplication();
  188. $replication->add($master);
  189. $replication->add($slave1);
  190. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  191. $this->assertSame($master, $replication->getConnection($cmd));
  192. $cmd = $profile->createCommand('get', array('foo'));
  193. $this->assertSame($master, $replication->getConnection($cmd));
  194. }
  195. /**
  196. * @group disconnected
  197. */
  198. public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
  199. {
  200. $profile = Profile\Factory::getDefault();
  201. $master = $this->getMockConnection('tcp://host1?alias=master');
  202. $replication = new MasterSlaveReplication();
  203. $replication->add($master);
  204. $cmd = $profile->createCommand('exists', array('foo'));
  205. $this->assertSame($master, $replication->getConnection($cmd));
  206. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  207. $this->assertSame($master, $replication->getConnection($cmd));
  208. }
  209. /**
  210. * @group disconnected
  211. */
  212. public function testSwitchesFromSlaveToMasterOnWriteRequestss()
  213. {
  214. $profile = Profile\Factory::getDefault();
  215. $master = $this->getMockConnection('tcp://host1?alias=master');
  216. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  217. $replication = new MasterSlaveReplication();
  218. $replication->add($master);
  219. $replication->add($slave1);
  220. $cmd = $profile->createCommand('exists', array('foo'));
  221. $this->assertSame($slave1, $replication->getConnection($cmd));
  222. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  223. $this->assertSame($master, $replication->getConnection($cmd));
  224. $cmd = $profile->createCommand('exists', array('foo'));
  225. $this->assertSame($master, $replication->getConnection($cmd));
  226. }
  227. /**
  228. * @group disconnected
  229. */
  230. public function testWritesCommandToCorrectConnection()
  231. {
  232. $profile = Profile\Factory::getDefault();
  233. $cmdExists = $profile->createCommand('exists', array('foo'));
  234. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  235. $master = $this->getMockConnection('tcp://host1?alias=master');
  236. $master->expects($this->once())->method('writeRequest')->with($cmdSet);
  237. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  238. $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
  239. $replication = new MasterSlaveReplication();
  240. $replication->add($master);
  241. $replication->add($slave1);
  242. $replication->writeRequest($cmdExists);
  243. $replication->writeRequest($cmdSet);
  244. }
  245. /**
  246. * @group disconnected
  247. */
  248. public function testReadsCommandFromCorrectConnection()
  249. {
  250. $profile = Profile\Factory::getDefault();
  251. $cmdExists = $profile->createCommand('exists', array('foo'));
  252. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  253. $master = $this->getMockConnection('tcp://host1?alias=master');
  254. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  255. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  256. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  257. $replication = new MasterSlaveReplication();
  258. $replication->add($master);
  259. $replication->add($slave1);
  260. $replication->readResponse($cmdExists);
  261. $replication->readResponse($cmdSet);
  262. }
  263. /**
  264. * @group disconnected
  265. */
  266. public function testExecutesCommandOnCorrectConnection()
  267. {
  268. $profile = Profile\Factory::getDefault();
  269. $cmdExists = $profile->createCommand('exists', array('foo'));
  270. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  271. $master = $this->getMockConnection('tcp://host1?alias=master');
  272. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  273. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  274. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  275. $replication = new MasterSlaveReplication();
  276. $replication->add($master);
  277. $replication->add($slave1);
  278. $replication->executeCommand($cmdExists);
  279. $replication->executeCommand($cmdSet);
  280. }
  281. /**
  282. * @group disconnected
  283. */
  284. public function testWatchTriggersSwitchToMasterConnection()
  285. {
  286. $profile = Profile\Factory::getDefault();
  287. $cmdWatch = $profile->createCommand('watch', array('foo'));
  288. $master = $this->getMockConnection('tcp://host1?alias=master');
  289. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  290. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  291. $slave1->expects($this->never())->method('executeCommand');
  292. $replication = new MasterSlaveReplication();
  293. $replication->add($master);
  294. $replication->add($slave1);
  295. $replication->executeCommand($cmdWatch);
  296. }
  297. /**
  298. * @group disconnected
  299. */
  300. public function testMultiTriggersSwitchToMasterConnection()
  301. {
  302. $profile = Profile\Factory::getDefault();
  303. $cmdMulti = $profile->createCommand('multi');
  304. $master = $this->getMockConnection('tcp://host1?alias=master');
  305. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  306. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  307. $slave1->expects($this->never())->method('executeCommand');
  308. $replication = new MasterSlaveReplication();
  309. $replication->add($master);
  310. $replication->add($slave1);
  311. $replication->executeCommand($cmdMulti);
  312. }
  313. /**
  314. * @group disconnected
  315. */
  316. public function testEvalTriggersSwitchToMasterConnection()
  317. {
  318. $profile = Profile\Factory::get('dev');
  319. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  320. $master = $this->getMockConnection('tcp://host1?alias=master');
  321. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  322. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  323. $slave1->expects($this->never())->method('executeCommand');
  324. $replication = new MasterSlaveReplication();
  325. $replication->add($master);
  326. $replication->add($slave1);
  327. $replication->executeCommand($cmdEval);
  328. }
  329. /**
  330. * @group disconnected
  331. */
  332. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  333. {
  334. $profile = Profile\Factory::get('dev');
  335. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  336. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  337. $master = $this->getMockConnection('tcp://host1?alias=master');
  338. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  339. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  340. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  341. $replication = new MasterSlaveReplication();
  342. $replication->add($master);
  343. $replication->add($slave1);
  344. $replication->executeCommand($cmdSortNormal);
  345. $replication->executeCommand($cmdSortStore);
  346. }
  347. /**
  348. * @group disconnected
  349. */
  350. public function testDiscardsUnreachableSlaveAndExecutesReadOnlyCommandOnNextSlave()
  351. {
  352. $profile = Profile\Factory::getDefault();
  353. $cmdExists = $profile->createCommand('exists', array('key'));
  354. $master = $this->getMockConnection('tcp://host1?alias=master');
  355. $master->expects($this->never())->method('executeCommand');
  356. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  357. $slave1->expects($this->once())
  358. ->method('executeCommand')
  359. ->with($cmdExists)
  360. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  361. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  362. $slave2->expects($this->once())
  363. ->method('executeCommand')
  364. ->with($cmdExists)
  365. ->will($this->returnValue(1));
  366. $replication = new MasterSlaveReplication();
  367. $replication->add($master);
  368. $replication->add($slave1);
  369. $replication->add($slave2);
  370. $replication->switchTo($slave1);
  371. $response = $replication->executeCommand($cmdExists);
  372. $this->assertSame(1, $response);
  373. $this->assertNull($replication->getConnectionById('slave1'));
  374. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  375. }
  376. /**
  377. * @group disconnected
  378. */
  379. public function testDiscardsUnreachableSlavesAndExecutesReadOnlyCommandOnMaster()
  380. {
  381. $profile = Profile\Factory::getDefault();
  382. $cmdExists = $profile->createCommand('exists', array('key'));
  383. $master = $this->getMockConnection('tcp://host1?alias=master');
  384. $master->expects($this->once())
  385. ->method('executeCommand')
  386. ->with($cmdExists)
  387. ->will($this->returnValue(1));
  388. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  389. $slave1->expects($this->once())
  390. ->method('executeCommand')
  391. ->with($cmdExists)
  392. ->will($this->throwException(new Connection\ConnectionException($slave1)));
  393. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  394. $slave2->expects($this->once())
  395. ->method('executeCommand')
  396. ->with($cmdExists)
  397. ->will($this->throwException(new Connection\ConnectionException($slave2)));
  398. $replication = new MasterSlaveReplication();
  399. $replication->add($master);
  400. $replication->add($slave1);
  401. $replication->add($slave2);
  402. $replication->switchTo($slave1);
  403. $response = $replication->executeCommand($cmdExists);
  404. $this->assertSame(1, $response);
  405. $this->assertNull($replication->getConnectionById('slave1'));
  406. $this->assertNull($replication->getConnectionById('slave2'));
  407. }
  408. /**
  409. * @group disconnected
  410. */
  411. public function testSucceedOnReadOnlyCommandAndNoConnectionSetAsMaster()
  412. {
  413. $profile = Profile\Factory::getDefault();
  414. $cmdExists = $profile->createCommand('exists', array('key'));
  415. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  416. $slave1->expects($this->once())
  417. ->method('executeCommand')
  418. ->with($cmdExists)
  419. ->will($this->returnValue(1));
  420. $replication = new MasterSlaveReplication();
  421. $replication->add($slave1);
  422. $response = $replication->executeCommand($cmdExists);
  423. $this->assertSame(1, $response);
  424. }
  425. /**
  426. * @group disconnected
  427. * @expectedException \Predis\ClientException
  428. * @expectedMessage No master server available for replication
  429. */
  430. public function testFailsOnWriteCommandAndNoConnectionSetAsMaster()
  431. {
  432. $profile = Profile\Factory::getDefault();
  433. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  434. $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
  435. $slave1->expects($this->never())->method('executeCommand');
  436. $replication = new MasterSlaveReplication();
  437. $replication->add($slave1);
  438. $replication->executeCommand($cmdSet);
  439. }
  440. /**
  441. * @group disconnected
  442. * @expectedException \Predis\Connection\ConnectionException
  443. */
  444. public function testFailsOnUnreachableMaster()
  445. {
  446. $profile = Profile\Factory::getDefault();
  447. $cmdSet = $profile->createCommand('set', array('key', 'value'));
  448. $master = $this->getMockConnection('tcp://host1?alias=master');
  449. $master->expects($this->once())
  450. ->method('executeCommand')
  451. ->with($cmdSet)
  452. ->will($this->throwException(new Connection\ConnectionException($master)));
  453. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  454. $slave1->expects($this->never())
  455. ->method('executeCommand');
  456. $replication = new MasterSlaveReplication();
  457. $replication->add($master);
  458. $replication->add($slave1);
  459. $replication->executeCommand($cmdSet);
  460. }
  461. /**
  462. * @group disconnected
  463. * @expectedException \Predis\NotSupportedException
  464. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  465. */
  466. public function testThrowsExceptionOnNonSupportedCommand()
  467. {
  468. $cmd = Profile\Factory::getDefault()->createCommand('info');
  469. $replication = new MasterSlaveReplication();
  470. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  471. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  472. $replication->getConnection($cmd);
  473. }
  474. /**
  475. * @group disconnected
  476. */
  477. public function testCanOverrideReadOnlyFlagForCommands()
  478. {
  479. $profile = Profile\Factory::getDefault();
  480. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  481. $cmdGet = $profile->createCommand('get', array('foo'));
  482. $master = $this->getMockConnection('tcp://host1?alias=master');
  483. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  484. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  485. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  486. $replication = new MasterSlaveReplication();
  487. $replication->add($master);
  488. $replication->add($slave1);
  489. $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
  490. $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
  491. $replication->executeCommand($cmdSet);
  492. $replication->executeCommand($cmdGet);
  493. }
  494. /**
  495. * @group disconnected
  496. */
  497. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  498. {
  499. $profile = Profile\Factory::getDefault();
  500. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  501. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  502. $master = $this->getMockConnection('tcp://host1?alias=master');
  503. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  504. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  505. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  506. $replication = new MasterSlaveReplication();
  507. $replication->add($master);
  508. $replication->add($slave1);
  509. $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
  510. list($arg1) = $cmd->getArguments();
  511. return $arg1 === 'foo';
  512. });
  513. $replication->executeCommand($cmdExistsFoo);
  514. $replication->executeCommand($cmdExistsBar);
  515. }
  516. /**
  517. * @group disconnected
  518. */
  519. public function testCanSetReadOnlyFlagForEvalScripts()
  520. {
  521. $profile = Profile\Factory::get('dev');
  522. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  523. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  524. $master = $this->getMockConnection('tcp://host1?alias=master');
  525. $master->expects($this->never())->method('executeCommand');
  526. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  527. $slave1->expects($this->exactly(2))
  528. ->method('executeCommand')
  529. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  530. $replication = new MasterSlaveReplication();
  531. $replication->add($master);
  532. $replication->add($slave1);
  533. $replication->getReplicationStrategy()->setScriptReadOnly($script);
  534. $replication->executeCommand($cmdEval);
  535. $replication->executeCommand($cmdEvalSha);
  536. }
  537. /**
  538. * @group disconnected
  539. */
  540. public function testExposesReplicationStrategy()
  541. {
  542. $replication = new MasterSlaveReplication();
  543. $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
  544. $strategy = new ReplicationStrategy();
  545. $replication = new MasterSlaveReplication($strategy);
  546. $this->assertSame($strategy, $replication->getReplicationStrategy());
  547. }
  548. /**
  549. * @group disconnected
  550. */
  551. public function testCanBeSerialized()
  552. {
  553. $master = $this->getMockConnection('tcp://host1?alias=master');
  554. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  555. $replication = new MasterSlaveReplication();
  556. $replication->add($master);
  557. $replication->add($slave1);
  558. $unserialized = unserialize(serialize($replication));
  559. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  560. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  561. }
  562. // ******************************************************************** //
  563. // ---- HELPER METHODS ------------------------------------------------ //
  564. // ******************************************************************** //
  565. /**
  566. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  567. *
  568. * @param mixed $parameters Optional parameters.
  569. *
  570. * @return mixed
  571. */
  572. protected function getMockConnection($parameters = null)
  573. {
  574. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  575. if ($parameters) {
  576. $parameters = Connection\Parameters::create($parameters);
  577. $hash = "{$parameters->host}:{$parameters->port}";
  578. $connection->expects($this->any())
  579. ->method('getParameters')
  580. ->will($this->returnValue($parameters));
  581. $connection->expects($this->any())
  582. ->method('__toString')
  583. ->will($this->returnValue($hash));
  584. }
  585. return $connection;
  586. }
  587. }