Packages
commanded
0.11.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/assertions/event_assertions.ex
defmodule Commanded.Assertions.EventAssertions do
import ExUnit.Assertions
use Commanded.EventStore
@default_receive_timeout 1_000
@doc """
Wait for an event of the given event type to be published
"""
def wait_for_event(event_type) do
wait_for_event(event_type, fn _event -> true end, @default_receive_timeout)
end
@doc """
Wait for an event of the given event type to be published until the timeout.
"""
def wait_for_event(event_type, timeout) when is_integer(timeout) do
wait_for_event(event_type, fn _event -> true end, timeout)
end
@doc """
Wait for an event of the given event type, matching the predicate, to be published.
"""
def wait_for_event(event_type, predicate_fn) when is_function(predicate_fn) do
wait_for_event(event_type, predicate_fn, @default_receive_timeout)
end
@doc """
Wait for an event of the given event type, matching the predicate, to be published until the timeout.
"""
def wait_for_event(event_type, predicate_fn, timeout) when is_function(predicate_fn) and is_integer(timeout) do
with_subscription(fn subscription ->
do_wait_for_event(subscription, event_type, predicate_fn, timeout)
end)
end
@doc """
Assert that an event of the given event type is published. Verify that event using the assertion function.
"""
def assert_receive_event(event_type, assertion_fn) do
assert_receive_event(event_type, fn _event -> true end, assertion_fn)
end
@doc """
Assert that an event of the given event type, matching the predicate, is published. Verify that event using the assertion function.
"""
def assert_receive_event(event_type, predicate_fn, assertion_fn) do
with_subscription(fn subscription ->
do_assert_receive(subscription, event_type, predicate_fn, assertion_fn)
end)
end
defp with_subscription(callback_fn) do
subscription_name = UUID.uuid4
{:ok, subscription} = create_subscription(subscription_name)
try do
apply(callback_fn, [subscription])
after
remove_subscription(subscription_name)
end
end
defp do_assert_receive(subscription, event_type, predicate_fn, assertion_fn) do
assert_receive {:events, received_events}, @default_receive_timeout
ack_events(subscription, received_events)
expected_type = Atom.to_string(event_type)
expected_event = Enum.find(received_events, fn received_event ->
case received_event.event_type do
^expected_type ->
case apply(predicate_fn, [received_event.data]) do
true -> received_event
_ -> false
end
_ -> false
end
end)
case expected_event do
nil -> do_assert_receive(subscription, event_type, predicate_fn, assertion_fn)
received_event -> apply(assertion_fn, [received_event.data])
end
end
defp do_wait_for_event(subscription, event_type, predicate_fn, timeout) do
assert_receive {:events, received_events}, timeout
ack_events(subscription, received_events)
expected_type = Atom.to_string(event_type)
expected_event = Enum.find(received_events, fn received_event ->
case received_event.event_type do
^expected_type -> apply(predicate_fn, [received_event.data])
_ -> false
end
end)
case expected_event do
nil -> do_wait_for_event(subscription, event_type, predicate_fn, timeout)
received_event -> received_event
end
end
defp create_subscription(subscription_name), do: @event_store.subscribe_to_all_streams(subscription_name, self(), :origin)
defp remove_subscription(subscription_name), do: @event_store.unsubscribe_from_all_streams(subscription_name)
defp ack_events(subscription, events), do: @event_store.ack_event(subscription, List.last(events))
end