leorodriguesrj
Is it possible to have elixir running with a custom node name during tests?
In Erlang, using “Common Test” framework, each test execution has a unique node name allowing a higher degree of resources isolation. Is it possible to achieve the same in elixir with ExUnit?
Marked As Solved
leorodriguesrj
I came up with this
work in progress!
# Starting "epmd"
epmd_path = System.find_executable("epmd")
port = Port.open({:spawn_executable, epmd_path}, [ ])
os_pid = Keyword.get(Port.info(port), :os_pid)
# Configuring a "shutdown hook" to stop epmd after everything is done.
# (I'm not particularly proud of this implementation, honestly)
System.at_exit(fn _ -> System.shell("kill -TERM #{os_pid}") end)
# This is a bit of an exageration just to compute a name
{:ok, now} = DateTime.now("Etc/UTC")
now = DateTime.to_unix(now)
cipher = '#{now}'
|> Enum.map(fn c -> c + 49 end)
|> List.to_string()
# Turn this node into a distributed node
{:ok, _} = Node.start(String.to_atom("#{cipher}@testhost"))
# Finally, start the tests
ExUnit.start()
For some reason, I was unable to make correct use of Port.close/1, so I used the kill command to stop epmd. Obviously, this implementation will fail in windows.
Also Liked
LostKobrakai
ExUnit tests may run async to each other, so afaik there is no built in support for something like that. Though I guess nothing would prevent you from creating a case template, which restarts distribution each time and assigns a unique name to the node.
LostKobrakai
Or Node.start/3 and Node.stop/0
lud
I didn’t know those even existed ![]()
@leorodriguesrj To start epmd add {_, 0} = System.cmd("epmd", ["-daemon"]) before ExUnit.start() in test_helper.exs
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








