Skip to content

Commit c4468a9

Browse files
author
José Valim
committed
Rewrite start_supervised already started into duplicate_child_name
Closes #9089.
1 parent 5aa31f4 commit c4468a9

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,15 @@ defmodule ExUnit.Callbacks do
344344
raise ArgumentError, "start_supervised/2 can only be invoked from the test process"
345345
end
346346

347-
Supervisor.start_child(sup, Supervisor.child_spec(child_spec_or_module, opts))
347+
child_spec = Supervisor.child_spec(child_spec_or_module, opts)
348+
349+
case Supervisor.start_child(sup, child_spec) do
350+
{:error, {:already_started, _pid}} ->
351+
{:error, {:duplicate_child_name, child_spec.id}}
352+
353+
other ->
354+
other
355+
end
348356
end
349357

350358
@doc """
@@ -367,9 +375,13 @@ defmodule ExUnit.Callbacks do
367375
end
368376
end
369377

370-
defp start_supervised_error({{:EXIT, reason}, _info}), do: Exception.format_exit(reason)
371-
defp start_supervised_error({reason, _info}), do: Exception.format_exit(reason)
372-
defp start_supervised_error(reason), do: Exception.format_exit(reason)
378+
defp start_supervised_error({{:EXIT, reason}, info}) when is_tuple(info),
379+
do: Exception.format_exit(reason)
380+
381+
defp start_supervised_error({reason, info}) when is_tuple(info),
382+
do: Exception.format_exit(reason)
383+
384+
defp start_supervised_error(reason), do: Exception.format_exit({:start_spec, reason})
373385

374386
@doc """
375387
Stops a child process started via `start_supervised/2`.

lib/ex_unit/test/ex_unit/supervised_test.exs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,19 @@ defmodule ExUnit.SupervisedTest do
7373

7474
test "starts a supervised process with ID checks" do
7575
{:ok, pid} = start_supervised({MyAgent, 0})
76-
assert {:error, {:already_started, ^pid}} = start_supervised({MyAgent, 0})
76+
77+
assert {:error, {:duplicate_child_name, ExUnit.SupervisedTest.MyAgent}} =
78+
start_supervised({MyAgent, 0})
79+
7780
assert {:error, {{:already_started, ^pid}, _}} = start_supervised({MyAgent, 0}, id: :another)
81+
82+
assert_raise RuntimeError, ~r"Reason: bad child specification", fn ->
83+
start_supervised!(%{id: 1, start: :oops})
84+
end
85+
86+
assert_raise RuntimeError, ~r"Reason: already started", fn ->
87+
start_supervised!({MyAgent, 0}, id: :another)
88+
end
7889
end
7990

8091
test "stops a supervised process" do

0 commit comments

Comments
 (0)