forked from mercadona/postoffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MessageRecovery module (mercadona#56)
- Loading branch information
Showing
3 changed files
with
104 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Postoffice.Rescuer.MessageRecovery do | ||
alias Postoffice.Rescuer.Client | ||
|
||
require Logger | ||
|
||
def run(host) do | ||
Logger.info("Started MessageRecovery for host #{host}") | ||
case Client.list(host) do | ||
{:ok, []} -> | ||
Logger.info("Undelivered messages not found on #{host}") | ||
{:ok, messages} -> | ||
messages | ||
|> Enum.map(fn message -> handle_message(message, host) end) | ||
{:error, []} -> | ||
Logger.info("Error on #{host} listing undelivering messages") | ||
end | ||
end | ||
|
||
defp handle_message(message, host) do | ||
{id, message_params} = Map.pop(message, "id") | ||
case Postoffice.receive_message(message_params) do | ||
{:ok, created_message} -> | ||
Logger.info("Successfully recovered message for topic #{created_message["topic"]} with public_id #{created_message["public_id"]}") | ||
Client.delete(host, id) | ||
{:relationship_does_not_exists, _errors} -> | ||
Logger.info("Trying to receive message for non existing topic #{message["topic"]}") | ||
end | ||
end | ||
end |
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,73 @@ | ||
defmodule Postoffice.Rescuer.MessageRecoveryTest do | ||
use ExUnit.Case | ||
|
||
import Mox | ||
|
||
alias Postoffice.Fixtures | ||
alias Postoffice.Messaging | ||
alias Postoffice.Rescuer.Adapters.HttpMock | ||
alias Postoffice.Rescuer.MessageRecovery | ||
|
||
@origin_host "http://fake_origin.host" | ||
@first_message_id 1 | ||
@second_message_id 2 | ||
@one_message_response "[{\"id\": 1, \"topic\": \"test\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": {}}]" | ||
@two_messages_response "[{\"id\": 1, \"topic\": \"test\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": {}}, {\"id\": 2, \"topic\": \"test\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": {}}]" | ||
@wrong_topic_message "[{\"id\": 1, \"topic\": \"test2\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": {}}]" | ||
|
||
setup [:set_mox_from_context, :verify_on_exit!] | ||
|
||
setup do | ||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Postoffice.Repo) | ||
end | ||
|
||
describe "recover messages" do | ||
test "no message created if no undelivered message found" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}} | ||
end) | ||
|
||
MessageRecovery.run(@origin_host) | ||
assert Messaging.list_messages() == [] | ||
end | ||
|
||
test "message created if one undelivered message found" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: @one_message_response}} | ||
end) | ||
|
||
expect(HttpMock, :delete, fn @origin_host, @first_message_id -> | ||
{:ok, %HTTPoison.Response{status_code: 204}} | ||
end) | ||
|
||
Fixtures.create_topic() | ||
MessageRecovery.run(@origin_host) | ||
assert Kernel.length(Messaging.list_messages()) == 1 | ||
end | ||
|
||
test "more than one message is created if multiple undelivered messages found" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: @two_messages_response}} | ||
end) | ||
expect(HttpMock, :delete, fn @origin_host, @first_message_id -> | ||
{:ok, %HTTPoison.Response{status_code: 204}} | ||
end) | ||
expect(HttpMock, :delete, fn @origin_host, @second_message_id -> | ||
{:ok, %HTTPoison.Response{status_code: 204}} | ||
end) | ||
|
||
Fixtures.create_topic() | ||
MessageRecovery.run(@origin_host) | ||
assert Kernel.length(Messaging.list_messages()) == 2 | ||
end | ||
|
||
test "no message created if something fails" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: @wrong_topic_message}} | ||
end) | ||
|
||
MessageRecovery.run(@origin_host) | ||
assert Kernel.length(Messaging.list_messages()) == 0 | ||
end | ||
end | ||
end |