Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project broadcast application instance moved #1570

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/trento/application/projectors/sap_system_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Trento.SapSystemProjector do
alias Trento.Domain.Events.{
ApplicationInstanceDeregistered,
ApplicationInstanceHealthChanged,
ApplicationInstanceMoved,
ApplicationInstanceRegistered,
SapSystemDeregistered,
SapSystemHealthChanged,
Expand Down Expand Up @@ -97,6 +98,26 @@ defmodule Trento.SapSystemProjector do
end
)

project(
%ApplicationInstanceMoved{
sap_system_id: sap_system_id,
instance_number: instance_number,
old_host_id: old_host_id,
new_host_id: new_host_id
},
fn multi ->
instance =
Repo.get_by(ApplicationInstanceReadModel,
sap_system_id: sap_system_id,
instance_number: instance_number,
host_id: old_host_id
)

changeset = ApplicationInstanceReadModel.changeset(instance, %{host_id: new_host_id})
Ecto.Multi.update(multi, :application_instance, changeset)
end
)

project(
%ApplicationInstanceHealthChanged{
sap_system_id: sap_system_id,
Expand Down Expand Up @@ -237,6 +258,31 @@ defmodule Trento.SapSystemProjector do
)
end

@impl true
def after_update(
%ApplicationInstanceMoved{
sap_system_id: sap_system_id,
instance_number: instance_number,
old_host_id: old_host_id,
new_host_id: new_host_id
},
_,
_
) do
TrentoWeb.Endpoint.broadcast(
@sap_systems_topic,
"application_instance_moved",
SapSystemView.render("application_instance_moved.json",
instance_moved: %{
sap_system_id: sap_system_id,
instance_number: instance_number,
old_host_id: old_host_id,
new_host_id: new_host_id
}
)
)
end

@impl true
def after_update(
%ApplicationInstanceHealthChanged{},
Expand Down
3 changes: 3 additions & 0 deletions lib/trento_web/views/v1/sap_system_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ defmodule TrentoWeb.V1.SapSystemView do
|> Map.delete(:host)
end

def render("application_instance_moved.json", %{instance_moved: instance_moved}),
do: instance_moved

def render("application_instance_health_changed.json", %{health: health}), do: health

def render("sap_systems.json", %{sap_systems: sap_systems}) do
Expand Down
160 changes: 105 additions & 55 deletions test/trento/application/projectors/sap_system_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Trento.SapSystemProjectorTest do
alias Trento.Domain.Events.{
ApplicationInstanceDeregistered,
ApplicationInstanceHealthChanged,
ApplicationInstanceMoved,
SapSystemDeregistered,
SapSystemHealthChanged,
SapSystemRestored,
Expand Down Expand Up @@ -60,16 +61,18 @@ defmodule Trento.SapSystemProjectorTest do
assert event.health == projection.health
assert event.ensa_version == projection.ensa_version

assert_broadcast "sap_system_registered",
%{
db_host: ^db_host,
health: ^health,
id: ^id,
sid: ^sid,
tenant: ^tenant,
ensa_version: ^ensa_version
},
1000
assert_broadcast(
"sap_system_registered",
%{
db_host: ^db_host,
health: ^health,
id: ^id,
sid: ^sid,
tenant: ^tenant,
ensa_version: ^ensa_version
},
1000
)
end

test "should update the health of a SAP System when a SapSystemHealthChanged event is received" do
Expand All @@ -81,12 +84,14 @@ defmodule Trento.SapSystemProjectorTest do

assert event.health == projection.health

assert_broadcast "sap_system_health_changed",
%{
id: ^sap_system_id,
health: ^health
},
1000
assert_broadcast(
"sap_system_health_changed",
%{
id: ^sap_system_id,
health: ^health
},
1000
)
end

test "should project a new application instance when ApplicationInstanceRegistered event is received" do
Expand Down Expand Up @@ -117,20 +122,57 @@ defmodule Trento.SapSystemProjectorTest do
assert event.host_id == projection.host_id
assert event.health == projection.health

assert_broadcast "application_instance_registered",
%{
features: ^features,
health: ^health,
host_id: ^host_id,
http_port: 8080,
https_port: 8443,
instance_hostname: ^instance_hostname,
instance_number: ^instance_number,
sap_system_id: ^sap_system_id,
sid: ^sid,
start_priority: ^start_priority
},
1000
assert_broadcast(
"application_instance_registered",
%{
features: ^features,
health: ^health,
host_id: ^host_id,
http_port: 8080,
https_port: 8443,
instance_hostname: ^instance_hostname,
instance_number: ^instance_number,
sap_system_id: ^sap_system_id,
sid: ^sid,
start_priority: ^start_priority
},
1000
)
end

test "should move the application instance when an ApplicationInstanceMoved is received" do
insert(:sap_system, id: sap_system_id = Faker.UUID.v4())
fabriziosestito marked this conversation as resolved.
Show resolved Hide resolved

%{host_id: old_host_id, instance_number: instance_number} =
insert(:application_instance, sap_system_id: sap_system_id)

event = %ApplicationInstanceMoved{
sap_system_id: sap_system_id,
instance_number: instance_number,
old_host_id: old_host_id,
new_host_id: new_host_id = Faker.UUID.v4()
}

ProjectorTestHelper.project(SapSystemProjector, event, "sap_system_projector")

application_instance =
Repo.get_by!(ApplicationInstanceReadModel,
sap_system_id: event.sap_system_id,
instance_number: event.instance_number
)

assert application_instance.host_id == event.new_host_id

assert_broadcast(
"application_instance_moved",
%{
sap_system_id: ^sap_system_id,
instance_number: ^instance_number,
old_host_id: ^old_host_id,
new_host_id: ^new_host_id
},
1000
)
end

test "should broadcast application_instance_health_changed when ApplicationInstanceHealthChanged event is received" do
Expand Down Expand Up @@ -158,14 +200,16 @@ defmodule Trento.SapSystemProjectorTest do

ProjectorTestHelper.project(SapSystemProjector, health_event, "sap_system_projector")

assert_broadcast "application_instance_health_changed",
%{
health: :critical,
host_id: ^host_id,
instance_number: ^instance_number,
sap_system_id: ^sap_system_id
},
1000
assert_broadcast(
"application_instance_health_changed",
%{
health: :critical,
host_id: ^host_id,
instance_number: ^instance_number,
sap_system_id: ^sap_system_id
},
1000
)
end

test "should update read model after deregistration" do
Expand All @@ -182,9 +226,11 @@ defmodule Trento.SapSystemProjectorTest do

projection = Repo.get(SapSystemReadModel, sap_system_id)

assert_broadcast "sap_system_deregistered",
%{id: ^sap_system_id, sid: ^sid},
1000
assert_broadcast(
"sap_system_deregistered",
%{id: ^sap_system_id, sid: ^sid},
1000
)

assert deregistered_at == projection.deregistered_at
end
Expand Down Expand Up @@ -251,14 +297,16 @@ defmodule Trento.SapSystemProjectorTest do
|> Repo.all()
|> Enum.count()

assert_broadcast "application_instance_deregistered",
%{
sap_system_id: ^sap_system_id,
instance_number: ^instance_number,
host_id: ^host_id,
sid: ^sid
},
1000
assert_broadcast(
"application_instance_deregistered",
%{
sap_system_id: ^sap_system_id,
instance_number: ^instance_number,
host_id: ^host_id,
sid: ^sid
},
1000
)
end

test "should update an already existing SAP System when a SapSystemUpdated event is received" do
Expand All @@ -278,11 +326,13 @@ defmodule Trento.SapSystemProjectorTest do

assert event.ensa_version == ensa_version

assert_broadcast "sap_system_updated",
%{
id: ^id,
ensa_version: ^ensa_version
},
1000
assert_broadcast(
"sap_system_updated",
%{
id: ^id,
ensa_version: ^ensa_version
},
1000
)
end
end