MasterSlaveReplicationTest.php 29 KB

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