mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 06:29:36 +00:00
first working version with simple WaitForContainerRunning implementation
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Testcontainers\Wait;
|
||||
|
||||
use Docker\API\Model\ContainersIdJsonGetResponse200;
|
||||
use Docker\Docker;
|
||||
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
|
||||
use Testcontainers\Exception\ContainerNotReadyException;
|
||||
|
||||
/**
|
||||
* Simply makes container inspect and checks if container is running.
|
||||
* Uses $timout and $pollInterval in milliseconds to set the parameters for waiting.
|
||||
*/
|
||||
class WaitForContainerRunning implements WaitInterface
|
||||
{
|
||||
protected Docker $dockerClient;
|
||||
|
||||
public function __construct(protected int $timeout = 10000, protected int $pollInterval = 500)
|
||||
{
|
||||
$this->dockerClient = ContainerRuntimeClient::getDockerClient();
|
||||
}
|
||||
|
||||
public function wait(string $id): void
|
||||
{
|
||||
$startTime = microtime(true) * 1000;
|
||||
|
||||
while (true) {
|
||||
$elapsedTime = (microtime(true) * 1000) - $startTime;
|
||||
|
||||
if ($elapsedTime > $this->timeout) {
|
||||
throw new ContainerNotReadyException($id);
|
||||
}
|
||||
|
||||
/** @var ContainersIdJsonGetResponse200 | null $containerInspect */
|
||||
$containerInspect = $this->dockerClient->containerInspect($id);
|
||||
$containerStatus = $containerInspect?->getState()?->getStatus();
|
||||
|
||||
if ($containerStatus === 'running') {
|
||||
return;
|
||||
}
|
||||
|
||||
usleep($this->pollInterval * 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,24 +5,48 @@ declare(strict_types=1);
|
||||
namespace Testcontainers\Wait;
|
||||
|
||||
use Docker\Docker;
|
||||
use Docker\DockerClientFactory;
|
||||
use Http\Client\Socket\Exception\TimeoutException;
|
||||
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
|
||||
use Testcontainers\Exception\ContainerNotReadyException;
|
||||
|
||||
class WaitForHealthCheck implements WaitInterface
|
||||
{
|
||||
protected Docker $dockerClient;
|
||||
protected int $timeout;
|
||||
protected int $pollInterval;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(int $timeout = 5000, int $pollInterval = 1000)
|
||||
{
|
||||
$this->dockerClient = Docker::create();
|
||||
$this->dockerClient = ContainerRuntimeClient::getDockerClient();
|
||||
$this->timeout = $timeout;
|
||||
$this->pollInterval = $pollInterval;
|
||||
}
|
||||
|
||||
public function wait(string $id): void
|
||||
{
|
||||
$containerInspect = $this->dockerClient->containerInspect($id);
|
||||
$containerInspect->getBody()->getContents();
|
||||
dd($containerInspect->getStatusCode());
|
||||
$startTime = microtime(true) * 1000;
|
||||
|
||||
if ($status !== 'healthy') {
|
||||
throw new ContainerNotReadyException($id);
|
||||
while (true) {
|
||||
$elapsedTime = (microtime(true) * 1000) - $startTime;
|
||||
|
||||
if ($elapsedTime > $this->timeout) {
|
||||
throw new TimeoutException(sprintf("Health check not healthy after %d ms", $this->timeout));
|
||||
}
|
||||
|
||||
$containerInspect = $this->dockerClient->containerInspect($id, [], Docker::FETCH_RESPONSE);
|
||||
//$containerStatus = $containerInspect?->getArrayCopy() ?? null;
|
||||
var_dump($containerInspect->getBody()->getContents());
|
||||
$containerStatus='';
|
||||
if ($containerStatus === 'healthy') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($containerStatus === 'unhealthy') {
|
||||
throw new ContainerNotReadyException(sprintf("Health check failed: %s", $containerStatus));
|
||||
}
|
||||
|
||||
usleep($this->pollInterval * 1000); // Sleep for the polling interval
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user