CredisSentinelTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. require_once dirname(__FILE__).'/../Client.php';
  3. require_once dirname(__FILE__).'/../Cluster.php';
  4. require_once dirname(__FILE__).'/../Sentinel.php';
  5. require_once dirname(__FILE__).'/CredisTestCommon.php';
  6. class CredisSentinelTest extends CredisTestCommon
  7. {
  8. /** @var Credis_Sentinel */
  9. protected $sentinel;
  10. protected $sentinelConfig;
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. if($this->sentinelConfig === NULL) {
  15. $configFile = dirname(__FILE__).'/sentinel_config.json';
  16. if( ! file_exists($configFile) || ! ($config = file_get_contents($configFile))) {
  17. $this->markTestSkipped('Could not load '.$configFile);
  18. return;
  19. }
  20. $this->sentinelConfig = json_decode($config);
  21. }
  22. $sentinelClient = new Credis_Client($this->sentinelConfig->host, $this->sentinelConfig->port);
  23. $this->sentinel = new Credis_Sentinel($sentinelClient);
  24. if($this->useStandalone) {
  25. $this->sentinel->forceStandalone();
  26. }
  27. $this->waitForSlaveReplication();
  28. }
  29. protected function tearDown()
  30. {
  31. if($this->sentinel) {
  32. $this->sentinel = NULL;
  33. }
  34. }
  35. public function testMasterClient()
  36. {
  37. $master = $this->sentinel->getMasterClient($this->sentinelConfig->clustername);
  38. $this->assertInstanceOf('Credis_Client',$master);
  39. $this->assertEquals($this->redisConfig[0]['port'],$master->getPort());
  40. $this->setExpectedExceptionShim('CredisException','Master not found');
  41. $this->sentinel->getMasterClient('non-existing-cluster');
  42. }
  43. public function testMasters()
  44. {
  45. $masters = $this->sentinel->masters();
  46. $this->assertInternalType('array',$masters);
  47. $this->assertCount(2,$masters);
  48. $this->assertArrayHasKey(0,$masters);
  49. $this->assertArrayHasKey(1,$masters);
  50. $this->assertArrayHasKey(1,$masters[0]);
  51. $this->assertArrayHasKey(1,$masters[1]);
  52. $this->assertArrayHasKey(5,$masters[1]);
  53. if($masters[0][1] == 'masterdown'){
  54. $this->assertEquals($this->sentinelConfig->clustername,$masters[1][1]);
  55. $this->assertEquals($this->redisConfig[0]['port'],$masters[1][5]);
  56. } else {
  57. $this->assertEquals('masterdown',$masters[1][1]);
  58. $this->assertEquals($this->sentinelConfig->clustername,$masters[0][1]);
  59. $this->assertEquals($this->redisConfig[0]['port'],$masters[0][5]);
  60. }
  61. }
  62. public function testMaster()
  63. {
  64. $master = $this->sentinel->master($this->sentinelConfig->clustername);
  65. $this->assertInternalType('array',$master);
  66. $this->assertArrayHasKey(1,$master);
  67. $this->assertArrayHasKey(5,$master);
  68. $this->assertEquals($this->sentinelConfig->clustername,$master[1]);
  69. $this->assertEquals($this->redisConfig[0]['port'],$master[5]);
  70. $this->setExpectedExceptionShim('CredisException','No such master with that name');
  71. $this->sentinel->master('non-existing-cluster');
  72. }
  73. public function testSlaveClient()
  74. {
  75. $slaves = $this->sentinel->getSlaveClients($this->sentinelConfig->clustername);
  76. $this->assertInternalType('array',$slaves);
  77. $this->assertCount(1,$slaves);
  78. foreach($slaves as $slave){
  79. $this->assertInstanceOf('Credis_Client',$slave);
  80. }
  81. $this->setExpectedExceptionShim('CredisException','No such master with that name');
  82. $this->sentinel->getSlaveClients('non-existing-cluster');
  83. }
  84. public function testSlaves()
  85. {
  86. $slaves = $this->sentinel->slaves($this->sentinelConfig->clustername);
  87. $this->assertInternalType('array',$slaves);
  88. $this->assertCount(1,$slaves);
  89. $this->assertArrayHasKey(0,$slaves);
  90. $this->assertArrayHasKey(5,$slaves[0]);
  91. $this->assertEquals(6385,$slaves[0][5]);
  92. $slaves = $this->sentinel->slaves('masterdown');
  93. $this->assertInternalType('array',$slaves);
  94. $this->assertCount(0,$slaves);
  95. $this->setExpectedExceptionShim('CredisException','No such master with that name');
  96. $this->sentinel->slaves('non-existing-cluster');
  97. }
  98. public function testNonExistingClusterNameWhenCreatingSlaves()
  99. {
  100. $this->setExpectedExceptionShim('CredisException','No such master with that name');
  101. $this->sentinel->createSlaveClients('non-existing-cluster');
  102. }
  103. public function testCreateCluster()
  104. {
  105. $cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername);
  106. $this->assertInstanceOf('Credis_Cluster',$cluster);
  107. $this->assertCount(2,$cluster->clients());
  108. $cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername,0,1,false);
  109. $this->assertInstanceOf('Credis_Cluster',$cluster);
  110. $this->assertCount(2,$cluster->clients());
  111. $this->setExpectedExceptionShim('CredisException','The master is down');
  112. $this->sentinel->createCluster($this->sentinelConfig->downclustername);
  113. }
  114. public function testGetCluster()
  115. {
  116. $cluster = $this->sentinel->getCluster($this->sentinelConfig->clustername);
  117. $this->assertInstanceOf('Credis_Cluster',$cluster);
  118. $this->assertCount(2,$cluster->clients());
  119. }
  120. public function testGetClusterOnDbNumber2()
  121. {
  122. $cluster = $this->sentinel->getCluster($this->sentinelConfig->clustername,2);
  123. $this->assertInstanceOf('Credis_Cluster',$cluster);
  124. $this->assertCount(2,$cluster->clients());
  125. $clients = $cluster->clients();
  126. $this->assertEquals(2,$clients[0]->getSelectedDb());
  127. $this->assertEquals(2,$clients[1]->getSelectedDb());
  128. }
  129. public function testGetMasterAddressByName()
  130. {
  131. $address = $this->sentinel->getMasterAddressByName($this->sentinelConfig->clustername);
  132. $this->assertInternalType('array',$address);
  133. $this->assertCount(2,$address);
  134. $this->assertArrayHasKey(0,$address);
  135. $this->assertArrayHasKey(1,$address);
  136. $this->assertEquals($this->redisConfig[0]['host'],$address[0]);
  137. $this->assertEquals($this->redisConfig[0]['port'],$address[1]);
  138. }
  139. public function testPing()
  140. {
  141. $pong = $this->sentinel->ping();
  142. $this->assertEquals("PONG",$pong);
  143. }
  144. public function testGetHostAndPort()
  145. {
  146. $host = 'localhost';
  147. $port = '123456';
  148. $client = $this->createMockShim('\Credis_Client');
  149. $sentinel = new Credis_Sentinel($client);
  150. $client->expects($this->once())->method('getHost')->willReturn($host);
  151. $client->expects($this->once())->method('getPort')->willReturn($port);
  152. $this->assertEquals($host, $sentinel->getHost());
  153. $this->assertEquals($port, $sentinel->getPort());
  154. }
  155. public function testNonExistingMethod()
  156. {
  157. $this->setExpectedExceptionShim('CredisException','Unknown sentinel subcommand \'bla\'');
  158. $this->sentinel->bla();
  159. }
  160. }