3

I have an eunit test that generates a unique node name and starts distribution:

{A,B,C} = now(),
Nodename = list_to_atom(lists:flatten(io_lib:format(
    "test-~b-~b-~b@localhost", [A, B, C]))),
{ok, _} = net_kernel:start([Nodename, shortnames]),

This works fine as long as a distributed Erlang node has been running on the machine at some previous time, and thus epmd is still running, but on the build server I can't assume that's the case.

I solved the problem by adding this to my test:

_ = os:cmd("epmd -daemon"),

but it feels like a hack. Is there a better/nicer way to ensure that epmd is started before running net_kernel:start?

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • How are you running your tests? If you start the erlang process as a node, by supplying `sname` or `name`, the process will implicitly ensure epmd is running for you. – troutwine Aug 19 '13 at 00:12
  • I'm running the tests through `rebar eunit`. – legoscia Aug 19 '13 at 09:35
  • I see. There's always [erl_opts](https://github.com/basho/rebar/blob/master/rebar.config.sample#L24) to add an name to, but that, as well, is kind of crummy. – troutwine Aug 21 '13 at 01:16
  • 1
    ```os:cmd("epmd -daemon")``` will block if the epmd is not runing (tested on windows machine). How to get around this issue? – csyangchen May 06 '15 at 10:29

1 Answers1

5

No, you cannot ensure EPMD is started in a cleaner way.

TL;DR

EPMD is an external program, implemented in C. Whilst net_kernel:start/1 takes care of creating the net_sup supervisor, it does not actually trigger the EPMD daemon, which has to be started explicitely. I had a look at how EPMD is started when the -sname option is specified in the erl command and - surprise, surprise - I discovered that the epmd program is started via a system() C call.

Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112