Don Branson
Don Branson

Reputation: 13709

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?

Upvotes: 2

Views: 523

Answers (2)

legoscia
legoscia

Reputation: 41528

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.

Upvotes: 4

Cody Poll
Cody Poll

Reputation: 8270

From the meck documentation

Meck will have trouble mocking certain modules since Meck works by recompiling and reloading modules. Since Erlang have a flat module namespace, replacing a module has to be done globally in the Erlang VM. This means certain modules cannot be mocked. The following is a non-exhaustive list of modules that can either be problematic to mock or not possible at all:

  • erlang
  • os
  • crypto
  • compile
  • global

So no, you can't mock exit. You can, however, wrap the exit call in another function and meck that function.

Upvotes: 7

Related Questions