diff --git a/test/flock/multi_node_test.exs b/test/flock/multi_node_test.exs new file mode 100644 index 0000000..3971737 --- /dev/null +++ b/test/flock/multi_node_test.exs @@ -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 diff --git a/test/support/my_bird.ex b/test/support/my_bird.ex index 84544cf..6902825 100644 --- a/test/support/my_bird.ex +++ b/test/support/my_bird.ex @@ -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} @@ -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 @@ -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 @@ -52,6 +62,7 @@ defmodule MyBird do send(test_pid, {:bird_terminated, msg}) :normal end + def terminate(:normal, _name) do :normal end diff --git a/test/support/node_manager_test.exs b/test/support/node_manager_test.exs new file mode 100644 index 0000000..adc483e --- /dev/null +++ b/test/support/node_manager_test.exs @@ -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 diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..e45f5ac 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1 @@ -ExUnit.start() +ExUnit.start(exclude: [:distributed])