SentinelReplicationTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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\Replication;
  11. use Predis\Command;
  12. use Predis\Connection;
  13. use Predis\Replication;
  14. use Predis\Response;
  15. use PredisTestCase;
  16. /**
  17. *
  18. */
  19. class SentinelReplicationTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. * @expectedException Predis\ClientException
  24. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  25. */
  26. public function testMethodGetSentinelConnectionThrowsExceptionOnEmptySentinelsPool()
  27. {
  28. $replication = $this->getReplicationConnection('svc', array());
  29. $replication->getSentinelConnection();
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testParametersForSentinelConnectionShouldNotUseDatabaseAndPassword()
  35. {
  36. $replication = $this->getReplicationConnection('svc', array(
  37. 'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret',
  38. ));
  39. $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
  40. $this->assertArrayNotHasKey('password', $parameters);
  41. $this->assertArrayNotHasKey('database', $parameters);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testParametersForSentinelConnectionHaveDefaultTimeout()
  47. {
  48. $replication = $this->getReplicationConnection('svc', array(
  49. 'tcp://127.0.0.1:5381?alias=sentinel',
  50. ));
  51. $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
  52. $this->assertArrayHasKey('timeout', $parameters);
  53. $this->assertSame(0.100, $parameters['timeout']);
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testParametersForSentinelConnectionCanOverrideDefaultTimeout()
  59. {
  60. $replication = $this->getReplicationConnection('svc', array(
  61. 'tcp://127.0.0.1:5381?alias=sentinel&timeout=1',
  62. ));
  63. $parameters = $replication
  64. ->getSentinelConnection()
  65. ->getParameters()
  66. ->toArray();
  67. $this->assertArrayHasKey('timeout', $parameters);
  68. $this->assertSame('1', $parameters['timeout']);
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testConnectionParametersInstanceForSentinelConnectionIsNotModified()
  74. {
  75. $originalParameters = Connection\Parameters::create(
  76. 'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret'
  77. );
  78. $replication = $this->getReplicationConnection('svc', array($originalParameters));
  79. $parameters = $replication
  80. ->getSentinelConnection()
  81. ->getParameters();
  82. $this->assertSame($originalParameters, $parameters);
  83. $this->assertNotNull($parameters->password);
  84. $this->assertNotNull($parameters->database);
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testMethodGetSentinelConnectionReturnsFirstAvailableSentinel()
  90. {
  91. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  92. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  93. $sentinel3 = $this->getMockSentinelConnection('tcp://127.0.0.1:5383?alias=sentinel3');
  94. $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2, $sentinel3));
  95. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testMethodAddAttachesMasterOrSlaveNodesToReplication()
  101. {
  102. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  103. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  104. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  105. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  106. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  107. $replication->add($master);
  108. $replication->add($slave1);
  109. $replication->add($slave2);
  110. $this->assertSame($master, $replication->getConnectionById('master'));
  111. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  112. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  113. $this->assertSame($master, $replication->getMaster());
  114. $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testMethodRemoveDismissesMasterOrSlaveNodesFromReplication()
  120. {
  121. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  122. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  123. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  124. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  125. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  126. $replication->add($master);
  127. $replication->add($slave1);
  128. $replication->add($slave2);
  129. $this->assertTrue($replication->remove($slave1));
  130. $this->assertFalse($replication->remove($sentinel1));
  131. $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
  132. $this->assertCount(1, $slaves = $replication->getSlaves());
  133. $this->assertSame('127.0.0.1:6383', (string) $slaves[0]);
  134. }
  135. /**
  136. * @group disconnected
  137. */
  138. public function testMethodUpdateSentinelsFetchesSentinelNodes()
  139. {
  140. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  141. $sentinel1
  142. ->expects($this->once())
  143. ->method('executeCommand')
  144. ->with($this->isRedisCommand(
  145. 'SENTINEL', array('sentinels', 'svc')
  146. ))
  147. ->will($this->returnValue(
  148. array(
  149. array(
  150. 'name', '127.0.0.1:5382',
  151. 'ip', '127.0.0.1',
  152. 'port', '5382',
  153. 'runid', 'a113aa7a0d4870a85bb22b4b605fd26eb93ed40e',
  154. 'flags', 'sentinel',
  155. ),
  156. array(
  157. 'name', '127.0.0.1:5383',
  158. 'ip', '127.0.0.1',
  159. 'port', '5383',
  160. 'runid', 'f53b52d281be5cdd4873700c94846af8dbe47209',
  161. 'flags', 'sentinel',
  162. ),
  163. )
  164. ));
  165. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  166. $replication->updateSentinels();
  167. // TODO: sorry for the smell...
  168. $reflection = new \ReflectionProperty($replication, 'sentinels');
  169. $reflection->setAccessible(true);
  170. $expected = array(
  171. array('host' => '127.0.0.1', 'port' => '5381'),
  172. array('host' => '127.0.0.1', 'port' => '5382'),
  173. array('host' => '127.0.0.1', 'port' => '5383'),
  174. );
  175. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  176. $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
  177. }
  178. /**
  179. * @group disconnected
  180. */
  181. public function testMethodUpdateSentinelsRemovesCurrentSentinelAndRetriesNextOneOnFailure()
  182. {
  183. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  184. $sentinel1
  185. ->expects($this->once())
  186. ->method('executeCommand')
  187. ->with($this->isRedisCommand(
  188. 'SENTINEL', array('sentinels', 'svc')
  189. ))
  190. ->will($this->throwException(
  191. new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
  192. ));
  193. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  194. $sentinel2
  195. ->expects($this->once())
  196. ->method('executeCommand')
  197. ->with($this->isRedisCommand(
  198. 'SENTINEL', array('sentinels', 'svc')
  199. ))
  200. ->will($this->returnValue(
  201. array(
  202. array(
  203. 'name', '127.0.0.1:5383',
  204. 'ip', '127.0.0.1',
  205. 'port', '5383',
  206. 'runid', 'f53b52d281be5cdd4873700c94846af8dbe47209',
  207. 'flags', 'sentinel',
  208. ),
  209. )
  210. ));
  211. $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2));
  212. $replication->updateSentinels();
  213. // TODO: sorry for the smell...
  214. $reflection = new \ReflectionProperty($replication, 'sentinels');
  215. $reflection->setAccessible(true);
  216. $expected = array(
  217. array('host' => '127.0.0.1', 'port' => '5382'),
  218. array('host' => '127.0.0.1', 'port' => '5383'),
  219. );
  220. $this->assertSame($sentinel2, $replication->getSentinelConnection());
  221. $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
  222. }
  223. /**
  224. * @group disconnected
  225. * @expectedException Predis\ClientException
  226. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  227. */
  228. public function testMethodUpdateSentinelsThrowsExceptionOnNoAvailableSentinel()
  229. {
  230. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  231. $sentinel1
  232. ->expects($this->once())
  233. ->method('executeCommand')
  234. ->with($this->isRedisCommand(
  235. 'SENTINEL', array('sentinels', 'svc')
  236. ))
  237. ->will($this->throwException(
  238. new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
  239. ));
  240. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  241. $replication->updateSentinels();
  242. }
  243. /**
  244. * @group disconnected
  245. */
  246. public function testMethodQuerySentinelFetchesMasterNodeSlaveNodesAndSentinelNodes()
  247. {
  248. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  249. $sentinel1
  250. ->expects($this->exactly(3))
  251. ->method('executeCommand')
  252. ->withConsecutive(
  253. $this->isRedisCommand('SENTINEL', array('sentinels', 'svc')),
  254. $this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc')),
  255. $this->isRedisCommand('SENTINEL', array('slaves', 'svc'))
  256. )
  257. ->will($this->onConsecutiveCalls(
  258. // SENTINEL sentinels svc
  259. array(
  260. array(
  261. 'name', '127.0.0.1:5382',
  262. 'ip', '127.0.0.1',
  263. 'port', '5382',
  264. 'runid', 'a113aa7a0d4870a85bb22b4b605fd26eb93ed40e',
  265. 'flags', 'sentinel',
  266. ),
  267. ),
  268. // SENTINEL get-master-addr-by-name svc
  269. array('127.0.0.1', '6381'),
  270. // SENTINEL slaves svc
  271. array(
  272. array(
  273. 'name', '127.0.0.1:6382',
  274. 'ip', '127.0.0.1',
  275. 'port', '6382',
  276. 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
  277. 'flags', 'slave',
  278. 'master-host', '127.0.0.1',
  279. 'master-port', '6381',
  280. ),
  281. array(
  282. 'name', '127.0.0.1:6383',
  283. 'ip', '127.0.0.1',
  284. 'port', '6383',
  285. 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
  286. 'flags', 'slave',
  287. 'master-host', '127.0.0.1',
  288. 'master-port', '6381',
  289. ),
  290. )
  291. ));
  292. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  293. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  294. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  295. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  296. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  297. $replication->querySentinel();
  298. // TODO: sorry for the smell...
  299. $reflection = new \ReflectionProperty($replication, 'sentinels');
  300. $reflection->setAccessible(true);
  301. $sentinels = array(
  302. array('host' => '127.0.0.1', 'port' => '5381'),
  303. array('host' => '127.0.0.1', 'port' => '5382'),
  304. );
  305. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  306. $this->assertSame($sentinels, array_intersect_key($sentinels, $reflection->getValue($replication)));
  307. $master = $replication->getMaster();
  308. $slaves = $replication->getSlaves();
  309. $this->assertSame('127.0.0.1:6381', (string) $master);
  310. $this->assertCount(2, $slaves);
  311. $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
  312. $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
  313. }
  314. /**
  315. * @group disconnected
  316. */
  317. public function testMethodGetMasterAsksSentinelForMasterOnMasterNotSet()
  318. {
  319. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  320. $sentinel1
  321. ->expects($this->at(0))
  322. ->method('executeCommand')
  323. ->with($this->isRedisCommand(
  324. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  325. ))
  326. ->will($this->returnValue(
  327. array('127.0.0.1', '6381')
  328. ));
  329. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  330. $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
  331. }
  332. /**
  333. * @group disconnected
  334. * @expectedException Predis\ClientException
  335. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  336. */
  337. public function testMethodGetMasterThrowsExceptionOnNoAvailableSentinels()
  338. {
  339. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  340. $sentinel1
  341. ->expects($this->any())
  342. ->method('executeCommand')
  343. ->with($this->isRedisCommand(
  344. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  345. ))
  346. ->will($this->throwException(
  347. new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
  348. ));
  349. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  350. $replication->getMaster();
  351. }
  352. /**
  353. * @group disconnected
  354. */
  355. public function testMethodGetSlavesOnEmptySlavePoolAsksSentinelForSlaves()
  356. {
  357. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  358. $sentinel1
  359. ->expects($this->at(0))
  360. ->method('executeCommand')
  361. ->with($this->isRedisCommand(
  362. 'SENTINEL', array('slaves', 'svc')
  363. ))
  364. ->will($this->returnValue(
  365. array(
  366. array(
  367. 'name', '127.0.0.1:6382',
  368. 'ip', '127.0.0.1',
  369. 'port', '6382',
  370. 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
  371. 'flags', 'slave',
  372. 'master-host', '127.0.0.1',
  373. 'master-port', '6381',
  374. ),
  375. array(
  376. 'name', '127.0.0.1:6383',
  377. 'ip', '127.0.0.1',
  378. 'port', '6383',
  379. 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
  380. 'flags', 'slave',
  381. 'master-host', '127.0.0.1',
  382. 'master-port', '6381',
  383. ),
  384. )
  385. ));
  386. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  387. $slaves = $replication->getSlaves();
  388. $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
  389. $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
  390. }
  391. /**
  392. * @group disconnected
  393. * @expectedException Predis\ClientException
  394. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  395. */
  396. public function testMethodGetSlavesThrowsExceptionOnNoAvailableSentinels()
  397. {
  398. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  399. $sentinel1
  400. ->expects($this->any())
  401. ->method('executeCommand')
  402. ->with($this->isRedisCommand(
  403. 'SENTINEL', array('slaves', 'svc')
  404. ))
  405. ->will($this->throwException(
  406. new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
  407. ));
  408. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  409. $replication->getSlaves();
  410. }
  411. /**
  412. * @group disconnected
  413. * @expectedException Predis\ClientException
  414. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  415. */
  416. public function testMethodConnectThrowsExceptionOnConnectWithEmptySentinelsPool()
  417. {
  418. $replication = $this->getReplicationConnection('svc', array());
  419. $replication->connect();
  420. }
  421. /**
  422. * @group disconnected
  423. */
  424. public function testMethodConnectForcesConnectionToSlave()
  425. {
  426. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  427. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  428. $master
  429. ->expects($this->never())
  430. ->method('connect');
  431. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  432. $slave1
  433. ->expects($this->once())
  434. ->method('connect');
  435. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  436. $replication->add($master);
  437. $replication->add($slave1);
  438. $replication->connect();
  439. }
  440. /**
  441. * @group disconnected
  442. */
  443. public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToSlave()
  444. {
  445. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  446. $sentinel1
  447. ->expects($this->any())
  448. ->method('executeCommand')
  449. ->with($this->isRedisCommand(
  450. 'SENTINEL', array('slaves', 'svc')
  451. ))
  452. ->will($this->returnValue(
  453. array(
  454. array(
  455. 'name', '127.0.0.1:6382',
  456. 'ip', '127.0.0.1',
  457. 'port', '6382',
  458. 'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
  459. 'flags', 'slave',
  460. 'master-host', '127.0.0.1',
  461. 'master-port', '6381',
  462. ),
  463. )
  464. ));
  465. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  466. $master
  467. ->expects($this->never())
  468. ->method('connect');
  469. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  470. $slave1
  471. ->expects($this->once())
  472. ->method('connect');
  473. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  474. $factory
  475. ->expects($this->once())
  476. ->method('create')
  477. ->with(array(
  478. 'host' => '127.0.0.1',
  479. 'port' => '6382',
  480. 'alias' => 'slave-127.0.0.1:6382',
  481. ))
  482. ->will($this->returnValue($slave1));
  483. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  484. $replication->add($master);
  485. $replication->connect();
  486. }
  487. /**
  488. * @group disconnected
  489. */
  490. public function testMethodDisconnectForcesDisconnectionOnAllConnectionsInPool()
  491. {
  492. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  493. $sentinel1
  494. ->expects($this->never())
  495. ->method('disconnect');
  496. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  497. $master
  498. ->expects($this->once())
  499. ->method('disconnect');
  500. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  501. $slave1
  502. ->expects($this->once())
  503. ->method('disconnect');
  504. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  505. $slave2
  506. ->expects($this->once())
  507. ->method('disconnect');
  508. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  509. $replication->add($master);
  510. $replication->add($slave1);
  511. $replication->add($slave2);
  512. $replication->disconnect();
  513. }
  514. /**
  515. * @group disconnected
  516. */
  517. public function testMethodIsConnectedReturnConnectionStatusOfCurrentConnection()
  518. {
  519. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  520. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  521. $slave1
  522. ->expects($this->exactly(2))
  523. ->method('isConnected')
  524. ->will($this->onConsecutiveCalls(true, false));
  525. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  526. $replication->add($slave1);
  527. $this->assertFalse($replication->isConnected());
  528. $replication->connect();
  529. $this->assertTrue($replication->isConnected());
  530. $replication->getConnectionById('slave1')->disconnect();
  531. $this->assertFalse($replication->isConnected());
  532. }
  533. /**
  534. * @group disconnected
  535. */
  536. public function testMethodGetConnectionByIdReturnsConnectionWhenFound()
  537. {
  538. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  539. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  540. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  541. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  542. $replication->add($master);
  543. $replication->add($slave1);
  544. $this->assertSame($master, $replication->getConnectionById('master'));
  545. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  546. $this->assertNull($replication->getConnectionById('unknown'));
  547. }
  548. /**
  549. * @group disconnected
  550. */
  551. public function testMethodSwitchToSelectsCurrentConnectionByConnectionAlias()
  552. {
  553. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  554. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  555. $master
  556. ->expects($this->once())
  557. ->method('connect');
  558. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  559. $slave1
  560. ->expects($this->never())
  561. ->method('connect');
  562. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave2');
  563. $slave2
  564. ->expects($this->once())
  565. ->method('connect');
  566. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  567. $replication->add($master);
  568. $replication->add($slave1);
  569. $replication->add($slave2);
  570. $replication->switchTo('master');
  571. $this->assertSame($master, $replication->getCurrent());
  572. $replication->switchTo('slave2');
  573. $this->assertSame($slave2, $replication->getCurrent());
  574. }
  575. /**
  576. * @group disconnected
  577. * @expectedException InvalidArgumentException
  578. * @expectedExceptionMessage Invalid connection or connection not found.
  579. */
  580. public function testMethodSwitchToThrowsExceptionOnConnectionNotFound()
  581. {
  582. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  583. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  584. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  585. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  586. $replication->add($master);
  587. $replication->add($slave1);
  588. $replication->switchTo('unknown');
  589. }
  590. /**
  591. * @group disconnected
  592. */
  593. public function testMethodSwitchToMasterSelectsCurrentConnectionToMaster()
  594. {
  595. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  596. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  597. $master
  598. ->expects($this->once())
  599. ->method('connect');
  600. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  601. $slave1
  602. ->expects($this->never())
  603. ->method('connect');
  604. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  605. $replication->add($master);
  606. $replication->add($slave1);
  607. $replication->switchToMaster();
  608. $this->assertSame($master, $replication->getCurrent());
  609. }
  610. /**
  611. * @group disconnected
  612. */
  613. public function testMethodSwitchToSlaveSelectsCurrentConnectionToRandomSlave()
  614. {
  615. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  616. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  617. $master
  618. ->expects($this->never())
  619. ->method('connect');
  620. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  621. $slave1
  622. ->expects($this->once())
  623. ->method('connect');
  624. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  625. $replication->add($master);
  626. $replication->add($slave1);
  627. $replication->switchToSlave();
  628. $this->assertSame($slave1, $replication->getCurrent());
  629. }
  630. /**
  631. * @group disconnected
  632. */
  633. public function testGetConnectionReturnsMasterForWriteCommands()
  634. {
  635. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  636. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  637. $master
  638. ->expects($this->exactly(2))
  639. ->method('isConnected')
  640. ->will($this->onConsecutiveCalls(false, true));
  641. $master
  642. ->expects($this->at(2))
  643. ->method('executeCommand')
  644. ->with($this->isRedisCommand('ROLE'))
  645. ->will($this->returnValue(array(
  646. 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
  647. )));
  648. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  649. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  650. $replication->add($master);
  651. $replication->add($slave1);
  652. $this->assertSame($master, $replication->getConnection(
  653. Command\RawCommand::create('set', 'key', 'value')
  654. ));
  655. $this->assertSame($master, $replication->getConnection(
  656. Command\RawCommand::create('del', 'key')
  657. ));
  658. }
  659. /**
  660. * @group disconnected
  661. */
  662. public function testGetConnectionReturnsSlaveForReadOnlyCommands()
  663. {
  664. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  665. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  666. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  667. $slave1
  668. ->expects($this->exactly(2))
  669. ->method('isConnected')
  670. ->will($this->onConsecutiveCalls(false, true));
  671. $slave1
  672. ->expects($this->at(2))
  673. ->method('executeCommand')
  674. ->with($this->isRedisCommand('ROLE'))
  675. ->will($this->returnValue(array(
  676. 'slave', '127.0.0.1', 9000, 'connected', 3167038,
  677. )));
  678. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  679. $replication->add($master);
  680. $replication->add($slave1);
  681. $this->assertSame($slave1, $replication->getConnection(
  682. Command\RawCommand::create('get', 'key')
  683. ));
  684. $this->assertSame($slave1, $replication->getConnection(
  685. Command\RawCommand::create('exists', 'key')
  686. ));
  687. }
  688. /**
  689. * @group disconnected
  690. */
  691. public function testGetConnectionSwitchesToMasterAfterWriteCommand()
  692. {
  693. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  694. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  695. $master
  696. ->expects($this->exactly(2))
  697. ->method('isConnected')
  698. ->will($this->onConsecutiveCalls(false, true));
  699. $master
  700. ->expects($this->at(2))
  701. ->method('executeCommand')
  702. ->with($this->isRedisCommand('ROLE'))
  703. ->will($this->returnValue(array(
  704. 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
  705. )));
  706. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  707. $slave1
  708. ->expects($this->exactly(1))
  709. ->method('isConnected')
  710. ->will($this->onConsecutiveCalls(false));
  711. $slave1
  712. ->expects($this->at(2))
  713. ->method('executeCommand')
  714. ->with($this->isRedisCommand('ROLE'))
  715. ->will($this->returnValue(array(
  716. 'slave', '127.0.0.1', 9000, 'connected', 3167038,
  717. )));
  718. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  719. $replication->add($master);
  720. $replication->add($slave1);
  721. $this->assertSame($slave1, $replication->getConnection(
  722. Command\RawCommand::create('exists', 'key')
  723. ));
  724. $this->assertSame($master, $replication->getConnection(
  725. Command\RawCommand::create('set', 'key', 'value')
  726. ));
  727. $this->assertSame($master, $replication->getConnection(
  728. Command\RawCommand::create('get', 'key')
  729. ));
  730. }
  731. /**
  732. * @group disconnected
  733. * @expectedException Predis\Replication\RoleException
  734. * @expectedExceptionMessage Expected master but got slave [127.0.0.1:6381]
  735. */
  736. public function testGetConnectionThrowsExceptionOnNodeRoleMismatch()
  737. {
  738. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  739. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  740. $master
  741. ->expects($this->once())
  742. ->method('isConnected')
  743. ->will($this->returnValue(false));
  744. $master
  745. ->expects($this->at(2))
  746. ->method('executeCommand')
  747. ->with($this->isRedisCommand('ROLE'))
  748. ->will($this->returnValue(array(
  749. 'slave', '127.0.0.1', 9000, 'connected', 3167038,
  750. )));
  751. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  752. $replication->add($master);
  753. $replication->getConnection(Command\RawCommand::create('del', 'key'));
  754. }
  755. /**
  756. * @group disconnected
  757. */
  758. public function testGetConnectionReturnsMasterForReadOnlyOperationsOnUnavailableSlaves()
  759. {
  760. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  761. $sentinel1
  762. ->expects($this->once())
  763. ->method('executeCommand')
  764. ->with($this->isRedisCommand(
  765. 'SENTINEL', array('slaves', 'svc')
  766. ))
  767. ->will($this->returnValue(
  768. array(
  769. array(
  770. 'name', '127.0.0.1:6382',
  771. 'ip', '127.0.0.1',
  772. 'port', '6382',
  773. 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
  774. 'flags', 'slave,s_down,disconnected',
  775. 'master-host', '127.0.0.1',
  776. 'master-port', '6381',
  777. ),
  778. )
  779. ));
  780. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  781. $master
  782. ->expects($this->once())
  783. ->method('isConnected')
  784. ->will($this->returnValue(false));
  785. $master
  786. ->expects($this->at(2))
  787. ->method('executeCommand')
  788. ->with($this->isRedisCommand('ROLE'))
  789. ->will($this->returnValue(array(
  790. 'master', '0', array(),
  791. )));
  792. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  793. $replication->add($master);
  794. $replication->getConnection(Command\RawCommand::create('get', 'key'));
  795. }
  796. /**
  797. * @group disconnected
  798. */
  799. public function testMethodExecuteCommandSendsCommandToNodeAndReturnsResponse()
  800. {
  801. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  802. $cmdGet = Command\RawCommand::create('get', 'key');
  803. $cmdGetResponse = 'value';
  804. $cmdSet = Command\RawCommand::create('set', 'key', 'value');
  805. $cmdSetResponse = Response\Status::get('OK');
  806. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  807. $master
  808. ->expects($this->any())
  809. ->method('isConnected')
  810. ->will($this->returnValue(true));
  811. $master
  812. ->expects($this->at(2))
  813. ->method('executeCommand')
  814. ->with($this->isRedisCommand(
  815. 'SET', array('key', $cmdGetResponse)
  816. ))
  817. ->will($this->returnValue($cmdSetResponse));
  818. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  819. $slave1
  820. ->expects($this->any())
  821. ->method('isConnected')
  822. ->will($this->returnValue(true));
  823. $slave1
  824. ->expects($this->at(2))
  825. ->method('executeCommand')
  826. ->with($this->isRedisCommand(
  827. 'GET', array('key')
  828. ))
  829. ->will($this->returnValue($cmdGetResponse));
  830. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  831. $replication->add($master);
  832. $replication->add($slave1);
  833. $this->assertSame($cmdGetResponse, $replication->executeCommand($cmdGet));
  834. $this->assertSame($cmdSetResponse, $replication->executeCommand($cmdSet));
  835. }
  836. /**
  837. * @group disconnected
  838. */
  839. public function testMethodExecuteCommandRetriesReadOnlyCommandOnNextSlaveOnFailure()
  840. {
  841. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  842. $sentinel1
  843. ->expects($this->any())
  844. ->method('executeCommand')
  845. ->with($this->isRedisCommand(
  846. 'SENTINEL', array('slaves', 'svc')
  847. ))
  848. ->will($this->returnValue(
  849. array(
  850. array(
  851. 'name', '127.0.0.1:6383',
  852. 'ip', '127.0.0.1',
  853. 'port', '6383',
  854. 'runid', '1c0bf1291797fbc5608c07a17da394147dc62817',
  855. 'flags', 'slave',
  856. 'master-host', '127.0.0.1',
  857. 'master-port', '6381',
  858. ),
  859. )
  860. ));
  861. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  862. $master
  863. ->expects($this->any())
  864. ->method('isConnected')
  865. ->will($this->returnValue(true));
  866. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  867. $slave1
  868. ->expects($this->any())
  869. ->method('isConnected')
  870. ->will($this->returnValue(true));
  871. $slave1
  872. ->expects($this->at(2))
  873. ->method('executeCommand')
  874. ->with($this->isRedisCommand(
  875. 'GET', array('key')
  876. ))
  877. ->will($this->throwException(
  878. new Connection\ConnectionException($slave1, 'Unknown connection error [127.0.0.1:6382]')
  879. ));
  880. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  881. $slave2
  882. ->expects($this->any())
  883. ->method('isConnected')
  884. ->will($this->returnValue(true));
  885. $slave2
  886. ->expects($this->at(2))
  887. ->method('executeCommand')
  888. ->with($this->isRedisCommand(
  889. 'GET', array('key')
  890. ))
  891. ->will($this->returnValue('value'));
  892. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  893. $factory
  894. ->expects($this->once())
  895. ->method('create')
  896. ->with(array(
  897. 'host' => '127.0.0.1',
  898. 'port' => '6383',
  899. 'alias' => 'slave-127.0.0.1:6383',
  900. ))
  901. ->will($this->returnValue($slave2));
  902. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  903. $replication->add($master);
  904. $replication->add($slave1);
  905. $this->assertSame('value', $replication->executeCommand(
  906. Command\RawCommand::create('get', 'key')
  907. ));
  908. }
  909. /**
  910. * @group disconnected
  911. */
  912. public function testMethodExecuteCommandRetriesWriteCommandOnNewMasterOnFailure()
  913. {
  914. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  915. $sentinel1
  916. ->expects($this->any())
  917. ->method('executeCommand')
  918. ->with($this->isRedisCommand(
  919. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  920. ))
  921. ->will($this->returnValue(
  922. array('127.0.0.1', '6391')
  923. ));
  924. $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  925. $masterOld
  926. ->expects($this->any())
  927. ->method('isConnected')
  928. ->will($this->returnValue(true));
  929. $masterOld
  930. ->expects($this->at(2))
  931. ->method('executeCommand')
  932. ->with($this->isRedisCommand(
  933. 'DEL', array('key')
  934. ))
  935. ->will($this->throwException(
  936. new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]')
  937. ));
  938. $masterNew = $this->getMockConnection('tcp://127.0.0.1:6391?alias=master');
  939. $masterNew
  940. ->expects($this->any())
  941. ->method('isConnected')
  942. ->will($this->returnValue(true));
  943. $masterNew
  944. ->expects($this->at(2))
  945. ->method('executeCommand')
  946. ->with($this->isRedisCommand(
  947. 'DEL', array('key')
  948. ))
  949. ->will($this->returnValue(1));
  950. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  951. $factory
  952. ->expects($this->once())
  953. ->method('create')
  954. ->with(array(
  955. 'host' => '127.0.0.1',
  956. 'port' => '6391',
  957. 'alias' => 'master',
  958. ))
  959. ->will($this->returnValue($masterNew));
  960. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  961. $replication->add($masterOld);
  962. $this->assertSame(1, $replication->executeCommand(
  963. Command\RawCommand::create('del', 'key')
  964. ));
  965. }
  966. /**
  967. * @group disconnected
  968. * @expectedException Predis\Response\ServerException
  969. * @expectedExceptionMessage ERR No such master with that name
  970. */
  971. public function testMethodExecuteCommandThrowsExceptionOnUnknownServiceName()
  972. {
  973. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  974. $sentinel1
  975. ->expects($this->any())
  976. ->method('executeCommand')
  977. ->with($this->isRedisCommand(
  978. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  979. ))
  980. ->will($this->returnValue(null));
  981. $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  982. $masterOld
  983. ->expects($this->any())
  984. ->method('isConnected')
  985. ->will($this->returnValue(true));
  986. $masterOld
  987. ->expects($this->at(2))
  988. ->method('executeCommand')
  989. ->with($this->isRedisCommand(
  990. 'DEL', array('key')
  991. ))
  992. ->will($this->throwException(
  993. new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]')
  994. ));
  995. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  996. $replication->add($masterOld);
  997. $replication->executeCommand(
  998. Command\RawCommand::create('del', 'key')
  999. );
  1000. }
  1001. /**
  1002. * @group disconnected
  1003. * @expectedException Predis\ClientException
  1004. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  1005. */
  1006. public function testMethodExecuteCommandThrowsExceptionOnConnectionFailureAndNoAvailableSentinels()
  1007. {
  1008. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  1009. $sentinel1
  1010. ->expects($this->any())
  1011. ->method('executeCommand')
  1012. ->with($this->isRedisCommand(
  1013. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  1014. ))
  1015. ->will($this->throwException(
  1016. new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
  1017. ));
  1018. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  1019. $master
  1020. ->expects($this->any())
  1021. ->method('isConnected')
  1022. ->will($this->returnValue(true));
  1023. $master
  1024. ->expects($this->at(2))
  1025. ->method('executeCommand')
  1026. ->with($this->isRedisCommand(
  1027. 'DEL', array('key')
  1028. ))
  1029. ->will($this->throwException(
  1030. new Connection\ConnectionException($master, 'Unknown connection error [127.0.0.1:6381]')
  1031. ));
  1032. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  1033. $replication->add($master);
  1034. $replication->executeCommand(
  1035. Command\RawCommand::create('del', 'key')
  1036. );
  1037. }
  1038. /**
  1039. * @group disconnected
  1040. */
  1041. public function testMethodGetReplicationStrategyReturnsInstance()
  1042. {
  1043. $strategy = new Replication\ReplicationStrategy();
  1044. $factory = new Connection\Factory();
  1045. $replication = new SentinelReplication(
  1046. 'svc', array('tcp://127.0.0.1:5381?alias=sentinel1'), $factory, $strategy
  1047. );
  1048. $this->assertSame($strategy, $replication->getReplicationStrategy());
  1049. }
  1050. /**
  1051. * @group disconnected
  1052. */
  1053. public function testMethodSerializeCanSerializeWholeObject()
  1054. {
  1055. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  1056. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  1057. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  1058. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  1059. $strategy = new Replication\ReplicationStrategy();
  1060. $factory = new Connection\Factory();
  1061. $replication = new SentinelReplication('svc', array($sentinel1), $factory, $strategy);
  1062. $replication->add($master);
  1063. $replication->add($slave1);
  1064. $replication->add($slave2);
  1065. $unserialized = unserialize(serialize($replication));
  1066. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  1067. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  1068. $this->assertEquals($master, $unserialized->getConnectionById('slave2'));
  1069. $this->assertEquals($strategy, $unserialized->getReplicationStrategy());
  1070. }
  1071. // ******************************************************************** //
  1072. // ---- HELPER METHODS ------------------------------------------------ //
  1073. // ******************************************************************** //
  1074. /**
  1075. * Creates a new instance of replication connection.
  1076. *
  1077. * @param string $service Name of the service
  1078. * @param array $sentinels Array of sentinels
  1079. * @param ConnectionFactoryInterface|null $factory Optional connection factory instance.
  1080. *
  1081. * @return SentinelReplication
  1082. */
  1083. protected function getReplicationConnection($service, $sentinels, Connection\FactoryInterface $factory = null)
  1084. {
  1085. $factory = $factory ?: new Connection\Factory();
  1086. $replication = new SentinelReplication($service, $sentinels, $factory);
  1087. $replication->setRetryWait(0);
  1088. return $replication;
  1089. }
  1090. /**
  1091. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  1092. *
  1093. * @param mixed $parameters Optional parameters.
  1094. *
  1095. * @return mixed
  1096. */
  1097. protected function getMockSentinelConnection($parameters = null)
  1098. {
  1099. $connection = $this->getMockConnection($parameters);
  1100. return $connection;
  1101. }
  1102. }