implement WaitForHttp and WaitForHostPort strategies

This commit is contained in:
Sergei Shitikov
2024-10-24 19:36:48 +02:00
parent a5c8d2bb73
commit 1cbfe638b9
5 changed files with 195 additions and 114 deletions
+12 -31
View File
@@ -4,42 +4,23 @@ declare(strict_types=1);
namespace Testcontainers\Wait;
use Docker\Docker;
use JsonException;
use RuntimeException;
use Testcontainers\Exception\ContainerNotReadyException;
//TODO: not ready yet
final class WaitForTcpPortOpen implements WaitStrategy
/**
* @deprecated Use WaitForHostPort instead
* Kept for backward compatibility
* Should be removed in next major version
*/
final class WaitForTcpPortOpen extends WaitForHostPort
{
private Docker $dockerClient;
public function __construct(private readonly int $port, private readonly ?string $network = null)
/**
* @phpstan-ignore-next-line
*/
public function __construct(int $port, string $network = null)
{
$this->dockerClient = Docker::create();
parent::__construct($port);
}
public static function make(int $port, ?string $network = null): self
{
return new self($port, $network);
}
/**
* @throws JsonException
*/
public function wait(string $id): void
{
$containerInspectResult = $this->dockerClient->containerInspect($id);
$dockerContainerNetworks = $containerInspectResult->getNetworkSettings()->getNetworks();
$dockerContainerAddress = '';
foreach ($dockerContainerNetworks as $network) {
if ($network->getNetworkID() === $this->network) {
$dockerContainerAddress = $network->getIPAddress();
break;
}
}
if (@fsockopen($dockerContainerAddress, $this->port) === false) {
throw new ContainerNotReadyException($id, new RuntimeException('Unable to connect to container TCP port'));
}
return new self($port);
}
}