mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 16:29:29 +00:00
Improve retry logic on start(), improved portBindings types
This commit is contained in:
@@ -47,6 +47,9 @@ class GenericContainer implements TestContainer
|
|||||||
protected bool $isPrivileged = false;
|
protected bool $isPrivileged = false;
|
||||||
protected ?string $networkName = null;
|
protected ?string $networkName = null;
|
||||||
|
|
||||||
|
protected int $startAttempts = 0;
|
||||||
|
protected const MAX_START_ATTEMPTS = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array<Mount>
|
* @var array<Mount>
|
||||||
*/
|
*/
|
||||||
@@ -173,13 +176,18 @@ class GenericContainer implements TestContainer
|
|||||||
|
|
||||||
public function start(): StartedGenericContainer
|
public function start(): StartedGenericContainer
|
||||||
{
|
{
|
||||||
|
$this->startAttempts++;
|
||||||
$containerConfig = $this->createContainerConfig();
|
$containerConfig = $this->createContainerConfig();
|
||||||
try {
|
try {
|
||||||
/** @var ContainerCreateResponse|null $containerCreateResponse */
|
/** @var ContainerCreateResponse|null $containerCreateResponse */
|
||||||
$containerCreateResponse = $this->dockerClient->containerCreate($containerConfig);
|
$containerCreateResponse = $this->dockerClient->containerCreate($containerConfig);
|
||||||
$this->id = $containerCreateResponse?->getId() ?? '';
|
$this->id = $containerCreateResponse?->getId() ?? '';
|
||||||
} catch (ContainerCreateNotFoundException) {
|
} catch (ContainerCreateNotFoundException) {
|
||||||
|
if ($this->startAttempts >= self::MAX_START_ATTEMPTS) {
|
||||||
|
throw new \RuntimeException("Failed to start container after pulling image.");
|
||||||
|
}
|
||||||
// If the image is not found, pull it and try again
|
// If the image is not found, pull it and try again
|
||||||
|
// TODO: add withPullPolicy support
|
||||||
$this->pullImage();
|
$this->pullImage();
|
||||||
return $this->start();
|
return $this->start();
|
||||||
}
|
}
|
||||||
@@ -254,12 +262,12 @@ class GenericContainer implements TestContainer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \ArrayObject<string, list<PortBinding>>
|
* @return array<string, array<int, PortBinding>>
|
||||||
*/
|
*/
|
||||||
protected function createPortBindings(): \ArrayObject
|
protected function createPortBindings(): array
|
||||||
{
|
{
|
||||||
$portGenerator = new RandomUniquePortGenerator();
|
$portGenerator = new RandomUniquePortGenerator();
|
||||||
$portBindings = new \ArrayObject();
|
$portBindings = [];
|
||||||
|
|
||||||
foreach ($this->exposedPorts as $port) {
|
foreach ($this->exposedPorts as $port) {
|
||||||
$portBinding = new PortBinding();
|
$portBinding = new PortBinding();
|
||||||
|
|||||||
Reference in New Issue
Block a user