-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Config | ||
|
||
if Mix.env() == :test do | ||
config :gnat, Gnat.SupervisorTest.MyApp.Gnat, | ||
connection_settings: [ | ||
%{} | ||
] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule Gnat.Supervisor do | ||
defmacro __using__(opts) do | ||
quote do | ||
@mod __MODULE__ | ||
|
||
use Supervisor | ||
|
||
def start_link(init_arg) do | ||
Supervisor.start_link(__MODULE__, init_arg) | ||
end | ||
|
||
@impl true | ||
def init(_init_arg) do | ||
otp_app = unquote(opts)[:otp_app] || raise ArgumentError, "otp_app option is required" | ||
config = Application.get_env(otp_app, @mod, []) | ||
|
||
children = [ | ||
{Gnat.ConnectionSupervisor, connection_opts(config)}, | ||
consumer_supervisor(config) | ||
] | ||
|> List.flatten() | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
|
||
defp connection_opts(config) do | ||
%{ | ||
name: @mod, | ||
connection_settings: Keyword.get(config, :connection_settings) | ||
} | ||
end | ||
|
||
defp consumer_supervisor(config) do | ||
case Keyword.get(config, :topics) do | ||
nil -> [] | ||
topics when is_list(topics) -> [{Gnat.ConsumerSupervisor, topics}] | ||
_ -> raise ArgumentError, "Invalid :topics option. Expected a list of topics." | ||
end | ||
end | ||
|
||
## Functions that forward to Gnat connection | ||
|
||
def active_subscriptions() do | ||
Gnat.active_subscriptions(@mod) | ||
end | ||
|
||
def pub(topic, message, opts \\ []) do | ||
Gnat.pub(@mod, topic, message, opts) | ||
end | ||
|
||
def request(topic, body, opts \\ []) do | ||
Gnat.request(@mod, topic, body, opts) | ||
end | ||
|
||
def request_multi(topic, body, opts \\ []) do | ||
Gnat.request_multi(@mod, topic, body, opts) | ||
end | ||
|
||
def server_info() do | ||
Gnat.server_info(@mod) | ||
end | ||
|
||
def sub(subscriber, topic, opts \\ []) do | ||
Gnat.sub(@mod, subscriber, topic, opts) | ||
end | ||
|
||
def unsub(sid, opts \\ []) do | ||
Gnat.unsub(@mod, sid, opts) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Gnat.SupervisorTest do | ||
use ExUnit.Case, async: true | ||
|
||
defmodule MyApp.Gnat do | ||
use Gnat.Supervisor, otp_app: :gnat | ||
end | ||
|
||
test "it can be supervised" do | ||
assert MyApp.Gnat.child_spec([]) == %{ | ||
id: MyApp.Gnat, | ||
start: {MyApp.Gnat, :start_link, [[]]}, | ||
type: :supervisor | ||
} | ||
end | ||
|
||
test "when started - it provides an API that does not require a Gnat connection arg" do | ||
{:ok, _pid} = MyApp.Gnat.start_link([]) | ||
:timer.sleep(100) | ||
{:ok, _sub} = MyApp.Gnat.sub(self(), "my_app.topic") | ||
:ok = MyApp.Gnat.pub("my_app.topic", "ohai") | ||
assert_receive {:msg, %{topic: "my_app.topic", body: "ohai"}} | ||
assert Supervisor.stop(MyApp.Gnat) == :ok | ||
end | ||
end |