Stopping an Erlang supervisor?
erlang
Solution
Send the `shutdown` signal to make the supervisor kill its children and exit:
exit(Pid, shutdown).
You may have to unlink the process from your test first.
Monitor the process, to wait for it to exit:
Ref = monitor(process, Pid),
receive
{'DOWN', Ref, process, Pid, _Reason} ->
ok
after 1000 ->
error(exit_timeout)
end.
Problem
I'm attempting to test a piece of my application that comprises a supervisor and two (different) workers. I'm using eunit, so in my setup and cleanup, I've got: ``` setup() -> {ok, Pid} = foo_sup:start_link(), Pid. cleanup(Pid) -> exit(Pid, kill). ``` `foo_sup:start_link/0` is defined as: ``` start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). ``` However, when my tests run, I get `{badmatch,{error,{already_started,<0.188.0>}}}`, which implies that my supervisor is still running. How do I: - signal a supervisor to stop itself and all of its children? - wait for that to complete?