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:
Supervisors are one of the core things that make Erlang/OTP what it is. An active OTP application consists of one or more processes that do the work. Those processes are started indirectly by supervisors, which are responsible for supervising them and restarting them if necessary.
Behaviours are formalizations of these common patterns. The idea is to divide the code for a process in a generic part (a behaviour module) and a specific part (a callback module). The behaviour module is part of Erlang/OTP.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With