Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/flock/multi_node_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Flock.MultiNodeTest do
use ExUnit.Case

@tag :distributed
test "multi-node test" do
:ok = NodeManagerTest.create_test_node()

Flock.start({MyBird, [], "uno"})
Flock.start({MyBird, [], "dos"})

:timer.sleep(1_000)

node_a = Flock.call("uno", :whereis)
node_b = Flock.call("dos", :whereis)

assert "flock_b@" <> _hostname = to_string(node_a)
assert "flock_a@" <> _hostname = to_string(node_b)
end
end
11 changes: 11 additions & 0 deletions test/support/my_bird.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule MyBird do
send(test_pid, spawn_message)
{:ok, test_pid}
end

def init(name) do
Process.send_after(self(), :chirp, 10_000)
{:ok, name}
Expand All @@ -21,12 +22,19 @@ defmodule MyBird do
def handle_call(:where_are_you?, _from, s) do
{:reply, node(), s}
end

def handle_call(:whereis, _from, s) do
{:reply, node(), s}
end

def handle_call(:ping, _from, s) do
{:reply, :pong, s}
end

def handle_call({:please_reply, msg}, _from, s) do
{:reply, msg, s}
end

def handle_call(:please_crash, _from, s) do
{:stop, :crash_requested, :crashed, s}
end
Expand All @@ -35,9 +43,11 @@ defmodule MyBird do
send(pid, msg)
{:noreply, s}
end

def handle_cast(:hi, s) do
{:noreply, s}
end

def handle_cast(:byebye, s) do
{:stop, :normal, s}
end
Expand All @@ -52,6 +62,7 @@ defmodule MyBird do
send(test_pid, {:bird_terminated, msg})
:normal
end

def terminate(:normal, _name) do
:normal
end
Expand Down
31 changes: 31 additions & 0 deletions test/support/node_manager_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule NodeManagerTest do

@cmd [
"run",
"--no-halt",
"-e",
"Node.start(:flock_b, :shortnames)"
]

def create_test_node do
_ = Node.stop()

{:ok, _} = Node.start(:flock_a, :shortnames)

{:ok, _task} =
Task.start_link(fn ->
System.cmd("mix", @cmd, env: %{"MIX_ENV" => "test"})
end)

"flock_a@" <> hostname = to_string(node())
other_node = "flock_b@" <> hostname |> String.to_atom()

System.at_exit(fn _exit_code ->
:rpc.call(other_node, :init, :stop, [])
end)

true = Node.connect(:"flock_b@#{hostname}")

:ok
end
end
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ExUnit.start()
ExUnit.start(exclude: [:distributed])