Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Horde.Supervisor child_spec overridable #135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/horde/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@ defmodule Horde.Supervisor do
Then you can use `MySupervisor.child_spec/1` and `MySupervisor.start_link/1` in the same way as you'd use `Horde.Supervisor.child_spec/1` and `Horde.Supervisor.start_link/1`.
"""

defmacro __using__(_opts) do
defmacro __using__(opts) do
quote do
@behaviour Horde.Supervisor

def child_spec(options) do
options = Keyword.put_new(options, :id, __MODULE__)

%{
default = %{
id: Keyword.get(options, :id, __MODULE__),
start: {__MODULE__, :start_link, [options]},
type: :supervisor
}

Supervisor.child_spec(default, unquote(Macro.escape(opts)))
end

def start_link(options) do
Horde.Supervisor.start_link(Keyword.put(options, :init_module, __MODULE__))
end

defoverridable child_spec: 1
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/supervisor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ defmodule SupervisorTest do
{:ok, members} = Horde.Cluster.members(:init_sup_test_1)
assert 2 = Enum.count(members)
end

test "can use `child_spec` function to override defaults from Horde.Supervisor" do
child_spec = %{
id: 123,
start:
{TestSupervisor3, :start_link,
[
[
custom_id: 123,
name: :init_sup_test_3,
strategy: :one_for_one
]
]},
restart: :transient,
type: :supervisor
}

assert ^child_spec =
TestSupervisor3.child_spec(
custom_id: 123,
name: :init_sup_test_3,
strategy: :one_for_one
)
end
end

describe ".start_child/2" do
Expand Down
17 changes: 17 additions & 0 deletions test/support/module_based_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ defmodule TestSupervisor2 do
{:ok, Keyword.put(options, :members, [:init_sup_test_1, :init_sup_test_2])}
end
end

defmodule TestSupervisor3 do
use Horde.Supervisor

def init(options) do
{:ok, Keyword.put(options, :members, [:init_sup_test_3, :init_sup_test_3])}
end

def child_spec(args) do
%{
id: args[:custom_id],
start: {__MODULE__, :start_link, [args]},
restart: :transient,
type: :supervisor
}
end
end