From 96cbbea5eacc14b1adaacb2f24806cc443802494 Mon Sep 17 00:00:00 2001 From: arpunk Date: Sun, 10 Dec 2017 17:26:35 -0500 Subject: [PATCH 1/2] Add multi-node basic example --- test/flock/multi_node_test.exs | 19 +++++++++++++++++++ test/support/my_bird.ex | 11 +++++++++++ test/support/node_manager_test.ex | 31 +++++++++++++++++++++++++++++++ test/test_helper.exs | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/flock/multi_node_test.exs create mode 100644 test/support/node_manager_test.ex 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.ex b/test/support/node_manager_test.ex new file mode 100644 index 0000000..adc483e --- /dev/null +++ b/test/support/node_manager_test.ex @@ -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]) From 03fad81c974f92ed7eefb9d7cad3dcecb6bc3997 Mon Sep 17 00:00:00 2001 From: arpunk Date: Sun, 10 Dec 2017 17:32:11 -0500 Subject: [PATCH 2/2] Interpret on each run --- test/support/{node_manager_test.ex => node_manager_test.exs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/support/{node_manager_test.ex => node_manager_test.exs} (100%) diff --git a/test/support/node_manager_test.ex b/test/support/node_manager_test.exs similarity index 100% rename from test/support/node_manager_test.ex rename to test/support/node_manager_test.exs