-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from NFIBrokerage/allow-dynamic-named-processes
Allow beeline to match Elixir's OTP naming
- Loading branch information
Showing
11 changed files
with
278 additions
and
68 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
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
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,33 @@ | ||
defmodule Beeline.ProcessNaming do | ||
@moduledoc false | ||
|
||
defmodule Guards do | ||
@moduledoc false | ||
# coveralls-ignore-start | ||
defguard is_beeline_name(name) | ||
when is_atom(name) or | ||
(is_tuple(name) and elem(name, 0) == :global and | ||
tuple_size(name) == 2) or | ||
(is_tuple(name) and elem(name, 0) == :via and | ||
elem(name, 1) == Registry and tuple_size(name) == 3) | ||
|
||
# coveralls-ignore-stop | ||
end | ||
|
||
# Provides common logic for standard OTP server_name types. | ||
def(name(%Beeline.Config{name: name}, appended_name)) do | ||
name(name, appended_name) | ||
end | ||
|
||
def name(base_name, appended_name) when is_atom(base_name) do | ||
Module.concat(base_name, appended_name) | ||
end | ||
|
||
def name({:global, base_name}, appended_name) do | ||
{:global, {base_name, appended_name}} | ||
end | ||
|
||
def name({:via, Registry, {registry_name, base_name}}, appended_name) do | ||
{:via, Registry, {registry_name, {base_name, appended_name}}} | ||
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
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
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,85 @@ | ||
defmodule Beeline.GlobalRegistryTest do | ||
use ExUnit.Case, async: true | ||
|
||
@moduletag :capture_log | ||
|
||
@producer_id {Beeline.Topology.Producer, :default} | ||
@fixture Beeline.DummyNameFixture | ||
@name {:global, @fixture} | ||
|
||
setup do | ||
[beeline_pid: start_supervised!({@fixture, %{name: @name, proc: self()}})] | ||
end | ||
|
||
test "the dummy handler can handle events" do | ||
events = [%{foo: "bar"}, %{foo: "bar"}, %{foo: "bar"}] | ||
|
||
:ok = Beeline.test_events(events, {:global, @fixture}) | ||
|
||
assert_receive {:event, event_a} | ||
assert_receive {:event, event_b} | ||
assert_receive {:event, event_c} | ||
|
||
assert [event_a, event_b, event_c] == events | ||
end | ||
|
||
test "the dummy producer and handler are both restarted with restart_stages/1" do | ||
%{ | ||
@producer_id => producer_pid, | ||
@fixture => consumer_pid | ||
} = stage_children() | ||
|
||
producer_ref = Process.monitor(producer_pid) | ||
consumer_ref = Process.monitor(consumer_pid) | ||
|
||
assert Beeline.restart_stages({:global, @fixture}) == :ok | ||
|
||
assert_receive {:DOWN, ^producer_ref, :process, ^producer_pid, :shutdown} | ||
assert_receive {:DOWN, ^consumer_ref, :process, ^consumer_pid, :shutdown} | ||
|
||
%{ | ||
@producer_id => producer_pid, | ||
@fixture => consumer_pid | ||
} = stage_children() | ||
|
||
assert Process.alive?(producer_pid) | ||
assert Process.alive?(consumer_pid) | ||
end | ||
|
||
test "when the consumer raises on an event, it kills the producer as well" do | ||
%{ | ||
@producer_id => producer_pid, | ||
@fixture => consumer_pid | ||
} = stage_children() | ||
|
||
producer_ref = Process.monitor(producer_pid) | ||
consumer_ref = Process.monitor(consumer_pid) | ||
|
||
good_event = %{foo: "bar"} | ||
bad_event = %{poison?: true} | ||
|
||
:ok = Beeline.test_events([good_event, bad_event], {:global, @fixture}) | ||
|
||
assert_receive {:event, ^good_event} | ||
refute_receive {:event, ^bad_event} | ||
|
||
assert_receive {:DOWN, ^producer_ref, :process, ^producer_pid, :shutdown} | ||
assert_receive {:DOWN, ^consumer_ref, :process, ^consumer_pid, _error} | ||
|
||
# then the producer and consumer are restarted | ||
%{ | ||
@producer_id => producer_pid, | ||
@fixture => consumer_pid | ||
} = stage_children() | ||
|
||
assert Process.alive?(producer_pid) | ||
assert Process.alive?(consumer_pid) | ||
end | ||
|
||
defp stage_children do | ||
{:global, @fixture} | ||
|> Beeline.ProcessNaming.name(StageSupervisor) | ||
|> Supervisor.which_children() | ||
|> Enum.into(%{}, fn {id, pid, _, _} -> {id, pid} end) | ||
end | ||
end |
Oops, something went wrong.