leorodriguesrj

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

leorodriguesrj

I came up with this :point_down:t3: 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

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

LostKobrakai

Or Node.start/3 and Node.stop/0

lud

lud

I didn’t know those even existed :smiley:

@leorodriguesrj To start epmd add {_, 0} = System.cmd("epmd", ["-daemon"]) before ExUnit.start() in test_helper.exs

ityonemo

ityonemo

I wouldn’t use clustering as a strategy for resource segregation. Certainly “actually testing clustering” using test-time clusters (for example using the :slave module) is a thing. But for resource segregation I think “recommended way” to do this is to do it in one node, and take advantage of something like Mox to mock out the contended resource, and in your mock, stub out calls to a process started using ExUnit.start_supervised – this process can be stateful and will automatically have a lifetime that matches the lifetime of your test. Mox is already “a resource that is sharded based on tests” so that could make what you are trying to do WAY easier.

You could also dig deeper and use the Process dictionary, :$caller and :$ancestor keys to manually build resource-shardable subsystems, and make it so that each test has its own checkout of your resource. I built a library around this concept but I don’t currently recommend it.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement