Skip to content

Commit

Permalink
Replace mock usage for dispatch (#946)
Browse files Browse the repository at this point in the history
* Use CommandAuditMiddleware to test the commands

* Use adapter on commanded dispatch to improve testability

* Inactivate heartbeat checker scheduler in test mode
  • Loading branch information
arbulu89 authored Nov 3, 2022
1 parent 71ecead commit 481be8a
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 159 deletions.
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :trento, Trento.Commanded, adapter: Trento.Commanded

config :trento, Trento.Commanded,
event_store: [
adapter: Commanded.EventStore.Adapters.EventStore,
Expand Down
7 changes: 7 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ config :trento, Trento.Integration.Checks.Wanda.Messaging.AMQP,
]
]

config :trento, Trento.Scheduler,
jobs: [
heartbeat_check: [
state: :inactive
]
]

config :trento,
extra_children: [
Trento.Messaging.Adapters.AMQP.Publisher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ defmodule Trento.RollupEventHandler do
defp after_max_retries_reached(%ClusterRolledUp{cluster_id: cluster_id}, _, _) do
%{cluster_id: cluster_id}
|> AbortClusterRollup.new!()
|> Trento.Commanded.dispatch()
|> commanded().dispatch()
end

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]
end
7 changes: 5 additions & 2 deletions lib/trento/application/integration/checks/checks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule Trento.Integration.Checks do
}) do
case StartChecksExecution.new(%{cluster_id: cluster_id}) do
{:ok, command} ->
Trento.Commanded.dispatch(command, correlation_id: execution_id)
commanded().dispatch(command, correlation_id: execution_id)

error ->
error
Expand All @@ -74,12 +74,15 @@ defmodule Trento.Integration.Checks do
with {:ok, execution_completed_event} <- ExecutionCompletedEventDto.new(payload),
{:ok, command} <-
build_complete_checks_execution_command(execution_completed_event) do
Trento.Commanded.dispatch(command, correlation_id: execution_id)
commanded().dispatch(command, correlation_id: execution_id)
end
end

def handle_callback(_), do: {:error, :invalid_payload}

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]

defp adapter,
do: Application.fetch_env!(:trento, __MODULE__)[:adapter]

Expand Down
7 changes: 5 additions & 2 deletions lib/trento/application/integration/discovery/discovery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Trento.Integration.Discovery do
@spec dispatch(command | [command]) :: :ok | {:error, any}
defp dispatch(commands) when is_list(commands) do
Enum.reduce(commands, :ok, fn command, acc ->
case {Trento.Commanded.dispatch(command), acc} do
case {commanded().dispatch(command), acc} do
{:ok, :ok} ->
:ok

Expand All @@ -140,5 +140,8 @@ defmodule Trento.Integration.Discovery do
end)
end

defp dispatch(command), do: Trento.Commanded.dispatch(command)
defp dispatch(command), do: commanded().dispatch(command)

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]
end
9 changes: 6 additions & 3 deletions lib/trento/application/usecases/clusters/clusters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ defmodule Trento.Clusters do
host_id: host_id,
checks_results: build_check_results(checks_results)
}) do
Trento.Commanded.dispatch(command)
commanded().dispatch(command)
end
end

@spec select_checks(String.t(), [String.t()]) :: :ok | {:error, any}
def select_checks(cluster_id, checks) do
with {:ok, command} <- SelectChecks.new(%{cluster_id: cluster_id, checks: checks}) do
Trento.Commanded.dispatch(command)
commanded().dispatch(command)
end
end

@spec request_checks_execution(String.t()) :: :ok | {:error, any}
def request_checks_execution(cluster_id) do
with {:ok, command} <- RequestChecksExecution.new(%{cluster_id: cluster_id}) do
Trento.Commanded.dispatch(command)
commanded().dispatch(command)
end
end

Expand Down Expand Up @@ -96,6 +96,9 @@ defmodule Trento.Clusters do
end)
end

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]

@spec build_check_results([String.t()]) :: {:ok, [CheckResult.t()]} | {:error, any}
defp build_check_results(checks_results) do
Enum.map(checks_results, fn c ->
Expand Down
5 changes: 4 additions & 1 deletion lib/trento/application/usecases/hosts/heartbeats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ defmodule Trento.Heartbeats do
defp dispatch_command(agent_id, heartbeat) do
case %{host_id: agent_id, heartbeat: heartbeat}
|> UpdateHeartbeat.new!()
|> Trento.Commanded.dispatch() do
|> commanded().dispatch() do
:ok ->
{:ok, :done}

{:error, _} = error ->
error
end
end

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]
end
8 changes: 8 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Trento.Factory do
ClusterReadModel,
DatabaseInstanceReadModel,
DatabaseReadModel,
Heartbeat,
HostChecksExecutionsReadModel,
HostConnectionSettings,
HostReadModel,
Expand Down Expand Up @@ -102,6 +103,13 @@ defmodule Trento.Factory do
}
end

def heartbeat_factory do
%Heartbeat{
agent_id: Faker.UUID.v4(),
timestamp: DateTime.utc_now()
}
end

def host_connection_settings_factory do
%HostConnectionSettings{
id: Faker.UUID.v4(),
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Mox.defmock(Trento.Commanded.Mock, for: Commanded.Application)

Application.put_env(:trento, Trento.Commanded, adapter: Trento.Commanded.Mock)

Mox.defmock(Trento.Integration.Telemetry.Mock, for: Trento.Integration.Telemetry.Gen)

Application.put_env(:trento, Trento.Integration.Telemetry,
Expand Down
Loading

0 comments on commit 481be8a

Please sign in to comment.