From add935ec915f1f11dc127952f05b24ffb65fc710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Fri, 13 Oct 2023 09:54:57 +0200 Subject: [PATCH] check fort port ready --- lib/port_checker.ex | 33 +++++++++++++++++++++++++++++++++ lib/reaper.ex | 1 + test/simple_test.exs | 2 ++ 3 files changed, 36 insertions(+) create mode 100644 lib/port_checker.ex diff --git a/lib/port_checker.ex b/lib/port_checker.ex new file mode 100644 index 0000000..24b85d1 --- /dev/null +++ b/lib/port_checker.ex @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: Apache-2.0 +defmodule TestcontainersElixir.PortChecker do + def wait_for_port(ip, port, timeout \\ 1000) do + wait_for_port(ip, port, timeout, :os.system_time(:millisecond)) + end + + defp wait_for_port(ip, port, timeout, start_time) do + if timeout + start_time < :os.system_time(:millisecond) do + {:error, :timeout} + else + if port_open?(ip, port) do + {:ok, :port_is_open} + else + # Sleep for 500 ms, then retry + :timer.sleep(500) + wait_for_port(ip, port, timeout, start_time) + end + end + end + + defp port_open?(ip, port, timeout \\ 1000) do + IO.inspect("checking port #{port}") + + case :gen_tcp.connect(~c"#{ip}", port, [:binary, active: false], timeout) do + {:ok, socket} -> + :gen_tcp.close(socket) + true + + {:error, _reason} -> + false + end + end +end diff --git a/lib/reaper.ex b/lib/reaper.ex index 2fbd533..7e15dbd 100644 --- a/lib/reaper.ex +++ b/lib/reaper.ex @@ -20,6 +20,7 @@ defmodule TestcontainersElixir.Reaper do @impl true def init(connection) do Process.flag(:trap_exit, true) + with {:ok, _image_create_response} <- Api.Image.image_create(connection, fromImage: @ryuk_image), {:ok, %Model.ContainerCreateResponse{Id: container_id}} <- diff --git a/test/simple_test.exs b/test/simple_test.exs index 28e4348..e70ecb8 100644 --- a/test/simple_test.exs +++ b/test/simple_test.exs @@ -1,12 +1,14 @@ defmodule SimpleTest do use ExUnit.Case import TestcontainersElixir.ExUnit + alias TestcontainersElixir.PortChecker alias TestcontainersElixir.Container test "creates and reaps container" do {:ok, container} = container(image: "nginx:latest", port: 80) port = Container.mapped_port(container, 80) + PortChecker.wait_for_port("localhost", port, 5000) {:ok, 200, _headers, body_ref} = :hackney.get("http://localhost:#{port}") {:ok, body} = :hackney.body(body_ref)