Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Container/StartedGenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
?->getBody()
->getContents() ?? '';

return $this->sanitizeOutput(mb_convert_encoding($output, 'UTF-8', 'UTF-8'));

Check failure on line 101 in src/Container/StartedGenericContainer.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Parameter #1 $output of method Testcontainers\Container\StartedGenericContainer::sanitizeOutput() expects string, string|false given.
}

public function getHost(): string
Expand Down Expand Up @@ -198,6 +198,13 @@
*/
public function getBoundPorts(): iterable
{
/**
* For some reason, in the latest Docker releases, at this moment, the container might not be fully started.
* This can lead to issues when trying to retrieve the ports.
* TODO: find a better strategy to ensure the container is fully started or run in a loop until it is ready.
* For the loop $this->inspect() shouldn't be cached.
*/
usleep(300 * 1000);
$ports = $this->inspect()?->getNetworkSettings()?->getPorts();

if ($ports === null) {
Expand Down
26 changes: 19 additions & 7 deletions src/Wait/WaitForHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WaitForHttp extends BaseWaitStrategy
protected int $readTimeout = 1000;

public function __construct(
protected int $port,
protected ?int $port = null,
int $timeout = 10000,
int $pollInterval = 500
) {
Expand Down Expand Up @@ -100,13 +100,18 @@ public function wait(StartedTestContainer $container): void
throw new ContainerWaitingTimeoutException($container->getId());
}

$containerAddress = $container->getHost();
try {
$this->resolvePort($container);
$containerAddress = $container->getHost();

$url = sprintf('%s://%s:%d%s', $this->protocol, $containerAddress, $this->port, $this->path);
$responseCode = $this->makeHttpRequest($url);
$url = sprintf('%s://%s:%d%s', $this->protocol, $containerAddress, $this->port, $this->path);
$responseCode = $this->makeHttpRequest($url);

if ($responseCode === $this->expectedStatusCode) {
return; // Container is ready
if ($responseCode === $this->expectedStatusCode) {
return; // Container is ready
}
} catch (\RuntimeException) {
// Port resolution or HTTP request failed, we'll try again until timeout
}

usleep($this->pollInterval * 1000);
Expand All @@ -120,7 +125,7 @@ private function makeHttpRequest(string $url): int
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method->value);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // No need for response body, just headers
curl_setopt($ch, CURLOPT_NOBODY, true); // No need for a response body, just headers
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->readTimeout);

// Allow insecure connections if requested
Expand All @@ -139,4 +144,11 @@ private function makeHttpRequest(string $url): int

return $responseCode;
}

private function resolvePort(StartedTestContainer $container): void
{
if ($this->port === null) {
$this->port = $container->getFirstMappedPort();
}
}
}
Loading