From 86b6e7047803c45f0e4fabe35473df335a2b5f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 23 Oct 2015 14:53:40 +0200 Subject: Add Supervisor.stop --- lib/elixir/lib/agent/server.ex | 15 --------------- lib/elixir/lib/supervisor.ex | 16 ++++++++++++++++ lib/elixir/test/elixir/supervisor_test.exs | 8 +++----- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/elixir/lib/agent/server.ex b/lib/elixir/lib/agent/server.ex index 3bef67e4c..c432bfed8 100644 --- a/lib/elixir/lib/agent/server.ex +++ b/lib/elixir/lib/agent/server.ex @@ -23,10 +23,6 @@ defmodule Agent.Server do {:reply, :ok, run(fun, [state])} end - def handle_call(:stop, _from, state) do - {:stop, :normal, :ok, state} - end - def handle_call(msg, from, state) do super(msg, from, state) end @@ -43,17 +39,6 @@ defmodule Agent.Server do {:ok, run(fun, [state])} end - def terminate(_reason, _state) do - # There is a race condition if the agent is - # restarted too fast and it is registered. - try do - self |> :erlang.process_info(:registered_name) |> elem(1) |> Process.unregister - rescue - _ -> :ok - end - :ok - end - defp initial_call(mfa) do _ = Process.put(:"$initial_call", get_initial_call(mfa)) :ok diff --git a/lib/elixir/lib/supervisor.ex b/lib/elixir/lib/supervisor.ex index 9d6156ad5..231a4339e 100644 --- a/lib/elixir/lib/supervisor.ex +++ b/lib/elixir/lib/supervisor.ex @@ -463,6 +463,22 @@ defmodule Supervisor do call(supervisor, :count_children) |> :maps.from_list end + @doc """ + Stops the supervisor with the given `reason`. + + It returns `:ok` if the supervisor terminates with the given + reason, if it terminates with another reason, the call will + exit. + + This function keeps OTP semantics regarding error reporting. + If the reason is any other than `:normal`, `:shutdown` or + `{:shutdown, _}`, an error report will be logged. + """ + @spec stop(supervisor, reason :: term, timeout) :: :ok + def stop(supervisor, reason \\ :normal, timeout \\ 5_000) do + :gen.stop(supervisor, reason, timeout) + end + @compile {:inline, call: 2} defp call(supervisor, req) do diff --git a/lib/elixir/test/elixir/supervisor_test.exs b/lib/elixir/test/elixir/supervisor_test.exs index 2c73a05f8..0bccbc031 100644 --- a/lib/elixir/test/elixir/supervisor_test.exs +++ b/lib/elixir/test/elixir/supervisor_test.exs @@ -51,8 +51,7 @@ defmodule SupervisorTest do wait_until_registered(:dyn_stack) assert GenServer.call(:dyn_stack, :pop) == :hello - - Process.exit(pid, :normal) + Supervisor.stop(pid) end test "start_link/3" do @@ -60,7 +59,7 @@ defmodule SupervisorTest do wait_until_registered(:stack_sup) assert GenServer.call(:stat_stack, :pop) == :hello - Process.exit(pid, :normal) + Supervisor.stop(pid) end test "*_child functions" do @@ -86,8 +85,7 @@ defmodule SupervisorTest do assert Supervisor.terminate_child(pid, Stack) == :ok assert Supervisor.delete_child(pid, Stack) == :ok - - Process.exit(pid, :normal) + Supervisor.stop(pid) end defp wait_until_registered(name) do -- cgit v1.2.1