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
+51
View File
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Testcontainers\Wait;
use Testcontainers\Container\StartedTestContainer;
use Testcontainers\Exception\ContainerWaitingTimeoutException;
class WaitForHostPort extends BaseWaitStrategy
{
public function __construct(
protected int $port,
int $timeout = 10000,
int $pollInterval = 500
) {
parent::__construct($timeout, $pollInterval);
}
public function wait(StartedTestContainer $container): void
{
$startTime = microtime(true) * 1000;
$containerAddress = $container->getHost();
while (true) {
$elapsedTime = (microtime(true) * 1000) - $startTime;
if ($elapsedTime > $this->timeout) {
throw new ContainerWaitingTimeoutException($container->getId());
}
if ($this->isPortOpen($containerAddress, $this->port)) {
return; // Port is open, container is ready
}
usleep($this->pollInterval * 1000); // Wait for the next polling interval
}
}
private function isPortOpen(string $ipAddress, int $port): bool
{
$connection = @fsockopen($ipAddress, $port, $errno, $errstr, 2);
if ($connection !== false) {
fclose($connection);
return true;
}
return false;
}
}