MasterSlaveReplicationTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Profile\ServerProfile;
  13. /**
  14. *
  15. */
  16. class MasterSlaveReplicationTest extends StandardTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testAddingConnectionsToReplication()
  22. {
  23. $master = $this->getMockConnection('tcp://host1?alias=master');
  24. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  25. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  26. $replication = new MasterSlaveReplication();
  27. $replication->add($master);
  28. $replication->add($slave1);
  29. $replication->add($slave2);
  30. $this->assertSame($master, $replication->getConnectionById('master'));
  31. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  32. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  33. $this->assertSame($master, $replication->getMaster());
  34. $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
  35. }
  36. /**
  37. * @group disconnected
  38. */
  39. public function testRemovingConnectionsFromReplication()
  40. {
  41. $master = $this->getMockConnection('tcp://host1?alias=master');
  42. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  43. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  44. $replication = new MasterSlaveReplication();
  45. $replication->add($master);
  46. $replication->add($slave1);
  47. $this->assertTrue($replication->remove($slave1));
  48. $this->assertFalse($replication->remove($slave2));
  49. $this->assertSame($master, $replication->getMaster());
  50. $this->assertSame(array(), $replication->getSlaves());
  51. }
  52. /**
  53. * @group disconnected
  54. * @expectedException RuntimeException
  55. * @expectedExceptionMessage Replication needs a master and at least one slave
  56. */
  57. public function testThrowsExceptionOnEmptyReplication()
  58. {
  59. $replication = new MasterSlaveReplication();
  60. $replication->connect();
  61. }
  62. /**
  63. * @group disconnected
  64. * @expectedException RuntimeException
  65. * @expectedExceptionMessage Replication needs a master and at least one slave
  66. */
  67. public function testThrowsExceptionOnMissingMaster()
  68. {
  69. $replication = new MasterSlaveReplication();
  70. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  71. $replication->connect();
  72. }
  73. /**
  74. * @group disconnected
  75. * @expectedException RuntimeException
  76. * @expectedExceptionMessage Replication needs a master and at least one slave
  77. */
  78. public function testThrowsExceptionOnMissingSlave()
  79. {
  80. $replication = new MasterSlaveReplication();
  81. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  82. $replication->connect();
  83. }
  84. /**
  85. * @group disconnected
  86. */
  87. public function testConnectForcesConnectionToOneOfSlaves()
  88. {
  89. $master = $this->getMockConnection('tcp://host1?alias=master');
  90. $master->expects($this->never())->method('connect');
  91. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  92. $slave->expects($this->once())->method('connect');
  93. $replication = new MasterSlaveReplication();
  94. $replication->add($master);
  95. $replication->add($slave);
  96. $replication->connect();
  97. }
  98. /**
  99. * @group disconnected
  100. */
  101. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  102. {
  103. $master = $this->getMockConnection('tcp://host1?alias=master');
  104. $master->expects($this->never())->method('isConnected')->will($this->returnValue(false));
  105. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  106. $slave->expects($this->once())->method('isConnected')->will($this->returnValue(true));
  107. $replication = new MasterSlaveReplication();
  108. $replication->add($master);
  109. $replication->add($slave);
  110. $replication->connect();
  111. $this->assertTrue($replication->isConnected());
  112. }
  113. /**
  114. * @group disconnected
  115. */
  116. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  117. {
  118. $master = $this->getMockConnection('tcp://host1?alias=master');
  119. $master->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  120. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  121. $slave->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  122. $replication = new MasterSlaveReplication();
  123. $replication->add($master);
  124. $replication->add($slave);
  125. $this->assertFalse($replication->isConnected());
  126. $replication->connect();
  127. $replication->disconnect();
  128. $this->assertFalse($replication->isConnected());
  129. }
  130. /**
  131. * @group disconnected
  132. */
  133. public function testDisconnectForcesCurrentConnectionToDisconnect()
  134. {
  135. $master = $this->getMockConnection('tcp://host1?alias=master');
  136. $master->expects($this->once())->method('disconnect');
  137. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  138. $slave->expects($this->once())->method('disconnect');
  139. $replication = new MasterSlaveReplication();
  140. $replication->add($master);
  141. $replication->add($slave);
  142. $replication->disconnect();
  143. }
  144. /**
  145. * @group disconnected
  146. */
  147. public function testCanSwitchConnectionByAlias()
  148. {
  149. $master = $this->getMockConnection('tcp://host1?alias=master');
  150. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  151. $replication = new MasterSlaveReplication();
  152. $replication->add($master);
  153. $replication->add($slave1);
  154. $this->assertNull($replication->getCurrent());
  155. $replication->switchTo('master');
  156. $this->assertSame($master, $replication->getCurrent());
  157. $replication->switchTo('slave1');
  158. $this->assertSame($slave1, $replication->getCurrent());
  159. }
  160. /**
  161. * @group disconnected
  162. * @expectedException InvalidArgumentException
  163. * @expectedExceptionMessage The specified connection is not valid
  164. */
  165. public function testThrowsErrorWhenSwitchingToUnknownConnection()
  166. {
  167. $replication = new MasterSlaveReplication();
  168. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  169. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  170. $replication->switchTo('unknown');
  171. }
  172. /**
  173. * @group disconnected
  174. */
  175. public function testUsesSlavesOnReadOnlyCommands()
  176. {
  177. $profile = ServerProfile::getDefault();
  178. $master = $this->getMockConnection('tcp://host1?alias=master');
  179. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  180. $replication = new MasterSlaveReplication();
  181. $replication->add($master);
  182. $replication->add($slave1);
  183. $cmd = $profile->createCommand('exists', array('foo'));
  184. $this->assertSame($slave1, $replication->getConnection($cmd));
  185. $cmd = $profile->createCommand('get', array('foo'));
  186. $this->assertSame($slave1, $replication->getConnection($cmd));
  187. }
  188. /**
  189. * @group disconnected
  190. */
  191. public function testUsesMasterOnWriteCommands()
  192. {
  193. $profile = ServerProfile::getDefault();
  194. $master = $this->getMockConnection('tcp://host1?alias=master');
  195. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  196. $replication = new MasterSlaveReplication();
  197. $replication->add($master);
  198. $replication->add($slave1);
  199. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  200. $this->assertSame($master, $replication->getConnection($cmd));
  201. $cmd = $profile->createCommand('get', array('foo'));
  202. $this->assertSame($master, $replication->getConnection($cmd));
  203. }
  204. /**
  205. * @group disconnected
  206. */
  207. public function testSwitchesFromSlaveToMasterOnWriteCommands()
  208. {
  209. $profile = ServerProfile::getDefault();
  210. $master = $this->getMockConnection('tcp://host1?alias=master');
  211. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  212. $replication = new MasterSlaveReplication();
  213. $replication->add($master);
  214. $replication->add($slave1);
  215. $cmd = $profile->createCommand('exists', array('foo'));
  216. $this->assertSame($slave1, $replication->getConnection($cmd));
  217. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  218. $this->assertSame($master, $replication->getConnection($cmd));
  219. $cmd = $profile->createCommand('exists', array('foo'));
  220. $this->assertSame($master, $replication->getConnection($cmd));
  221. }
  222. /**
  223. * @group disconnected
  224. */
  225. public function testWritesCommandToCorrectConnection()
  226. {
  227. $profile = ServerProfile::getDefault();
  228. $cmdExists = $profile->createCommand('exists', array('foo'));
  229. $cmdSet = $profile->getDefault()->createCommand('set', array('foo', 'bar'));
  230. $master = $this->getMockConnection('tcp://host1?alias=master');
  231. $master->expects($this->once())->method('writeCommand')->with($cmdSet);
  232. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  233. $slave1->expects($this->once())->method('writeCommand')->with($cmdExists);
  234. $replication = new MasterSlaveReplication();
  235. $replication->add($master);
  236. $replication->add($slave1);
  237. $replication->writeCommand($cmdExists);
  238. $replication->writeCommand($cmdSet);
  239. }
  240. /**
  241. * @group disconnected
  242. */
  243. public function testReadsCommandFromCorrectConnection()
  244. {
  245. $profile = ServerProfile::getDefault();
  246. $cmdExists = $profile->createCommand('exists', array('foo'));
  247. $cmdSet = $profile->getDefault()->createCommand('set', array('foo', 'bar'));
  248. $master = $this->getMockConnection('tcp://host1?alias=master');
  249. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  250. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  251. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  252. $replication = new MasterSlaveReplication();
  253. $replication->add($master);
  254. $replication->add($slave1);
  255. $replication->readResponse($cmdExists);
  256. $replication->readResponse($cmdSet);
  257. }
  258. /**
  259. * @group disconnected
  260. */
  261. public function testExecutesCommandOnCorrectConnection()
  262. {
  263. $profile = ServerProfile::getDefault();
  264. $cmdExists = $profile->createCommand('exists', array('foo'));
  265. $cmdSet = $profile->getDefault()->createCommand('set', array('foo', 'bar'));
  266. $master = $this->getMockConnection('tcp://host1?alias=master');
  267. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  268. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  269. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  270. $replication = new MasterSlaveReplication();
  271. $replication->add($master);
  272. $replication->add($slave1);
  273. $replication->executeCommand($cmdExists);
  274. $replication->executeCommand($cmdSet);
  275. }
  276. /**
  277. * @group disconnected
  278. */
  279. public function testWatchTriggersSwitchToMasterConnection()
  280. {
  281. $profile = ServerProfile::getDefault();
  282. $cmdWatch = $profile->createCommand('watch', array('foo'));
  283. $master = $this->getMockConnection('tcp://host1?alias=master');
  284. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  285. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  286. $slave1->expects($this->never())->method('executeCommand');
  287. $replication = new MasterSlaveReplication();
  288. $replication->add($master);
  289. $replication->add($slave1);
  290. $replication->executeCommand($cmdWatch);
  291. }
  292. /**
  293. * @group disconnected
  294. */
  295. public function testMultiTriggersSwitchToMasterConnection()
  296. {
  297. $profile = ServerProfile::getDefault();
  298. $cmdMulti = $profile->createCommand('multi');
  299. $master = $this->getMockConnection('tcp://host1?alias=master');
  300. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  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($cmdMulti);
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testEvalTriggersSwitchToMasterConnection()
  312. {
  313. $profile = ServerProfile::get('dev');
  314. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  315. $master = $this->getMockConnection('tcp://host1?alias=master');
  316. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  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($cmdEval);
  323. }
  324. /**
  325. * @group disconnected
  326. */
  327. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  328. {
  329. $profile = ServerProfile::get('dev');
  330. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  331. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  332. $master = $this->getMockConnection('tcp://host1?alias=master');
  333. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  334. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  335. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  336. $replication = new MasterSlaveReplication();
  337. $replication->add($master);
  338. $replication->add($slave1);
  339. $replication->executeCommand($cmdSortNormal);
  340. $replication->executeCommand($cmdSortStore);
  341. }
  342. /**
  343. * @group disconnected
  344. * @expectedException Predis\NotSupportedException
  345. * @expectedExceptionMessage The command INFO is not allowed in replication mode
  346. */
  347. public function testThrowsExceptionOnNonSupportedCommand()
  348. {
  349. $cmd = ServerProfile::getDefault()->createCommand('info');
  350. $replication = new MasterSlaveReplication();
  351. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  352. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  353. $replication->getConnection($cmd);
  354. }
  355. /**
  356. * @group disconnected
  357. */
  358. public function testCanOverrideReadOnlyFlagForCommands()
  359. {
  360. $profile = ServerProfile::getDefault();
  361. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  362. $cmdGet = $profile->createCommand('get', array('foo'));
  363. $master = $this->getMockConnection('tcp://host1?alias=master');
  364. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  365. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  366. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  367. $replication = new MasterSlaveReplication();
  368. $replication->add($master);
  369. $replication->add($slave1);
  370. $replication->setCommandReadOnly($cmdSet->getId(), true);
  371. $replication->setCommandReadOnly($cmdGet->getId(), false);
  372. $replication->executeCommand($cmdSet);
  373. $replication->executeCommand($cmdGet);
  374. }
  375. /**
  376. * @group disconnected
  377. */
  378. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  379. {
  380. $profile = ServerProfile::getDefault();
  381. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  382. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  383. $master = $this->getMockConnection('tcp://host1?alias=master');
  384. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  385. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  386. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  387. $replication = new MasterSlaveReplication();
  388. $replication->add($master);
  389. $replication->add($slave1);
  390. $replication->setCommandReadOnly('exists', function ($cmd) {
  391. list($arg1) = $cmd->getArguments();
  392. return $arg1 === 'foo';
  393. });
  394. $replication->executeCommand($cmdExistsFoo);
  395. $replication->executeCommand($cmdExistsBar);
  396. }
  397. /**
  398. * @group disconnected
  399. */
  400. public function testCanSetReadOnlyFlagForEvalScripts()
  401. {
  402. $profile = ServerProfile::get('dev');
  403. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  404. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  405. $master = $this->getMockConnection('tcp://host1?alias=master');
  406. $master->expects($this->never())->method('executeCommand');
  407. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  408. $slave1->expects($this->exactly(2))
  409. ->method('executeCommand')
  410. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  411. $replication = new MasterSlaveReplication();
  412. $replication->add($master);
  413. $replication->add($slave1);
  414. $replication->setScriptReadOnly($script);
  415. $replication->executeCommand($cmdEval);
  416. $replication->executeCommand($cmdEvalSha);
  417. }
  418. /**
  419. * @group disconnected
  420. */
  421. public function testCanBeSerialized()
  422. {
  423. $master = $this->getMockConnection('tcp://host1?alias=master');
  424. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  425. $replication = new MasterSlaveReplication();
  426. $replication->add($master);
  427. $replication->add($slave1);
  428. $unserialized = unserialize(serialize($replication));
  429. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  430. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  431. }
  432. // ******************************************************************** //
  433. // ---- HELPER METHODS ------------------------------------------------ //
  434. // ******************************************************************** //
  435. /**
  436. * Returns a base mocked connection from Predis\Connection\SingleConnectionInterface.
  437. *
  438. * @param mixed $parameters Optional parameters.
  439. * @return mixed
  440. */
  441. protected function getMockConnection($parameters = null)
  442. {
  443. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  444. if ($parameters) {
  445. $parameters = new ConnectionParameters($parameters);
  446. $hash = "{$parameters->host}:{$parameters->port}";
  447. $connection->expects($this->any())
  448. ->method('getParameters')
  449. ->will($this->returnValue($parameters));
  450. $connection->expects($this->any())
  451. ->method('__toString')
  452. ->will($this->returnValue($hash));
  453. }
  454. return $connection;
  455. }
  456. }