From 2f1c8750e0eb8330b998cfc8a1561e2f59460a99 Mon Sep 17 00:00:00 2001 From: Sergei Shitikov Date: Sun, 29 Sep 2024 13:28:47 +0200 Subject: [PATCH] Improve retry logic on start(), improved portBindings types --- src/Container/GenericContainer.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index 8bb17d7..7413c56 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -47,6 +47,9 @@ class GenericContainer implements TestContainer protected bool $isPrivileged = false; protected ?string $networkName = null; + protected int $startAttempts = 0; + protected const MAX_START_ATTEMPTS = 2; + /** * @var array */ @@ -173,13 +176,18 @@ class GenericContainer implements TestContainer public function start(): StartedGenericContainer { + $this->startAttempts++; $containerConfig = $this->createContainerConfig(); try { /** @var ContainerCreateResponse|null $containerCreateResponse */ $containerCreateResponse = $this->dockerClient->containerCreate($containerConfig); $this->id = $containerCreateResponse?->getId() ?? ''; } 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 + // TODO: add withPullPolicy support $this->pullImage(); return $this->start(); } @@ -254,12 +262,12 @@ class GenericContainer implements TestContainer } /** - * @return \ArrayObject> + * @return array> */ - protected function createPortBindings(): \ArrayObject + protected function createPortBindings(): array { $portGenerator = new RandomUniquePortGenerator(); - $portBindings = new \ArrayObject(); + $portBindings = []; foreach ($this->exposedPorts as $port) { $portBinding = new PortBinding();