Skip to content

Commit 4682511

Browse files
author
José Valim
committed
Make test and test cases exit with reason :shutdown
This guarantees OTP processes are properly shutdown when the test finishes.
1 parent dc403e4 commit 4682511

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ defmodule ExUnit.Callbacks do
2323
Finally, `setup_all` callbacks run in the test case process, while all
2424
`setup` callbacks run in the same process as the test itself. `on_exit`
2525
callbacks always run in a separate process than the test case or the
26-
test itself.
26+
test itself. Since the test process exits with reason `:shutdown`, most
27+
of times `on_exit/1` can be avoided as processes are going to clean
28+
up on their own.
2729
2830
## Context
2931

lib/ex_unit/lib/ex_unit/runner.ex

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ defmodule ExUnit.Runner do
155155
send parent, {self, :case_finished, test_case, failed_tests}
156156
end
157157

158-
receive do: ({^parent, :done} -> :done)
158+
exit(:shutdown)
159159
end)
160160

161161
{test_case, pending} =
@@ -167,9 +167,7 @@ defmodule ExUnit.Runner do
167167
{test_case, []}
168168
end
169169

170-
test_case = exec_on_exit(test_case, case_pid)
171-
send case_pid, {parent, :done}
172-
{test_case, pending}
170+
{exec_on_exit(test_case, case_pid), pending}
173171
end
174172

175173
defp exec_case_setup(%ExUnit.TestCase{name: case_name} = test_case) do
@@ -219,7 +217,7 @@ defmodule ExUnit.Runner do
219217
end)
220218

221219
send parent, {self, :test_finished, %{test | time: us}}
222-
receive do: ({^parent, :done} -> :done)
220+
exit(:shutdown)
223221
end)
224222

225223
test =
@@ -230,9 +228,7 @@ defmodule ExUnit.Runner do
230228
%{test | state: {:failed, {{:EXIT, test_pid}, error, []}}}
231229
end
232230

233-
test = exec_on_exit(test, test_pid)
234-
send test_pid, {parent, :done}
235-
test
231+
exec_on_exit(test, test_pid)
236232
end
237233

238234
defp exec_test_setup(%ExUnit.Test{case: case} = test, context) do

lib/ex_unit/test/ex_unit/callbacks_test.exs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,27 @@ defmodule ExUnit.CallbacksTest do
105105
on_exit fn -> ExUnit.configure(formatters: [ExUnit.CLIFormatter]) end
106106
end
107107

108-
test "kills test process only after on_exit runs" do
108+
test "exits with shutdown reason" do
109109
defmodule OnExitAliveTest do
110110
use ExUnit.Case
111111

112112
setup do
113+
parent = self()
114+
113115
pid = spawn_link fn ->
114116
Process.flag(:trap_exit, true)
115-
receive do: ({:EXIT, _, _} -> :ok)
117+
send parent, :ready
118+
receive do
119+
{:EXIT, ^parent, :shutdown} ->
120+
receive do: ({:on_exit, pid} -> send pid, :done)
121+
end
116122
end
117123

124+
receive do: (:ready -> :ok)
125+
118126
on_exit fn ->
119-
assert Process.alive?(pid)
127+
send pid, {:on_exit, self}
128+
assert_receive :done
120129
IO.puts "on_exit run"
121130
end
122131

0 commit comments

Comments
 (0)