From 9eb1e7dffbb64f9a12970bc4a255992f99d4cfd9 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 23 Mar 2020 13:56:22 -0500 Subject: [PATCH] use struct types throughout --- lib/router/admin.ex | 2 +- lib/router/session.ex | 66 +++++---- mix.exs | 4 +- mix.lock | 4 +- test/support/test_callee.ex | 21 ++- test/wampex_test.exs | 269 ++---------------------------------- 6 files changed, 61 insertions(+), 305 deletions(-) diff --git a/lib/router/admin.ex b/lib/router/admin.ex index 16204d9..4f838d8 100644 --- a/lib/router/admin.ex +++ b/lib/router/admin.ex @@ -40,7 +40,7 @@ defmodule Wampex.Router.Admin do {req_id, %Invocation{ arg_kw: %{"realm" => realm, "authid" => authid, "password" => password}, - options: %{"procedure" => "admin.create_peer"} + details: %{"procedure" => "admin.create_peer"} } = event, {pid, node}}, %{proxy: proxy} = state ) do diff --git a/lib/router/session.ex b/lib/router/session.ex index 4dede66..b9b3e9d 100644 --- a/lib/router/session.ex +++ b/lib/router/session.ex @@ -6,13 +6,17 @@ defmodule Wampex.Router.Session do alias __MODULE__, as: Sess alias StatesLanguage, as: SL - alias Wampex.Roles.{Broker, Caller, Dealer, Peer} - alias Wampex.Roles.Peer.{Abort, Challenge, Error, Welcome} + alias Wampex.Roles.{Broker, Callee, Caller, Dealer, Peer, Publisher, Subscriber} + alias Wampex.Roles.Peer.{Abort, Authenticate, Challenge, Error, Hello, Welcome} alias Wampex.Router alias Broker.{Subscribed, Published, Event, Unsubscribed} + alias Callee.{Register, Unregister, Yield} + alias Caller.Call alias Dealer.{Invocation, Registered, Result, Unregistered} + alias Publisher.Publish alias Router.{Authentication, Realms} alias Realms.Session, as: RealmSession + alias Subscriber.{Subscribe, Unsubscribe} @enforce_keys [:transport, :transport_pid] defstruct [ @@ -54,19 +58,19 @@ defmodule Wampex.Router.Session do proxy: module() | nil, transport_pid: pid() | module() | nil, message: Wampex.message() | nil, - hello: Wampex.hello() | nil, - authenticate: Wampex.authenticate() | nil, - register: Wampex.register() | nil, - unregister: Wampex.unregister() | nil, - call: Wampex.call() | nil, - yield: Wampex.yield() | nil, - subscribe: Wampex.subscribe() | nil, - unsubscribe: Wampex.unsubscribe() | nil, - publish: Wampex.publish() | nil, + hello: Hello.t() | nil, + authenticate: Authenticate.t() | nil, + register: Register.t() | nil, + unregister: Unregister.t() | nil, + call: Call.t() | nil, + yield: Yield.t() | nil, + subscribe: Subscribe.t() | nil, + unsubscribe: Unsubscribe.t() | nil, + publish: Publish.t() | nil, goodbye: binary() | nil, realm: String.t() | nil, name: module() | nil, - challenge: map() | nil, + challenge: Challenge.t() | nil, roles: [module()], registrations: [], subscriptions: [], @@ -168,13 +172,13 @@ defmodule Wampex.Router.Session do id: session_id, transport: tt, transport_pid: t, - hello: {:hello, realm, dets}, + hello: %Hello{realm: realm, options: options}, hello_received: false, authentication: auth } = data } = sl ) do - {actions, challenge} = maybe_challenge(auth, realm, dets, session_id, tt, t) + {actions, challenge} = maybe_challenge(auth, realm, options, session_id, tt, t) {:ok, %SL{ @@ -184,7 +188,7 @@ defmodule Wampex.Router.Session do | challenge: challenge, hello_received: true, realm: realm, - peer_information: dets + peer_information: options } }, [{:next_event, :internal, :transition}] ++ actions} end @@ -203,7 +207,7 @@ defmodule Wampex.Router.Session do challenge: challenge, realm: realm, name: name, - authenticate: {:authenticate, sig, _dets} + authenticate: %Authenticate{signature: sig} } } = sl ) do @@ -248,7 +252,7 @@ defmodule Wampex.Router.Session do transport_pid: t, subscriptions: subs, realm: realm, - subscribe: {:subscribe, ri, opts, topic}, + subscribe: %Subscribe{request_id: ri, options: opts, topic: topic}, db: db } = data } = sl @@ -275,7 +279,7 @@ defmodule Wampex.Router.Session do transport_pid: t, db: db, subscriptions: subs, - unsubscribe: {:unsubscribe, rid, subscription_id} + unsubscribe: %Unsubscribe{request_id: rid, subscription_id: subscription_id} } } = sl ) do @@ -298,7 +302,7 @@ defmodule Wampex.Router.Session do transport: tt, transport_pid: t, db: db, - publish: {:publish, rid, opts, topic, arg_l, arg_kw} + publish: %Publish{request_id: rid, options: opts, topic: topic, arg_list: arg_l, arg_kw: arg_kw} } } = sl ) do @@ -314,7 +318,7 @@ defmodule Wampex.Router.Session do publication_id: pub_id, arg_list: arg_l, arg_kw: arg_kw, - options: opts + details: opts }, pid} ) end) @@ -343,7 +347,7 @@ defmodule Wampex.Router.Session do transport_pid: t, registrations: regs, realm: realm, - unregister: {:unregister, request_id, registration_id} + unregister: %Unregister{request_id: request_id, registration_id: registration_id} } = data } = sl ) do @@ -366,7 +370,7 @@ defmodule Wampex.Router.Session do registrations: regs, db: db, realm: realm, - register: {:register, rid, _opts, procedure} + register: %Register{request_id: rid, procedure: procedure} } = data } = sl ) do @@ -393,7 +397,7 @@ defmodule Wampex.Router.Session do transport_pid: t, realm: realm, request_id: ri, - call: {:call, call_id, dets, proc, al, akw} + call: %Call{request_id: call_id, options: opts, procedure: proc, arg_list: al, arg_kw: akw} } = data } = sl ) do @@ -401,7 +405,7 @@ defmodule Wampex.Router.Session do with {callees, index} <- RealmSession.callees(db, realm, proc), {id, {pid, node}} <- get_live_callee(proxy, callees, index, 3) do req_id = RealmSession.get_request_id(ri) - dets = Map.put(dets, "procedure", proc) + opts = Map.put(opts, "procedure", proc) send( {proxy, node}, @@ -411,7 +415,7 @@ defmodule Wampex.Router.Session do %Invocation{ request_id: req_id, registration_id: id, - options: dets, + details: opts, arg_list: al, arg_kw: akw }, @@ -460,7 +464,13 @@ defmodule Wampex.Router.Session do @handle_yield, _, @yield, - %SL{data: %Sess{proxy: proxy, invocations: inv, yield: {:yield, id, dets, arg_l, arg_kw}}} = data + %SL{ + data: %Sess{ + proxy: proxy, + invocations: inv, + yield: %Yield{request_id: id, options: opts, arg_list: arg_l, arg_kw: arg_kw} + } + } = data ) do {call_id, _, {pid, node}} = Enum.find(inv, fn @@ -470,7 +480,7 @@ defmodule Wampex.Router.Session do send( {proxy, node}, - {%Result{request_id: call_id, arg_list: arg_l, arg_kw: arg_kw, options: dets}, pid} + {%Result{request_id: call_id, arg_list: arg_l, arg_kw: arg_kw, details: opts}, pid} ) inv = @@ -623,7 +633,7 @@ defmodule Wampex.Router.Session do chal = %Challenge{ auth_method: auth.method(), - options: ch + extra: ch } send_to_peer( diff --git a/mix.exs b/mix.exs index b52e3e7..32665c6 100644 --- a/mix.exs +++ b/mix.exs @@ -53,10 +53,10 @@ defmodule Wampex.Router.MixProject do {:plug_cowboy, "~> 2.1"}, {:postgrex, ">= 0.0.0"}, {:states_language, "~> 0.2"}, - {:wampex, git: "https://gitlab.com/entropealabs/wampex.git", tag: "fef406d7efd7d8d5d49e49951da802280a3d43b6"}, + {:wampex, git: "https://gitlab.com/entropealabs/wampex.git", tag: "c2c8ee4fbf0b72dfb5b786b33a47a4698b4c5491"}, {:wampex_client, git: "https://gitlab.com/entropealabs/wampex_client.git", - tag: "5c8b95114bfc11b8c9ce92803f22c088b34a78eb", + tag: "5842014aa550977e859656d193583d022254b1fd", only: [:dev, :test]} ] end diff --git a/mix.lock b/mix.lock index 395ee42..66955ed 100644 --- a/mix.lock +++ b/mix.lock @@ -42,8 +42,8 @@ "states_language": {:hex, :states_language, "0.2.8", "f9dfd3c0bd9a9d7bda25ef315f2d90944cd6b2022a7f3c403deb1d4ec451825e", [:mix], [{:elixpath, "~> 0.1.0", [hex: :elixpath, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:json_xema, "~> 0.4.0", [hex: :json_xema, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xema, "~> 0.11.0", [hex: :xema, repo: "hexpm", optional: false]}], "hexpm", "a5231691e7cb37fe32dc7de54c2dc86d1d60e84c4f0379f3246e55be2a85ec78"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"}, - "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "fef406d7efd7d8d5d49e49951da802280a3d43b6", [tag: "fef406d7efd7d8d5d49e49951da802280a3d43b6"]}, - "wampex_client": {:git, "https://gitlab.com/entropealabs/wampex_client.git", "5c8b95114bfc11b8c9ce92803f22c088b34a78eb", [tag: "5c8b95114bfc11b8c9ce92803f22c088b34a78eb"]}, + "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "c2c8ee4fbf0b72dfb5b786b33a47a4698b4c5491", [tag: "c2c8ee4fbf0b72dfb5b786b33a47a4698b4c5491"]}, + "wampex_client": {:git, "https://gitlab.com/entropealabs/wampex_client.git", "5842014aa550977e859656d193583d022254b1fd", [tag: "5842014aa550977e859656d193583d022254b1fd"]}, "websockex": {:hex, :websockex, "0.4.2", "9a3b7dc25655517ecd3f8ff7109a77fce94956096b942836cdcfbc7c86603ecc", [:mix], [], "hexpm", "803cd76e91544b56f0e655e36790be797fa6436db9224f7c303db9b9df2a3df4"}, "xema": {:hex, :xema, "0.11.0", "7b5118418633cffc27092110d02d4faeea938149dd3f6c64299e41e747067e80", [:mix], [{:conv_case, "~> 0.2.2", [hex: :conv_case, repo: "hexpm", optional: false]}, {:decimal, "~> 1.7", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "51491c9a953d65069d4b30aa2f70bc45ff99fd1bc3345bc72ce4e644d01ea14e"}, } diff --git a/test/support/test_callee.ex b/test/support/test_callee.ex index 71ec326..c0866b9 100644 --- a/test/support/test_callee.ex +++ b/test/support/test_callee.ex @@ -3,8 +3,9 @@ defmodule TestCallee do use GenServer require Logger alias Wampex.Client - alias Wampex.Roles.Callee - alias Callee.{Register, Unregister, Yield} + alias Wampex.Roles.{Callee, Dealer} + alias Callee.{Register, Yield} + alias Dealer.Invocation def start_link(test, name, device) do GenServer.start_link(__MODULE__, {test, name, device}) @@ -16,8 +17,11 @@ defmodule TestCallee do {:ok, {test, name, reg}} end - def handle_info({:invocation, id, _, _, _, arg_kw} = invocation, {test, name, _reg} = state) do - Logger.info("Got invocation #{inspect(invocation)}") + def handle_info( + %Invocation{request_id: id, details: dets, arg_kw: arg_kw} = invocation, + {test, name, _reg} = state + ) do + Logger.info("Got invocation #{inspect(dets)}") send(test, invocation) @@ -32,13 +36,4 @@ defmodule TestCallee do {:noreply, state} end - - def terminate(_reason, {_test, name, reg}) do - Client.send_request( - name, - Callee.unregister(%Unregister{registration_id: reg}) - ) - - :ok - end end diff --git a/test/wampex_test.exs b/test/wampex_test.exs index c7782ab..33a52d0 100644 --- a/test/wampex_test.exs +++ b/test/wampex_test.exs @@ -19,13 +19,13 @@ defmodule WampexTest do @url "ws://localhost:4000/ws" @authid "admin" - @auth_password "test1234" - @realm_uri "org.entropealabs.admin" + @auth_password "bnASR5qF9y8k/sHF6S+NneCOhvVI0zFkvoKQpc2F+hA=" + @realm_uri "admin" @auth %Authentication{authid: @authid, authmethods: ["wampcra"], secret: @auth_password} @realm %Realm{name: @realm_uri, authentication: @auth} @roles [Callee, Caller, Publisher, Subscriber] @device "as987d9a8sd79a87ds" - + @session %Session{url: @url, realm: @realm, roles: @roles} setup_all do topologies = Application.get_env(:wampex_router, :topologies) @@ -44,236 +44,6 @@ defmodule WampexTest do ] end - @session %Session{url: @url, realm: @realm, roles: @roles} - - @tag :client - test "session_name" do - assert Test.Session = Client.session_name(Test) - end - - @tag :client - test "subscriber_registry_name" do - assert Test.SubscriberRegistry = Client.subscriber_registry_name(Test) - end - - @tag :client - test "callee_registry_name" do - assert Test.CalleeRegistry = Client.callee_registry_name(Test) - end - - @tag :client - test "transport_name" do - assert Test.Transport = Client.transport_name(Test) - end - - @tag :client - test "Callee.add" do - assert %{callee: %{}} = Callee.add(%{}) - end - - @tag :client - test "Callee.register" do - assert [64, %{}, "test"] = Callee.register(%Register{procedure: "test"}) - end - - @tag :client - test "Callee.unregister" do - assert [66, 123_456] = Callee.unregister(%Unregister{registration_id: 123_456}) - end - - @tag :client - test "Callee.yield" do - assert [70, 1234, %{}, [], %{}] = Callee.yield(%Yield{request_id: 1234}) - assert [70, 1234, %{}, ["1"], %{}] = Callee.yield(%Yield{request_id: 1234, arg_list: ["1"]}) - - assert [70, 1234, %{test: 1}, [2], %{}] = - Callee.yield(%Yield{ - request_id: 1234, - arg_list: [2], - arg_kw: %{}, - options: %{test: 1} - }) - end - - @tag :client - test "Caller.add" do - assert %{caller: %{}} = Caller.add(%{}) - end - - @tag :client - test "Caller.call" do - assert [48, %{}, "test", [], %{}] = Caller.call(%Call{procedure: "test"}) - assert [48, %{}, "test", [1], %{}] = Caller.call(%Call{procedure: "test", arg_list: [1]}) - - assert [48, %{ok: 1}, "test", [1], %{test: 1}] = - Caller.call(%Call{ - procedure: "test", - arg_list: [1], - arg_kw: %{test: 1}, - options: %{ok: 1} - }) - end - - @tag :client - test "Peer.add" do - assert %{} = Caller.add(%{}) - end - - @tag :client - test "Peer.hello" do - assert [1, "test", %{agent: "WAMPex", roles: %{callee: %{}}}] = Peer.hello(%Hello{realm: "test", roles: [Callee]}) - end - - @tag :client - test "Peer.authenticate" do - assert [5, "a-signa-ture", %{}] == - Peer.authenticate(%Authenticate{signature: "a-signa-ture", extra: %{}}) - end - - @tag :client - test "Peer.gooodbye" do - assert [6, %{}, "test"] = Peer.goodbye(%Goodbye{reason: "test"}) - end - - @tag :client - test "Publisher.add" do - assert %{publisher: %{}} = Publisher.add(%{}) - end - - @tag :client - test "Publisher.publish" do - assert [16, %{}, "test", [], %{}] = Publisher.publish(%Publish{topic: "test"}) - assert [16, %{}, "test", [1], %{}] = Publisher.publish(%Publish{topic: "test", arg_list: [1]}) - - assert [16, %{test: 2}, "test", [1], %{test: 1}] = - Publisher.publish(%Publish{ - topic: "test", - arg_list: [1], - arg_kw: %{test: 1}, - options: %{test: 2} - }) - end - - @tag :client - test "Subscriber.add" do - assert %{subscriber: %{}} = Subscriber.add(%{}) - end - - @tag :client - test "Subscriber.subscribe" do - assert [32, %{test: 1}, "test"] = Subscriber.subscribe(%Subscribe{topic: "test", options: %{test: 1}}) - end - - @tag :client - test "Subscriber.unsubscribe" do - assert [34, 1234] = Subscriber.unsubscribe(%Unsubscribe{subscription_id: 1234}) - end - - @tag :router - test "Dealer.add" do - assert %{dealer: %{}} = Dealer.add(%{}) - end - - @tag :router - test "Dealer.registered" do - assert [65, 123_456, 765_432] = Dealer.registered(%Registered{request_id: 123_456, registration_id: 765_432}) - end - - @tag :router - test "Dealer.unregistered" do - assert [67, 123_456] = Dealer.unregistered(%Unregistered{request_id: 123_456}) - end - - @tag :router - test "Dealer.result" do - assert [50, 1234, %{}, [], %{}] = Dealer.result(%Result{request_id: 1234}) - assert [50, 1234, %{}, ["1"], %{}] = Dealer.result(%Result{request_id: 1234, arg_list: ["1"]}) - - assert [50, 1234, %{test: 1}, [2], %{}] = - Dealer.result(%Result{ - request_id: 1234, - arg_list: [2], - arg_kw: %{}, - options: %{test: 1} - }) - end - - @tag :router - test "Dealer.invocation" do - assert [68, 1234, 1234, %{}, [], %{}] = Dealer.invocation(%Invocation{request_id: 1234, registration_id: 1234}) - - assert [68, 1234, 1234, %{}, ["1"], %{}] = - Dealer.invocation(%Invocation{ - request_id: 1234, - registration_id: 1234, - arg_list: ["1"] - }) - - assert [68, 1234, 1234, %{test: 1}, [2], %{test: 2}] = - Dealer.invocation(%Invocation{ - request_id: 1234, - registration_id: 1234, - arg_list: [2], - arg_kw: %{test: 2}, - options: %{test: 1} - }) - end - - @tag :router - test "Broker.add" do - assert %{broker: %{}} = Broker.add(%{}) - end - - @tag :router - test "Broker.event" do - assert [36, 123_456, 765_432, %{}, [], %{}] = - Broker.event(%Event{subscription_id: 123_456, publication_id: 765_432}) - - assert [36, 123_456, 765_432, %{}, [2], %{}] = - Broker.event(%Event{subscription_id: 123_456, publication_id: 765_432, arg_list: [2]}) - - assert [36, 123_456, 765_432, %{}, [2], %{test: 1}] = - Broker.event(%Event{ - subscription_id: 123_456, - publication_id: 765_432, - arg_list: [2], - arg_kw: %{test: 1} - }) - end - - @tag :router - test "Broker.subscribed" do - assert [33, 123_456, 123_456] = Broker.subscribed(%Subscribed{request_id: 123_456, subscription_id: 123_456}) - end - - @tag :router - test "Broker.unsubscribed" do - assert [35, 123_456] = Broker.unsubscribed(%Unsubscribed{request_id: 123_456}) - end - - @tag :router - test "Broker.published" do - assert [17, 123_456, 654_321] = Broker.published(%Published{request_id: 123_456, publication_id: 654_321}) - end - - @tag :client - 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 - - @tag :client - 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 - @tag :client test "callee registration" do name = TestCalleeRegistration @@ -282,25 +52,6 @@ defmodule WampexTest do assert_receive {:registered, id} end - @tag :router - test "get wildcard key" do - subscriber_name = TestSubscriber - topic = ".test.light..status" - {:ok, _pid} = Client.start_link(name: subscriber_name, session: @session) - - assert {:ok, id} = - Client.send_request( - subscriber_name, - Subscriber.subscribe(%Subscribe{topic: topic, options: %{match: "wildcard"}}) - ) - - assert :ok = - Client.send_request( - subscriber_name, - Subscriber.unsubscribe(%Unsubscribe{subscription_id: id}) - ) - end - @tag :abort test "abort" do Process.flag(:trap_exit, true) @@ -331,7 +82,7 @@ defmodule WampexTest do caller_name = TestAdminCaller Client.start_link(name: caller_name, session: @session) - {:ok, %{}, [id], %{}} = + %Result{arg_list: [id]} = Client.send_request( caller_name, Caller.call(%Call{ @@ -363,11 +114,11 @@ defmodule WampexTest do callee_name = TestCalleeRespond Client.start_link(name: callee_name, session: @session) TestCallee.start_link(self(), callee_name, @device) - assert_receive {:registered, id} + assert_receive {_, _} caller_name = TestCaller Client.start_link(name: caller_name, session: @session) - {:ok, %{}, ["ok"], %{"color" => "#FFFFFF"}} = + %Result{arg_list: ["ok"], arg_kw: %{"color" => "#FFFFFF"}} = Client.send_request( caller_name, Caller.call(%Call{ @@ -377,7 +128,7 @@ defmodule WampexTest do }) ) - assert_receive {:invocation, _, _, _, _, _} + assert_receive %Invocation{} end @tag :client @@ -406,8 +157,8 @@ defmodule WampexTest do }) ) - assert_receive {:event, _, _, _, _, _}, 2000 - assert_receive {:event, _, _, _, _, _}, 2000 - assert_receive {:event, _, _, _, _, _}, 2000 + assert_receive %Event{}, 2000 + assert_receive %Event{}, 2000 + assert_receive %Event{}, 2000 end end -- 2.45.3