PageRenderTime 25ms CodeModel.GetById 49ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/CredisTestCommon.php

https://github.com/colinmollenhour/credis
PHP | 181 lines | 156 code | 12 blank | 13 comment | 36 complexity | b5eeeb13d6d8fb993ea692c889b7368f MD5 | raw file
  1. <?php
  2. // backward compatibility (https://stackoverflow.com/a/42828632/187780)
  3. if (!class_exists('\PHPUnit\Framework\TestCase') && class_exists('\PHPUnit_Framework_TestCase')) {
  4. class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
  5. }
  6. if (version_compare(phpversion(), '8.0.0', '>=')) {
  7. include 'phpunit-shims/php8.php';
  8. } else {
  9. include 'phpunit-shims/php7.php';
  10. }
  11. class CredisTestCommon extends CredisTestCommonShim
  12. {
  13. protected $useStandalone = false;
  14. protected $redisConfig = null;
  15. protected $slaveConfig = null;
  16. protected function setUpInternal()
  17. {
  18. if ($this->redisConfig === null)
  19. {
  20. $configFile = dirname(__FILE__) . '/redis_config.json';
  21. if (!file_exists($configFile) || !($config = file_get_contents($configFile)))
  22. {
  23. $this->markTestSkipped('Could not load ' . $configFile);
  24. return;
  25. }
  26. $this->redisConfig = json_decode($config);
  27. $arrayConfig = array();
  28. foreach ($this->redisConfig as $config)
  29. {
  30. $arrayConfig[] = (array)$config;
  31. }
  32. $this->redisConfig = $arrayConfig;
  33. }
  34. if(!$this->useStandalone && !extension_loaded('redis')) {
  35. $this->fail('The Redis extension is not loaded.');
  36. }
  37. }
  38. /**
  39. * Verifies the slave has connected to the master and replication has caught up
  40. *
  41. * @return bool
  42. */
  43. protected function waitForSlaveReplication()
  44. {
  45. if ($this->slaveConfig === null)
  46. {
  47. foreach ($this->redisConfig as $config)
  48. {
  49. if ($config['alias'] === 'slave')
  50. {
  51. $this->slaveConfig = $config;
  52. break;
  53. }
  54. }
  55. if ($this->slaveConfig === null)
  56. {
  57. $this->markTestSkipped('Could not load slave config');
  58. return false;
  59. }
  60. }
  61. $masterConfig = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port']);
  62. $masterConfig->forceStandalone();
  63. $slaveConfig = new Credis_Client($this->slaveConfig['host'], $this->slaveConfig['port']);
  64. $slaveConfig->forceStandalone();
  65. $start = microtime(true);
  66. $timeout = $start + 60;
  67. while (microtime(true) < $timeout)
  68. {
  69. usleep(100);
  70. $role = $slaveConfig->role();
  71. if ($role[0] !== 'slave')
  72. {
  73. $this->markTestSkipped('slave config does not points to a slave');
  74. return false;
  75. }
  76. if ($role[3] === 'connected')
  77. {
  78. $masterRole = $masterConfig->role();
  79. if ($masterRole[0] !== 'master')
  80. {
  81. $this->markTestSkipped('master config does not points to a master');
  82. return false;
  83. }
  84. if ($role[4] >= $masterRole[1])
  85. {
  86. return true;
  87. }
  88. }
  89. }
  90. // shouldn't get here
  91. $this->fail("Timeout (".(microtime(true) - $start)." seconds) waiting for master-slave replication to finalize");
  92. return false;
  93. }
  94. public static function setUpBeforeClassInternal()
  95. {
  96. if(preg_match('/^WIN/',strtoupper(PHP_OS))){
  97. echo "Unit tests will not work automatically on Windows. Please setup all Redis instances manually:".PHP_EOL;
  98. echo "\tredis-server redis-master.conf".PHP_EOL;
  99. echo "\tredis-server redis-slave.conf".PHP_EOL;
  100. echo "\tredis-server redis-2.conf".PHP_EOL;
  101. echo "\tredis-server redis-3.conf".PHP_EOL;
  102. echo "\tredis-server redis-4.conf".PHP_EOL;
  103. echo "\tredis-server redis-auth.conf".PHP_EOL;
  104. echo "\tredis-server redis-socket.conf".PHP_EOL.PHP_EOL;
  105. } else {
  106. chdir(__DIR__.'/../');
  107. if (!file_exists('./tests/tls/ca.crt') || !file_exists('./tests/tls/server.crt')) {
  108. // generate SSL keys
  109. system('./tests/gen-test-certs.sh');
  110. }
  111. chdir(__DIR__);
  112. $directoryIterator = new DirectoryIterator(__DIR__);
  113. foreach($directoryIterator as $item){
  114. if(!$item->isfile() || !preg_match('/^redis\-(.+)\.conf$/',$item->getFilename()) || $item->getFilename() == 'redis-sentinel.conf'){
  115. continue;
  116. }
  117. exec('redis-server '.$item->getFilename());
  118. }
  119. copy('redis-master.conf','redis-master.conf.bak');
  120. copy('redis-slave.conf','redis-slave.conf.bak');
  121. // wait for redis instances to initialize
  122. sleep(1);
  123. }
  124. }
  125. public static function tearDownAfterClassInternal()
  126. {
  127. if(preg_match('/^WIN/',strtoupper(PHP_OS))){
  128. echo "Please kill all Redis instances manually:".PHP_EOL;
  129. } else {
  130. chdir(__DIR__);
  131. $directoryIterator = new DirectoryIterator(__DIR__);
  132. foreach($directoryIterator as $item){
  133. if(!$item->isfile() || !preg_match('/^redis\-(.+)\.pid$/',$item->getFilename())){
  134. continue;
  135. }
  136. $pid = trim(file_get_contents($item->getFilename()));
  137. if(function_exists('posix_kill')){
  138. posix_kill($pid,15);
  139. } else {
  140. exec('kill '.$pid);
  141. }
  142. }
  143. sleep(1); // give teardown some time to finish
  144. @unlink('dump.rdb');
  145. @unlink('redis-master.conf');
  146. @unlink('redis-slave.conf');
  147. @copy('redis-master.conf.bak','redis-master.conf');
  148. @copy('redis-slave.conf.bak','redis-slave.conf');
  149. }
  150. }
  151. /**
  152. * php 7.2 compat fix, as directly polyfilling for older PHPUnit causes a function signature compatibility issue
  153. * This is due to the defined return type
  154. */
  155. public function setExpectedExceptionShim($class, $message = NULL, $code = NULL)
  156. {
  157. if (method_exists($this, 'setExpectedException')) {
  158. $this->setExpectedException($class, $message, $code);
  159. } else {
  160. parent::expectException($class);
  161. if ($message !== null) {
  162. $this->expectExceptionMessage($message);
  163. }
  164. if ($code !== null) {
  165. $this->expectExceptionCode($code);
  166. }
  167. }
  168. }
  169. }