]> Entropealabs - wampex_client.git/commitdiff
improve test coverage, when session dies take everything out
authorChristopher <chris@entropealabs.com>
Fri, 21 Feb 2020 02:46:51 +0000 (20:46 -0600)
committerChristopher <chris@entropealabs.com>
Fri, 21 Feb 2020 02:46:51 +0000 (20:46 -0600)
coveralls.json
lib/wampex.ex
lib/wampex/authentication.ex
lib/wampex/realm.ex
lib/wampex/session.ex
test/support/test_subscriber.ex
test/wampex_test.exs

index 5cb4f6d7bfb39c2f5f3528a78a22b7d2e2cd8acb..e3bafe7e0cd9fc2bfcd396a6671f052969d7cf9d 100644 (file)
@@ -1,5 +1,8 @@
 {
   "coverage_options": {
-    "minimum_coverage": 76.1
-  }
+    "minimum_coverage": 83.6
+  },
+  "skip_files": [
+    "test/support"
+  ]
 }
index dacb7db194ed51108b0718bbf6f6d74e05ef764e..8f2fcd1b4828b833808da8740cba79fcc39fb5ee 100644 (file)
@@ -54,7 +54,7 @@ defmodule Wampex do
       {Registry, [keys: :unique, name: callee_registry, partitions: System.schedulers_online()]}
     ]
 
-    Supervisor.init(children, strategy: :one_for_all)
+    Supervisor.init(children, strategy: :one_for_all, max_restarts: 0)
   end
 
   @spec session_name(module()) :: module()
index 93070fe08c40014674c58b353561c1589e0ca307..fbe743cc70e2cdaa2f418598ddf2c95f73b8d921 100644 (file)
@@ -3,7 +3,7 @@ defmodule Wampex.Authentication do
   @enforce_keys [:authid, :authmethods, :secret]
   defstruct [:authid, :authmethods, :secret]
 
-  @callback handle(challenge :: binary(), auth :: Authentication.t()) :: {binary(), map()}
+  @callback handle(challenge :: binary(), auth :: __MODULE__.t()) :: {binary(), map()}
 
   @type t :: %__MODULE__{
           authid: binary(),
index 5de5b8bdb5dd480c3d1c0e62c38e030588fa1f4b..d218bbd52ea7441ff0136227c2f379eedf92aa10 100644 (file)
@@ -1,10 +1,12 @@
 defmodule Wampex.Realm do
   @moduledoc "Defines the WAMP Realm"
+  alias Wampex.Authentication
 
+  @enforce_keys [:name]
   defstruct [:name, :authentication]
 
   @type t :: %__MODULE__{
           name: binary(),
-          authentication: nil
+          authentication: Authentication.t() | nil
         }
 end
index 86aedc282fa3eba2971d6a4d4908c84735a28cc1..8964da59b8d58ae3e934474eb7ab9b743a8643b7 100644 (file)
@@ -8,7 +8,6 @@ defmodule Wampex.Session do
 
   alias StatesLanguage, as: SL
   alias Wampex.Realm
-  alias Wampex.Authentication
   alias Wampex.Role.Peer
   alias Wampex.Role.Peer.{Authenticate, Hello}
   alias Wampex.Serializer.MessagePack
@@ -31,7 +30,6 @@ defmodule Wampex.Session do
     :realm,
     :name,
     :roles,
-    :authentication,
     :challenge,
     message_queue: [],
     request_id: 0,
@@ -52,7 +50,6 @@ defmodule Wampex.Session do
           realm: Realm.t(),
           name: module() | nil,
           roles: [module()],
-          authentication: Authentication.t() | nil,
           challenge: {binary(), binary()} | nil,
           message_queue: [],
           request_id: integer(),
@@ -100,7 +97,7 @@ defmodule Wampex.Session do
         _,
         %SL{data: %Sess{message_queue: mq} = sess} = data
       ) do
-    Logger.info("Queueing request: #{inspect(request)}")
+    Logger.debug("Queueing request: #{inspect(request)}")
     {:ok, %SL{data | data: %Sess{sess | message_queue: [{request, from} | mq]}}, []}
   end
 
@@ -125,7 +122,7 @@ defmodule Wampex.Session do
         _,
         %SL{data: %Sess{message_queue: mq} = sess} = data
       ) do
-    Logger.info("Queueing request: #{inspect(request)}")
+    Logger.debug("Queueing request: #{inspect(request)}")
     {:ok, %SL{data | data: %Sess{sess | message_queue: [{request, nil} | mq]}}, []}
   end
 
@@ -139,8 +136,7 @@ defmodule Wampex.Session do
             transport: tt,
             transport_pid: t,
             roles: roles,
-            realm: %Realm{name: realm},
-            authentication: auth
+            realm: %Realm{name: realm, authentication: auth}
           }
         } = data
       ) do
@@ -155,7 +151,12 @@ defmodule Wampex.Session do
         _,
         "Challenge",
         %SL{
-          data: %Sess{transport: tt, transport_pid: t, challenge: challenge, authentication: auth}
+          data: %Sess{
+            transport: tt,
+            transport_pid: t,
+            challenge: challenge,
+            realm: %Realm{authentication: auth}
+          }
         } = sl
       ) do
     {signature, extra} = auth.__struct__.handle(challenge, auth)
@@ -215,19 +216,8 @@ defmodule Wampex.Session do
           data: %Sess{request_id: r_id, transport: tt, transport_pid: t, message_queue: mq} = sess
         } = data
       ) do
-    {request_id, requests} =
-      mq
-      |> Enum.reverse()
-      |> Enum.reduce({r_id, []}, fn {request, from}, {id, requests} ->
-        r = do_send(id, tt, t, request)
-        {r, [{r, from} | requests]}
-      end)
-
-    requests =
-      Enum.filter(requests, fn
-        {_, nil} -> false
-        {_, _} -> true
-      end)
+    {request_id, requests} = send_message_queue(r_id, mq, tt, t)
+    requests = remove_cast_requests(requests)
 
     {:ok,
      %SL{
@@ -317,7 +307,7 @@ defmodule Wampex.Session do
 
   defp maybe_authentication(message, nil), do: message
 
-  defp maybe_authentication(%Hello{} = message, %Authentication{} = auth) do
+  defp maybe_authentication(%Hello{} = message, auth) do
     %Hello{message | options: Map.from_struct(auth)}
   end
 
@@ -345,6 +335,26 @@ defmodule Wampex.Session do
     end)
   end
 
+  defp send_message_queue(r_id, [], _, _), do: {r_id, []}
+
+  defp send_message_queue(r_id, mq, tt, t) do
+    mq
+    |> Enum.reverse()
+    |> Enum.reduce({r_id, []}, fn {request, from}, {id, requests} ->
+      r = do_send(id, tt, t, request)
+      {r, [{r, from} | requests]}
+    end)
+  end
+
+  def remove_cast_requests([]), do: []
+
+  def remove_cast_requests(requests) do
+    Enum.filter(requests, fn
+      {_, nil} -> false
+      {_, _} -> true
+    end)
+  end
+
   defp handle_message(msg, data, roles) do
     Enum.reduce_while(roles, nil, fn r, _ ->
       try do
index 2e061a6a8cf4f9cf596dd1998a7a9c91dfc44e2c..a96e4e6b45b0fa244db218821cd777b98f1e6335 100644 (file)
@@ -16,7 +16,6 @@ defmodule TestSubscriber do
   end
 
   def handle_info(event, {test, _name, _sub} = state) do
-    Logger.info("Got event #{inspect(event)}")
     send(test, event)
     {:noreply, state}
   end
index 6b8ae23922abbb771f875bb686c254df6d45c68f..7c15bc50d1d6a894e77a2c9c70560f992e55a5ca 100644 (file)
@@ -4,20 +4,21 @@ defmodule WampexTest do
 
   alias Wampex.{Authentication, Realm, Session}
   alias Wampex.Role.{Callee, Caller, Peer, Publisher, Subscriber}
+  alias Wampex.Serializer.{JSON, MessagePack}
   alias Callee.{Register, Unregister, Yield}
   alias Caller.Call
-  alias Peer.{Goodbye, Hello}
+  alias Peer.{Authenticate, Goodbye, Hello}
   alias Publisher.Publish
   alias Subscriber.{Subscribe, Unsubscribe}
   require Logger
 
   @url "ws://localhost:18080/ws"
+  @auth %Authentication{authid: "entone", authmethods: ["wampcra"], secret: "test1234"}
   @realm %Realm{name: "com.myrealm"}
   @roles [Callee, Caller, Publisher, Subscriber]
   @device "as987d9a8sd79a87ds"
-  @auth %Authentication{authid: "entone", authmethods: ["wampcra"], secret: "test1234"}
 
-  @session %Session{url: @url, realm: @realm, roles: @roles, authentication: @auth}
+  @session %Session{url: @url, realm: @realm, roles: @roles}
 
   test "session_name" do
     assert Test.Session = Wampex.session_name(Test)
@@ -86,6 +87,11 @@ defmodule WampexTest do
              Peer.hello(%Hello{realm: "test", roles: [Callee]})
   end
 
+  test "Peer.authenticate" do
+    assert [5, "a-signa-ture", %{}] ==
+             Peer.authenticate(%Authenticate{signature: "a-signa-ture", extra: %{}})
+  end
+
   test "Peer.gooodbye" do
     assert [6, %{}, "test"] = Peer.goodbye(%Goodbye{reason: "test"})
   end
@@ -120,6 +126,22 @@ defmodule WampexTest do
     assert [34, 1234] = Subscriber.unsubscribe(%Unsubscribe{subscription_id: 1234})
   end
 
+  test "MessagePack Serializer" do
+    assert :binary = MessagePack.data_type()
+    assert "\x93\x05\xC4\t123456789\x80" = MessagePack.serialize!([5, "123456789", %{}])
+    assert {:ok, "\x93\x05\xC4\t123456789\x80"} = MessagePack.serialize([5, "123456789", %{}])
+    assert [5, "123456789", %{}] = MessagePack.deserialize!("\x93\x05\xC4\t123456789\x80")
+    assert {:ok, [5, "123456789", %{}]} = MessagePack.deserialize("\x93\x05\xC4\t123456789\x80")
+  end
+
+  test "JSON Serializer" do
+    assert :text = JSON.data_type()
+    assert "[5,\"123456789\",{}]" = JSON.serialize!([5, "123456789", %{}])
+    assert {:ok, "[5,\"123456789\",{}]"} = JSON.serialize([5, "123456789", %{}])
+    assert [5, "123456789", %{}] = JSON.deserialize!("[5,\"123456789\",{}]")
+    assert {:ok, [5, "123456789", %{}]} = JSON.deserialize("[5,\"123456789\",{}]")
+  end
+
   test "callee registration" do
     name = TestCalleeRegistration
     Wampex.start_link(name: name, session: @session)
@@ -127,6 +149,22 @@ defmodule WampexTest do
     assert_receive {:registered, id}
   end
 
+  test "authentication" do
+    callee_name = TestCalleeRespondAuthenticated
+    session = %Session{@session | realm: %Realm{@session.realm | authentication: @auth}}
+    Wampex.start_link(name: callee_name, session: session)
+    TestCallee.start_link(self(), callee_name, @device)
+    assert_receive {:registered, id}
+  end
+
+  test "abort" do
+    Process.flag(:trap_exit, true)
+    callee_name = TestAbort
+    {:ok, pid} = Wampex.start_link(name: callee_name, session: @session)
+    Wampex.cast_send_request(callee_name, Peer.hello(%Hello{realm: "test", roles: [Callee]}))
+    assert_receive {:EXIT, ^pid, :shutdown}
+  end
+
   test "callee is invoked and responds and caller gets result" do
     callee_name = TestCalleeRespond
     Wampex.start_link(name: callee_name, session: @session)