Skip to content

Commit

Permalink
Added new configuration option: wait_until_ready in microseconds (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcustiel authored Sep 17, 2021
1 parent 34df2ac commit d079ff5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion codeception.https.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/Extension/Phiremock.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function setDefaultLogsPath(): void

private function waitUntilReady(): void
{
if (!$this->extensionConfig->isWaitUntilReady()) {
if (!$this->extensionConfig->waitUntilReady()) {
return;
}

Expand All @@ -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!');
}
}
38 changes: 24 additions & 14 deletions src/PhiremockExtension/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -184,7 +189,7 @@ public function isSecure(): bool
&& $this->getCertificateKeyPath() !== null;
}

public function isWaitUntilReady(): bool
public function waitUntilReady(): bool
{
return $this->waitUntilReady;
}
Expand All @@ -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
{
Expand Down

0 comments on commit d079ff5

Please sign in to comment.