]> Entropealabs - wampex_router.git/commitdiff
some integration tests
authorChristopher <chris@entropealabs.com>
Tue, 18 Feb 2020 00:47:51 +0000 (18:47 -0600)
committerChristopher <chris@entropealabs.com>
Tue, 18 Feb 2020 00:47:51 +0000 (18:47 -0600)
lib/test.ex [deleted file]
mix.exs
test/support/test_callee.ex [new file with mode: 0644]
test/support/test_subscriber.ex [new file with mode: 0644]
test/wampex_test.exs

diff --git a/lib/test.ex b/lib/test.ex
deleted file mode 100644 (file)
index 46e9a03..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-defmodule Test do
-  alias Wampex.{Realm, Session}
-  alias Wampex.Role.{Callee, Caller, Publisher, Subscriber}
-  require Logger
-
-  @url "ws://localhost:18080/ws"
-  @realm %Realm{name: "com.myrealm"}
-  @roles [Callee, Caller, Publisher, Subscriber]
-  @device "s87d6f8s7df6"
-
-  @session Session.new(@url, @realm, @roles)
-
-  defmodule TestCallee do
-    use GenServer
-    require Logger
-    alias Wampex.Role.Callee
-
-    @device "s87d6f8s7df6"
-
-    def start_link(_) do
-      GenServer.start_link(__MODULE__, :ok)
-    end
-
-    def init(:ok) do
-      Wampex.register(TestCalleeSession, "com.actuator.#{@device}.light")
-    end
-
-    def handle_info({:invocation, id, _, _, _, arg_kw} = invocation, state) do
-      Logger.info("Got invocation #{inspect(invocation)}")
-
-      Wampex.cast_send_request(
-        TestCalleeSession,
-        Callee.yield(id, [:ok], %{color: Map.get(arg_kw, "color")})
-      )
-
-      {:noreply, state}
-    end
-  end
-
-  defmodule TestSubscriber do
-    use GenServer
-    require Logger
-
-    def start_link(_) do
-      GenServer.start_link(__MODULE__, :ok)
-    end
-
-    def init(:ok) do
-      Wampex.subscribe(TestSubscriberSession, "com.data.temp")
-    end
-
-    def handle_info(event, state) do
-      Logger.info("Got event #{inspect(event)}")
-      {:noreply, state}
-    end
-  end
-
-  def register do
-    Wampex.start_link(TestCalleeSession, @session)
-    :timer.sleep(500)
-    TestCallee.start_link([])
-  end
-
-  def call do
-    Wampex.start_link(TestCaller, @session)
-    :timer.sleep(500)
-
-    resp =
-      Wampex.send_request(
-        TestCaller,
-        Caller.call(
-          "com.actuator.#{@device}.light",
-          [1],
-          %{
-            color: "#FFFFFF"
-          },
-          %{}
-        )
-      )
-
-    Logger.info("Got Response: #{inspect(resp)}")
-  end
-
-  def subscribe do
-    Wampex.start_link(TestSubscriberSession, @session)
-    :timer.sleep(500)
-    Enum.each(1..5, fn _ -> TestSubscriber.start_link([]) end)
-  end
-
-  def publish do
-    Wampex.start_link(TestPublisher, @session)
-    :timer.sleep(500)
-
-    Enum.each(1..100, fn _ ->
-      Wampex.cast_send_request(
-        TestPublisher,
-        Publisher.publish("com.data.temp", [12.5, 45.6, 87.5], %{loc: "60645"}, %{})
-      )
-
-      :timer.sleep(500)
-    end)
-  end
-end
diff --git a/mix.exs b/mix.exs
index 659f1181c6ac1548e92b33560da8e2912380172f..6883db7b32ac693ac8205651ae4ada439c1d79f1 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -7,10 +7,14 @@ defmodule Wampex.MixProject do
       version: "0.1.0",
       elixir: "~> 1.9",
       start_permanent: Mix.env() == :prod,
+      elixirc_paths: elixirc_paths(Mix.env()),
       deps: deps()
     ]
   end
 
+  defp elixirc_paths(:test), do: ["lib", "test/support"]
+  defp elixirc_paths(_), do: ["lib"]
+
   def application do
     [
       extra_applications: [:logger]
diff --git a/test/support/test_callee.ex b/test/support/test_callee.ex
new file mode 100644 (file)
index 0000000..9b4ea4c
--- /dev/null
@@ -0,0 +1,37 @@
+defmodule TestCallee do
+  use GenServer
+  require Logger
+  alias Wampex.Role.Callee
+
+  def start_link(test, name, device) do
+    GenServer.start_link(__MODULE__, {test, name, device})
+  end
+
+  def init({test, name, device}) do
+    {:ok, reg} = Wampex.register(name, "com.actuator.#{device}.light")
+    send(test, {:registered, reg})
+    {:ok, {test, name, reg}}
+  end
+
+  def handle_info({:invocation, id, _, _, _, arg_kw} = invocation, {test, name, _reg} = state) do
+    Logger.info("Got invocation #{inspect(invocation)}")
+
+    send(test, invocation)
+
+    Wampex.cast_send_request(
+      name,
+      Callee.yield(id, [:ok], %{color: Map.get(arg_kw, "color")})
+    )
+
+    {:noreply, state}
+  end
+
+  def terminate(_reason, {_test, name, reg}) do
+    Wampex.send_request(
+      name,
+      Callee.unregister(reg)
+    )
+
+    :ok
+  end
+end
diff --git a/test/support/test_subscriber.ex b/test/support/test_subscriber.ex
new file mode 100644 (file)
index 0000000..d371a77
--- /dev/null
@@ -0,0 +1,30 @@
+defmodule TestSubscriber do
+  use GenServer
+  require Logger
+  alias Wampex.Role.Subscriber
+
+  def start_link(test, name, topic) do
+    GenServer.start_link(__MODULE__, {test, name, topic})
+  end
+
+  def init({test, name, topic}) do
+    {:ok, sub} = Wampex.subscribe(name, topic)
+    send(test, {:subscribed, sub})
+    {:ok, {test, name, sub}}
+  end
+
+  def handle_info(event, {test, _name, _sub} = state) do
+    Logger.info("Got event #{inspect(event)}")
+    send(test, event)
+    {:noreply, state}
+  end
+
+  def terminate(_r, {_test, name, sub}) do
+    Wampex.send_request(
+      name,
+      Subscriber.unsubscribe(sub)
+    )
+
+    :ok
+  end
+end
index f6cf6cfec286bf696ef473539834b659d8decd3c..995024a28b4bce0724d2f42851cbdd9feb8099ff 100644 (file)
@@ -1,8 +1,62 @@
 defmodule WampexTest do
-  use ExUnit.Case
+  use ExUnit.Case, async: true
   doctest Wampex
 
-  test "greets the world" do
-    assert Wampex.hello() == :world
+  alias Wampex.{Realm, Session}
+  alias Wampex.Role.{Callee, Caller, Publisher, Subscriber}
+  require Logger
+
+  @url "ws://localhost:18080/ws"
+  @realm %Realm{name: "com.myrealm"}
+  @roles [Callee, Caller, Publisher, Subscriber]
+  @device "as987d9a8sd79a87ds"
+
+  @session Session.new(@url, @realm, @roles)
+
+  test "callee registration" do
+    name = TestCalleeRegistration
+    Wampex.start_link(name, @session)
+    TestCallee.start_link(self(), name, @device)
+    assert_receive {:registered, id}
+  end
+
+  test "callee is invoked and responds and caller gets result" do
+    callee_name = TestCalleeRespond
+    Wampex.start_link(callee_name, @session)
+    TestCallee.start_link(self(), callee_name, @device)
+    assert_receive {:registered, id}
+    caller_name = TestCaller
+    Wampex.start_link(caller_name, @session)
+
+    {:ok, ["ok"], %{"color" => "#FFFFFF"}} =
+      Wampex.send_request(
+        caller_name,
+        Caller.call("com.actuator.#{@device}.light", [1], %{color: "#FFFFFF"}, %{})
+      )
+
+    assert_receive {:invocation, _, _, _, _, _}
+  end
+
+  test "subscriber registration" do
+    name = TestSubscriberRegister
+    Wampex.start_link(name, @session)
+    TestSubscriber.start_link(self(), name, "com.data.temp")
+    assert_receive {:subscribed, id}
+  end
+
+  test "subscriber receives events from publisher" do
+    name = TestSubscriberEvents
+    Wampex.start_link(name, @session)
+    TestSubscriber.start_link(self(), name, "com.data.temp")
+    assert_receive {:subscribed, id}
+
+    Wampex.start_link(TestPublisher, @session)
+
+    Wampex.cast_send_request(
+      TestPublisher,
+      Publisher.publish("com.data.temp", [12.5, 45.6, 87.5], %{loc: "60645"}, %{})
+    )
+
+    assert_receive {:event, _, _, _, _, _}
   end
 end