Skip to content

Commit

Permalink
Clean up duplicate fixture code
Browse files Browse the repository at this point in the history
  • Loading branch information
pmonson711 committed Oct 11, 2022
1 parent 87c1f93 commit 32b3056
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 137 deletions.
46 changes: 5 additions & 41 deletions test/beeline/dummy_producer_test.exs
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
defmodule Beeline.DummyProducerFixture do
@moduledoc """
A fixture event handler that subscribes to a dummy producer
"""

use Beeline

def start_link(test_proc) do
Beeline.start_link(__MODULE__,
name: __MODULE__,
producers: [
default: [
adapter: :dummy,
connection: nil,
stream_name: "dummy-stream"
]
],
spawn_health_checkers?: false,
# these options don't matter in test mode
auto_subscribe?: fn _producer -> false end,
get_stream_position: fn _producer -> -1 end,
context: test_proc
)
end

@impl GenStage
def handle_events([subscription_event], _from, test_proc) do
event = Beeline.decode_event(subscription_event)

if match?(%{poison?: true}, event) do
raise "inconceivable!"
end

send(test_proc, {:event, event})

{:noreply, [], test_proc}
end
end

defmodule Beeline.DummyProducerTest do
use ExUnit.Case, async: true

@moduletag :capture_log

@producer_id {Beeline.Topology.Producer, :default}
@fixture Beeline.DummyProducerFixture
@fixture Beeline.DummyNameFixture

setup do
[beeline_pid: start_supervised!({@fixture, self()})]
[
beeline_pid:
start_supervised!({@fixture, %{name: @fixture, proc: self()}})
]
end

test "the dummy handler can handle events" do
Expand Down
44 changes: 3 additions & 41 deletions test/beeline/global_registry_test.exs
Original file line number Diff line number Diff line change
@@ -1,52 +1,14 @@
defmodule Beeline.GlobalProducerFixture do
@moduledoc """
A fixture event handler that subscribes to a dummy producer
"""

use Beeline

def start_link(test_proc) do
Beeline.start_link(__MODULE__,
name: {:global, __MODULE__},
producers: [
default: [
adapter: :dummy,
connection: nil,
stream_name: "dummy-stream"
]
],
spawn_health_checkers?: false,
# these options don't matter in test mode
auto_subscribe?: fn _producer -> false end,
get_stream_position: fn _producer -> -1 end,
context: test_proc
)
end

@impl GenStage
def handle_events([subscription_event], _from, test_proc) do
event = Beeline.decode_event(subscription_event)

if match?(%{poison?: true}, event) do
raise "inconceivable!"
end

send(test_proc, {:event, event})

{:noreply, [], test_proc}
end
end

defmodule Beeline.GlobalRegistryTest do
use ExUnit.Case, async: true

@moduletag :capture_log

@producer_id {Beeline.Topology.Producer, :default}
@fixture Beeline.GlobalProducerFixture
@fixture Beeline.DummyNameFixture
@name {:global, @fixture}

setup do
[beeline_pid: start_supervised!({@fixture, self()})]
[beeline_pid: start_supervised!({@fixture, %{name: @name, proc: self()}})]
end

test "the dummy handler can handle events" do
Expand Down
62 changes: 7 additions & 55 deletions test/beeline/registry_test.exs
Original file line number Diff line number Diff line change
@@ -1,67 +1,25 @@
defmodule Beeline.RegistryProducerFixture do
@moduledoc """
A fixture event handler that subscribes to a dummy producer
"""

use Beeline

def start_link(test_proc) do
Beeline.start_link(__MODULE__,
name: {:via, Registry, {Registry.ViaTest, __MODULE__}},
producers: [
default: [
adapter: :dummy,
connection: nil,
stream_name: "dummy-stream"
]
],
spawn_health_checkers?: false,
# these options don't matter in test mode
auto_subscribe?: fn _producer -> false end,
get_stream_position: fn _producer -> -1 end,
context: test_proc
)
end

@impl GenStage
def handle_events([subscription_event], _from, test_proc) do
event = Beeline.decode_event(subscription_event)

if match?(%{poison?: true}, event) do
raise "inconceivable!"
end

send(test_proc, {:event, event})

{:noreply, [], test_proc}
end
end

defmodule Beeline.RegistryTest do
use ExUnit.Case, async: true

@moduletag :capture_log

@producer_id {Beeline.Topology.Producer, :default}
@fixture Beeline.RegistryProducerFixture
@fixture Beeline.DummyNameFixture
@name {:via, Registry, {Registry.ViaTest, @fixture}}

setup_all do
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
:ok
end

setup do
[beeline_pid: start_supervised!({@fixture, self()})]
[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,
{:via, Registry, {Registry.ViaTest, @fixture}}
)
:ok = Beeline.test_events(events, @name)

assert_receive {:event, event_a}
assert_receive {:event, event_b}
Expand All @@ -79,9 +37,7 @@ defmodule Beeline.RegistryTest do
producer_ref = Process.monitor(producer_pid)
consumer_ref = Process.monitor(consumer_pid)

assert Beeline.restart_stages(
{:via, Registry, {Registry.ViaTest, @fixture}}
) == :ok
assert Beeline.restart_stages(@name) == :ok

assert_receive {:DOWN, ^producer_ref, :process, ^producer_pid, :shutdown}
assert_receive {:DOWN, ^consumer_ref, :process, ^consumer_pid, :shutdown}
Expand All @@ -107,11 +63,7 @@ defmodule Beeline.RegistryTest do
good_event = %{foo: "bar"}
bad_event = %{poison?: true}

:ok =
Beeline.test_events(
[good_event, bad_event],
{:via, Registry, {Registry.ViaTest, @fixture}}
)
:ok = Beeline.test_events([good_event, bad_event], @name)

assert_receive {:event, ^good_event}
refute_receive {:event, ^bad_event}
Expand All @@ -130,7 +82,7 @@ defmodule Beeline.RegistryTest do
end

defp stage_children do
{:via, Registry, {Registry.ViaTest, @fixture}}
@name
|> Beeline.ProcessNaming.name(StageSupervisor)
|> Supervisor.which_children()
|> Enum.into(%{}, fn {id, pid, _, _} -> {id, pid} end)
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/dummy_fixture.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Beeline.DummyNameFixture do
@moduledoc """
A fixture event handler that subscribes to a dummy producer
"""

use Beeline

def start_link(%{name: name, proc: test_proc}) do
Beeline.start_link(__MODULE__,
name: name,
producers: [
default: [
adapter: :dummy,
connection: nil,
stream_name: "dummy-stream"
]
],
spawn_health_checkers?: false,
# these options don't matter in test mode
auto_subscribe?: fn _producer -> false end,
get_stream_position: fn _producer -> -1 end,
context: test_proc
)
end

@impl GenStage
def handle_events([subscription_event], _from, test_proc) do
event = Beeline.decode_event(subscription_event)

if match?(%{poison?: true}, event) do
raise "inconceivable!"
end

send(test_proc, {:event, event})

{:noreply, [], test_proc}
end
end

0 comments on commit 32b3056

Please sign in to comment.