Skip to content

Commit

Permalink
Merge pull request #44 from DohanKim/bug/supervisor_registry
Browse files Browse the repository at this point in the history
Supervisor process foundable with :via option
  • Loading branch information
cpursley authored Jan 26, 2024
2 parents 2ae0086 + fa057d6 commit d378887
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/walex/config/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule WalEx.Config.Registry do

def set_name(:set_agent, module, app_name), do: set_name(module, app_name)
def set_name(:set_gen_server, module, app_name), do: set_name(module, app_name)
def set_name(:set_supervisor, module, app_name), do: {:via, module, app_name}
def set_name(:set_supervisor, module, app_name), do: set_name(module, app_name)

defp set_name(module, app_name), do: {:via, Registry, {@walex_registry, {module, app_name}}}

Expand Down
5 changes: 2 additions & 3 deletions lib/walex/destinations/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ defmodule WalEx.Destinations.Supervisor do
alias Destinations.{EventModules, EventRelay, Webhooks}

def start_link(opts) do
app_name = Keyword.get(opts, :app_name)
app_name = Keyword.get(opts, :name)
name = Config.Registry.set_name(:set_supervisor, __MODULE__, app_name)

Supervisor.start_link(__MODULE__, configs: opts, name: name)
Supervisor.start_link(__MODULE__, opts, name: name)
end

@impl true
def init(opts) do
app_name =
opts
|> Keyword.get(:configs)
|> Keyword.get(:name)

children =
Expand Down
3 changes: 1 addition & 2 deletions lib/walex/replication/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ defmodule WalEx.Replication.Supervisor do
app_name = Keyword.get(opts, :app_name)
name = WalEx.Config.Registry.set_name(:set_supervisor, __MODULE__, app_name)

Supervisor.start_link(__MODULE__, configs: opts, name: name)
Supervisor.start_link(__MODULE__, opts, name: name)
end

@impl true
def init(opts) do
app_name =
opts
|> Keyword.get(:configs)
|> Keyword.get(:app_name)

children = [
Expand Down
5 changes: 2 additions & 3 deletions lib/walex/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule WalEx.Supervisor do

name = WalExRegistry.set_name(:set_supervisor, __MODULE__, app_name)

Supervisor.start_link(__MODULE__, configs: supervisor_opts, name: name)
Supervisor.start_link(__MODULE__, supervisor_opts, name: name)
end

@impl true
Expand Down Expand Up @@ -63,8 +63,7 @@ defmodule WalEx.Supervisor do
Enum.filter(other_configs, &(not Keyword.has_key?(opts, &1)))
end

defp set_children(opts) do
configs = Keyword.get(opts, :configs)
defp set_children(configs) do
app_name = Keyword.get(configs, :name)

[
Expand Down
69 changes: 68 additions & 1 deletion test/walex/config/registry_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
defmodule WalEx.Config.RegistryTest do
use ExUnit.Case, async: false

require Logger

alias WalEx.Supervisor, as: WalExSupervisor
alias WalEx.Config.Registry, as: WalExRegistry
alias WalEx.Config.RegistryTest, as: WalExRegistryTest

@base_configs [
name: :test_name,
hostname: "hostname",
username: "username",
password: "password",
database: "todos_test",
port: 5432,
subscriptions: ["subscriptions"],
publication: "publication"
]

describe "start_registry/0" do
test "should start a process" do
assert {:ok, pid} = WalExRegistry.start_registry()
Expand All @@ -28,9 +42,51 @@ defmodule WalEx.Config.RegistryTest do
end

test "should set supervisor name" do
assert {:via, WalExRegistryTest, :app_name_test} ==
assert {:via, Registry, {:walex_registry, {WalExRegistryTest, :app_name_test}}} ==
WalExRegistry.set_name(:set_supervisor, __MODULE__, :app_name_test)
end

test "should be able to find processes" do
assert {:ok, walex_supervisor_pid} = WalExSupervisor.start_link(@base_configs)

assert walex_supervisor_pid ==
GenServer.whereis(
WalExRegistry.set_name(:set_supervisor, WalExSupervisor, :test_name)
)

assert GenServer.whereis(
WalExRegistry.set_name(
:set_supervisor,
WalEx.Destinations.Supervisor,
:test_name
)
)
|> is_pid()

assert GenServer.whereis(
WalExRegistry.set_name(
:set_supervisor,
WalEx.Replication.Supervisor,
:test_name
)
)
|> is_pid()

assert GenServer.whereis(
WalExRegistry.set_name(:set_gen_server, WalEx.Destinations, :test_name)
)
|> is_pid()

assert GenServer.whereis(
WalExRegistry.set_name(:set_gen_server, WalEx.Replication.Server, :test_name)
)
|> is_pid()

assert GenServer.whereis(
WalExRegistry.set_name(:set_gen_server, WalEx.Replication.Publisher, :test_name)
)
|> is_pid()
end
end

describe "get_state/3" do
Expand All @@ -48,4 +104,15 @@ defmodule WalEx.Config.RegistryTest do
assert configs == WalExRegistry.get_state(:get_agent, __MODULE__, :app_name_test)
end
end

describe "find_pid" do
test "should find Supervisor" do
assert {:ok, walex_supervisor_pid} = WalExSupervisor.start_link(@base_configs)

assert walex_supervisor_pid ==
GenServer.whereis(
WalEx.Config.Registry.set_name(:set_supervisor, WalEx.Supervisor, :test_name)
)
end
end
end

0 comments on commit d378887

Please sign in to comment.