From d079ff54c9468f09edae3617d5c33d79171f647a Mon Sep 17 00:00:00 2001 From: Mariano Custiel Date: Fri, 17 Sep 2021 22:47:43 +0200 Subject: [PATCH] Added new configuration option: wait_until_ready in microseconds (#50) --- README.md | 3 ++- codeception.https.yml | 5 +++- src/Extension/Phiremock.php | 11 ++++----- src/PhiremockExtension/Config.php | 38 +++++++++++++++++++------------ 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 63a6de3..fcbd77b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ extensions: logs_path: /var/log/my_app/tests/logs # defaults to codeception's tests output dir debug: true # defaults to false wait_until_ready: true # defaults to false - wait_until_ready_timeout: 15 # defaults to 30 + wait_until_ready_timeout: 15 # (seconds) defaults to 30 + wait_until_ready_interval: 100 # (microseconds) defaults to 50000 expectations_path: /my/expectations/path # defaults to tests/_expectations server_factory: \My\FactoryClass # defaults to 'default' extra_instances: [] # deaults to an empty array diff --git a/codeception.https.yml b/codeception.https.yml index aaaa471..bfa45f0 100644 --- a/codeception.https.yml +++ b/codeception.https.yml @@ -20,7 +20,10 @@ extensions: server_factory: Mcustiel\Phiremock\Codeception\Module\Tests\Helpers\FactoryWithGuzzle7 logs_path: tests/_output/phiremock.extra.log debug: true - start_delay: 1 + start_delay: 0 + wait_until_ready: true + wait_until_ready_timeout: 5 + wait_until_ready_interval: 10000 extra_instances: - listen: 0.0.0.0:18443 diff --git a/src/Extension/Phiremock.php b/src/Extension/Phiremock.php index 3657ecc..d6d2e78 100644 --- a/src/Extension/Phiremock.php +++ b/src/Extension/Phiremock.php @@ -113,7 +113,7 @@ private function setDefaultLogsPath(): void private function waitUntilReady(): void { - if (!$this->extensionConfig->isWaitUntilReady()) { + if (!$this->extensionConfig->waitUntilReady()) { return; } @@ -126,22 +126,21 @@ private function waitUntilReady(): void ); $start = \microtime(true); - + $interval = $this->extensionConfig->getWaitUntilReadyIntervalMicros(); + $timeout = $this->extensionConfig->getWaitUntilReadyTimeout(); while (true) { if ($readinessChecker->isReady()) { break; } - - \sleep(1); + \usleep($interval); $elapsed = (int) (\microtime(true) - $start); - if ($elapsed > $this->extensionConfig->getWaitUntilReadyTimeout()) { + if ($elapsed > $timeout) { throw new \RuntimeException( \sprintf('Phiremock failed to start within %d seconds', $this->extensionConfig->getWaitUntilReadyTimeout()) ); } } - $this->writeln('Phiremock is ready!'); } } diff --git a/src/PhiremockExtension/Config.php b/src/PhiremockExtension/Config.php index 41665e4..72acb13 100644 --- a/src/PhiremockExtension/Config.php +++ b/src/PhiremockExtension/Config.php @@ -38,21 +38,23 @@ class Config public const DEFAULT_SUITES = []; public const DEFAULT_WAIT_UNTIL_READY = false; public const DEFAULT_WAIT_UNTIL_READY_TIMEOUT = 30; + public const DEFAULT_WAIT_UNTIL_READY_INTERVAL_MICROS = 50000; public const DEFAULT_CONFIG = [ - 'listen' => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT, - 'debug' => self::DEFAULT_DEBUG_MODE, - 'start_delay' => self::DEFAULT_DELAY, - 'bin_path' => self::DEFAULT_PHIREMOCK_PATH, - 'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH, - 'server_factory' => self::DEFAULT_SERVER_FACTORY, - 'certificate' => self::DEFAULT_CERTIFICATE, - 'certificate_key' => self::DEFAULT_CERTIFICATE_KEY, - 'cert_passphrase' => self::DEFAULT_CERTIFICATE_PASSPHRASE, - 'extra_instances' => self::DEFAULT_EXTRA_INSTANCES, - 'suites' => self::DEFAULT_SUITES, - 'wait_until_ready' => self::DEFAULT_WAIT_UNTIL_READY, - 'wait_until_ready_timeout' => self::DEFAULT_WAIT_UNTIL_READY_TIMEOUT + 'listen' => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT, + 'debug' => self::DEFAULT_DEBUG_MODE, + 'start_delay' => self::DEFAULT_DELAY, + 'bin_path' => self::DEFAULT_PHIREMOCK_PATH, + 'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH, + 'server_factory' => self::DEFAULT_SERVER_FACTORY, + 'certificate' => self::DEFAULT_CERTIFICATE, + 'certificate_key' => self::DEFAULT_CERTIFICATE_KEY, + 'cert_passphrase' => self::DEFAULT_CERTIFICATE_PASSPHRASE, + 'extra_instances' => self::DEFAULT_EXTRA_INSTANCES, + 'suites' => self::DEFAULT_SUITES, + 'wait_until_ready' => self::DEFAULT_WAIT_UNTIL_READY, + 'wait_until_ready_timeout' => self::DEFAULT_WAIT_UNTIL_READY_TIMEOUT, + 'wait_until_ready_interval' => self::DEFAULT_WAIT_UNTIL_READY_INTERVAL_MICROS, ]; /** @var string */ @@ -87,6 +89,8 @@ class Config private $waitUntilReady; /** @var int */ private $waitUntilReadyTimeout; + /** @var int */ + private $waitUntilReadyCheckIntervalMicros; /** @throws ConfigurationException */ public function __construct(array $config, callable $output) @@ -106,6 +110,7 @@ public function __construct(array $config, callable $output) $this->suites = $config['suites']; $this->waitUntilReady = (bool) $config['wait_until_ready']; $this->waitUntilReadyTimeout = (int) $config['wait_until_ready_timeout']; + $this->waitUntilReadyCheckIntervalMicros = (int) $config['wait_until_ready_interval']; } public function getSuites(): array @@ -184,7 +189,7 @@ public function isSecure(): bool && $this->getCertificateKeyPath() !== null; } - public function isWaitUntilReady(): bool + public function waitUntilReady(): bool { return $this->waitUntilReady; } @@ -194,6 +199,11 @@ public function getWaitUntilReadyTimeout(): int return $this->waitUntilReadyTimeout; } + public function getWaitUntilReadyIntervalMicros(): int + { + return $this->waitUntilReadyCheckIntervalMicros; + } + /** @throws ConfigurationException */ public static function getDefaultLogsPath(): string {