-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event handler and process manager monitor event store subscription an…
…d shutdown on terminate
- Loading branch information
1 parent
df1a6e0
commit 3d8c578
Showing
4 changed files
with
186 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Commanded.Event.EventHandlerSubscriptionTest do | ||
use Commanded.MockEventStoreCase | ||
|
||
defmodule ExampleHandler do | ||
use Commanded.Event.Handler, name: "ExampleHandler" | ||
end | ||
|
||
setup do | ||
{:ok, subscription} = start_subscription() | ||
{:ok, handler} = start_handler(subscription) | ||
|
||
[ | ||
handler: handler, | ||
subscription: subscription | ||
] | ||
end | ||
|
||
describe "event handler subscription" do | ||
test "should monitor subscription and terminate handler on shutdown", %{ | ||
handler: handler, | ||
subscription: subscription | ||
} do | ||
Process.unlink(handler) | ||
ref = Process.monitor(handler) | ||
|
||
shutdown_subscription(subscription) | ||
|
||
assert_receive {:DOWN, ^ref, :process, ^handler, :normal} | ||
end | ||
end | ||
|
||
defp start_subscription do | ||
pid = | ||
spawn(fn -> | ||
receive do | ||
:shutdown -> :ok | ||
end | ||
end) | ||
|
||
{:ok, pid} | ||
end | ||
|
||
defp start_handler(subscription) do | ||
reply_to = self() | ||
|
||
expect(MockEventStore, :subscribe_to, fn :all, "ExampleHandler", handler, :origin -> | ||
send(handler, {:subscribed, subscription}) | ||
send(reply_to, {:subscribed, subscription}) | ||
|
||
{:ok, subscription} | ||
end) | ||
|
||
{:ok, pid} = ExampleHandler.start_link() | ||
|
||
assert_receive {:subscribed, ^subscription} | ||
|
||
{:ok, pid} | ||
end | ||
|
||
defp shutdown_subscription(subscription) do | ||
ref = Process.monitor(subscription) | ||
|
||
send(subscription, :shutdown) | ||
|
||
assert_receive {:DOWN, ^ref, :process, _, _} | ||
end | ||
end |
75 changes: 75 additions & 0 deletions
75
test/process_managers/process_manager_subscription_test.exs
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,75 @@ | ||
defmodule Commanded.ProcessManagers.ProcessManagerSubscriptionTest do | ||
use Commanded.MockEventStoreCase | ||
|
||
defmodule ExampleRouter do | ||
end | ||
|
||
defmodule ExampleProcessManager do | ||
use Commanded.ProcessManagers.ProcessManager, | ||
name: "ExampleProcessManager", | ||
router: ExampleRouter | ||
|
||
@derive Jason.Encoder | ||
defstruct [:data] | ||
end | ||
|
||
setup do | ||
{:ok, subscription} = start_subscription() | ||
{:ok, pm} = start_process_manager(subscription) | ||
|
||
[ | ||
pm: pm, | ||
subscription: subscription | ||
] | ||
end | ||
|
||
describe "process manager subscription" do | ||
test "should monitor subscription and terminate process manager on shutdown", %{ | ||
pm: pm, | ||
subscription: subscription | ||
} do | ||
Process.unlink(pm) | ||
ref = Process.monitor(pm) | ||
|
||
shutdown_subscription(subscription) | ||
|
||
assert_receive {:DOWN, ^ref, :process, ^pm, :normal} | ||
end | ||
end | ||
|
||
defp start_subscription do | ||
pid = | ||
spawn(fn -> | ||
receive do | ||
:shutdown -> :ok | ||
end | ||
end) | ||
|
||
{:ok, pid} | ||
end | ||
|
||
defp start_process_manager(subscription) do | ||
reply_to = self() | ||
|
||
expect(MockEventStore, :subscribe_to, fn :all, "ExampleProcessManager", pm, :origin -> | ||
send(pm, {:subscribed, subscription}) | ||
send(reply_to, {:subscribed, subscription}) | ||
|
||
{:ok, subscription} | ||
end) | ||
|
||
{:ok, pid} = ExampleProcessManager.start_link() | ||
|
||
assert_receive {:subscribed, ^subscription} | ||
|
||
{:ok, pid} | ||
end | ||
|
||
defp shutdown_subscription(subscription) do | ||
ref = Process.monitor(subscription) | ||
|
||
send(subscription, :shutdown) | ||
|
||
assert_receive {:DOWN, ^ref, :process, _, _} | ||
end | ||
end |