Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping an Erlang supervisor?

Tags:

erlang

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:

  1. signal a supervisor to stop itself and all of its children?
  2. wait for that to complete?
like image 986
Roger Lipscombe Avatar asked Jan 15 '14 13:01

Roger Lipscombe


People also ask

What is supervisor in Erlang?

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.

What is behavior in Erlang?

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.


1 Answers

  1. 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.

  2. 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.
    
like image 74
legoscia Avatar answered Nov 09 '22 04:11

legoscia