Skip to content

Commit

Permalink
Make Horde.Supervisor child_spec overridable
Browse files Browse the repository at this point in the history
Fixes #107
  • Loading branch information
Pedro Tavares committed Jul 9, 2019
1 parent 52b8e7f commit 2c14332
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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
22 changes: 22 additions & 0 deletions test/supervisor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ 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
spec = %{
id: 123,
start:
{TestSupervisor3, :start_link,
[
custom_id: 123,
name: :init_sup_test_3,
strategy: :one_for_one
]},
restart: :transient,
type: :supervisor
}

assert 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

0 comments on commit 2c14332

Please sign in to comment.