enewbury

enewbury

Notify origin of messages on :peer nodes in distributed tests

It seems the latest and greatest way to write distributed tests in elixir and erlang is to use the :peer module with the connection: :standard_io since this allows you to spin up peers and communicate with them via standard_io rather than making your test node part of a distributed cluster. This allows you to test things like network partitions, and keep things async since each test can have its own protected cluster.

That being said, the api to communicate with those peers seems fairly limited to send, call and cast. In my tests, I need to notify the test process when messages are received on one of the peers. For instance with a normal distributed cluster you could simply send a message back to your test process like

    # Note: untested pseudocode as an example
    parent = self()

    Node.spawn_link(node, fn ->
      Worker.do_stuff()

      next_message = fn ->
        receive do
          message ->
            send(parent, message)
            next_message.()
        end
      end

      next_message.()
    end)

    assert_receive :initialized

    :rpc.call(node, Worker, :do_other_stuff, [])

   assert_receive :new_message

With the peer connection, you can’t send messages directly to the host/origin node since it’s not part of the erlang cluster.

When you’re just waiting for a single message, in the past I’ve used :peer.call with a quoted block of code to execute like this…

      code = 
        quote do
          Worker.do_stuff()

          receive do
            :initialized -> :initialized
          end
        end
      
      :initialized = :peer.call(node_pid, Code, :eval_quoted, [code]

However, this only works if you’re waiting on one message and then return it. In my situation I want to continuously monitor for incoming messages and forward them to the test process so that I can make assertions about messages that come in. Any ideas on how to make that happen with :peer on standard_io connection?

Where Next?

Popular in Questions Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement