Skip to content

Commit 0bed0d0

Browse files
author
José Valim
committed
Add DynamicSupervisor.stop and streamline event manager code
1 parent 157913c commit 0bed0d0

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

lib/elixir/lib/dynamic_supervisor.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,22 @@ defmodule DynamicSupervisor do
421421
call(supervisor, :count_children) |> :maps.from_list()
422422
end
423423

424+
@doc """
425+
Synchronously stops the given supervisor with the given `reason`.
426+
427+
It returns `:ok` if the supervisor terminates with the given
428+
reason. If it terminates with another reason, the call exits.
429+
430+
This function keeps OTP semantics regarding error reporting.
431+
If the reason is any other than `:normal`, `:shutdown` or
432+
`{:shutdown, _}`, an error report is logged.
433+
"""
434+
@since "1.7.0"
435+
@spec stop(Supervisor.supervisor(), reason :: term, timeout) :: :ok
436+
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) do
437+
GenServer.stop(supervisor, reason, timeout)
438+
end
439+
424440
@doc """
425441
Receives a set of options that initializes a dynamic supervisor.
426442

lib/elixir/test/elixir/dynamic_supervisor_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ defmodule DynamicSupervisorTest do
9595
# And the initial call
9696
assert {:supervisor, DynamicSupervisorTest.Simple, 1} =
9797
:proc_lib.translate_initial_call(pid)
98+
99+
# And shuts down
100+
assert DynamicSupervisor.stop(__MODULE__) == :ok
98101
end
99102

100103
test "sets initial call to the same as a regular supervisor" do

lib/ex_unit/lib/ex_unit/event_manager.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ defmodule ExUnit.EventManager do
1717
end
1818

1919
def stop({sup, event}) do
20-
for {_, pid, _, _} <- Supervisor.which_children(sup) do
20+
for {_, pid, _, _} <- DynamicSupervisor.which_children(sup) do
2121
GenServer.stop(pid, :normal, @timeout)
2222
end
2323

24-
Supervisor.stop(sup)
24+
DynamicSupervisor.stop(sup)
2525
:gen_event.stop(event)
2626
end
2727

lib/ex_unit/lib/ex_unit/runner.ex

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ defmodule ExUnit.Runner do
66
@rand_algorithm :exs1024
77

88
def run(opts, load_us) do
9-
{opts, config} = configure(opts)
9+
{:ok, manager} = EM.start_link()
10+
{:ok, stats} = EM.add_handler(manager, ExUnit.RunnerStats, opts)
11+
{opts, config} = configure(manager, opts)
1012

1113
:erlang.system_flag(:backtrace_depth, Keyword.fetch!(opts, :stacktrace_depth))
1214

@@ -17,24 +19,20 @@ defmodule ExUnit.Runner do
1719
end)
1820

1921
EM.suite_finished(config.manager, run_us, load_us)
20-
result = ExUnit.RunnerStats.stats(config.stats)
22+
result = ExUnit.RunnerStats.stats(stats)
2123
EM.stop(config.manager)
2224
result
2325
end
2426

25-
defp configure(opts) do
27+
defp configure(manager, opts) do
2628
opts = normalize_opts(opts)
27-
28-
{:ok, manager} = EM.start_link()
29-
{:ok, stats} = EM.add_handler(manager, ExUnit.RunnerStats, opts)
3029
Enum.each(opts[:formatters], &EM.add_handler(manager, &1, opts))
3130

3231
config = %{
3332
capture_log: opts[:capture_log],
3433
exclude: opts[:exclude],
3534
include: opts[:include],
3635
manager: manager,
37-
stats: stats,
3836
max_cases: opts[:max_cases],
3937
seed: opts[:seed],
4038
modules: :async,

lib/ex_unit/lib/ex_unit/runner_stats.ex

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ defmodule ExUnit.RunnerStats do
44
use GenServer
55
alias ExUnit.{Manifest, Test, TestModule}
66

7-
@manifest ".ex_unit_results.elixir"
7+
def stats(pid) do
8+
GenServer.call(pid, :stats, :infinity)
9+
end
810

911
def init(opts) do
10-
{manifest_file, old_manifest} =
11-
case Keyword.fetch(opts, :manifest_path) do
12-
:error ->
13-
{nil, %{}}
14-
15-
{:ok, manifest_path} ->
16-
manifest_file = Path.join(manifest_path, @manifest)
17-
{manifest_file, Manifest.read(manifest_file)}
18-
end
12+
{manifest_file, old_manifest} = read_manifest(opts)
1913

2014
state = %{
2115
total: 0,
@@ -30,8 +24,17 @@ defmodule ExUnit.RunnerStats do
3024
{:ok, state}
3125
end
3226

33-
def stats(pid) do
34-
GenServer.call(pid, :stats, :infinity)
27+
@manifest ".ex_unit_results.elixir"
28+
29+
defp read_manifest(opts) do
30+
case Keyword.fetch(opts, :manifest_path) do
31+
:error ->
32+
{nil, %{}}
33+
34+
{:ok, manifest_path} ->
35+
manifest_file = Path.join(manifest_path, @manifest)
36+
{manifest_file, Manifest.read(manifest_file)}
37+
end
3538
end
3639

3740
def handle_call(:stats, _from, state) do

0 commit comments

Comments
 (0)