erlangmeck

Can meck mock erlang:exit?


I want this in a supervisor module:

stop() ->
  exit(whereis(mousetrap_sup), kill).

So a naïve test might do this:

stop_invokes_exit_test() ->
  meck:new(erlang, [unstick, passthrough]),
  meck:expect(erlang, whereis, 1, a_pid),
  meck:expect(erlang, exit, 2, true),
  mousetrap_sup:stop(),
  ?assert(meck:called(erlang, exit, [a_pid, kill])).

Not surprisingly, it hangs.

I can see where it might not be possible to exercise this code with a test, but is there a way?


Solution

  • You could spawn a process using that name, and check the exit reason:

    {Pid, Ref} = spawn_monitor(timer, sleep, [infinity]),
    register(my_sup, Pid),
    mousetrap_sup:stop(),
    receive
        {'DOWN', Ref, process, Pid, Reason} ->
            ?assertEqual(killed, Reason)
    after 1000 ->
        error(not_killed)
    end.