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

Supervisor process foundable with :via option #44

Merged
merged 5 commits into from
Jan 26, 2024
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
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
12 changes: 2 additions & 10 deletions lib/walex/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ defmodule WalEx.Supervisor do
alias WalEx.Replication.Supervisor, as: ReplicationSupervisor
alias WalExConfig.Registry, as: WalExRegistry

def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
}
end

def start_link(opts) do
app_name = Keyword.get(opts, :name)
module_names = build_module_names(app_name, opts)
Expand All @@ -26,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 @@ -70,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
Loading