Skip to content

Commit

Permalink
refactor core logic for running testcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 16, 2023
1 parent 8c2bc4d commit 2fd1e2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
35 changes: 14 additions & 21 deletions lib/docker/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,10 @@ defmodule Testcontainers.Docker.Api do
with :ok <- pull_image(create_request."Image", recv_timeout: 60_000),
{:ok, id} <- create_container(create_request, recv_timeout: 2000),
:ok <- start_container(id, recv_timeout: 300_000),
:ok <-
(if on_exit do
with :ok <- on_exit.(:stop_container, fn -> stop_container(id) end) do
reap_container(id)
end
else
:ok
end),
{:ok, container} <- get_container(id),
:ok <-
Enum.reduce(wait_strategies, :ok, fn
wait_strategy, :ok ->
WaitStrategy.wait_until_container_is_ready(
wait_strategy,
container.container_id
)

_, error ->
error
end) do
{:ok, container}
:ok <- if(on_exit, do: on_exit.(fn -> stop_container(id) end), else: :ok),
:ok <- reap_container(id),
:ok <- wait_for_container(id, wait_strategies) do
get_container(id)
end
end

Expand All @@ -47,6 +30,16 @@ defmodule Testcontainers.Docker.Api do
end
end

defp wait_for_container(id, wait_strategies) do
Enum.reduce(wait_strategies, :ok, fn
wait_strategy, :ok ->
WaitStrategy.wait_until_container_is_ready(wait_strategy, id)

_, error ->
error
end)
end

defp pull_image(image, options) do
conn = Connection.get_connection(options)

Expand Down
2 changes: 1 addition & 1 deletion lib/ex_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ defmodule Testcontainers.ExUnit do
It also sets up the ExUnit callback to remove the container after the test finishes.
"""
def run_container(config) do
Docker.Api.run(config, on_exit: &ExUnit.Callbacks.on_exit/2)
Docker.Api.run(config, on_exit: &ExUnit.Callbacks.on_exit/1)
end
end
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:ok, _} = Testcontainers.Reaper.start_link()
Testcontainers.Reaper.start_link()
ExUnit.configure(max_cases: System.schedulers_online() * 4)
ExUnit.start()

Expand Down

0 comments on commit 2fd1e2c

Please sign in to comment.