Skip to content

Commit

Permalink
Add integration test for lost connection (#134)
Browse files Browse the repository at this point in the history
* Update sparrow

* Update error handling

* Bump version to 2.0.0

Co-authored-by: Rafał Słota <rafal.slota92@gmail.com>
  • Loading branch information
kmakiela and Rafał Słota authored Jan 28, 2020
1 parent da4d37a commit 95e8850
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
9 changes: 0 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,6 @@ workflows:
requires:
- pre-build
filters: *all_tags
- test-erlang-22_elixir-1-7:
requires:
- pre-build
filters: *all_tags
- test-erlang-21_elixir-1-9:
requires:
- pre-build
Expand All @@ -305,11 +301,6 @@ workflows:
requires:
- pre-build
filters: *all_tags
- test-erlang-21_elixir-1-7:
requires:
- pre-build
filters: *all_tags

- build-tar-and-docker-image:
requires:
- pre-build
Expand Down
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ config :elixometer,
metric_prefix: "mongoose_push"

config :mongoose_push, loglevel: :debug
config :goth, endpoint: "http://fcm-mock:4001"

config :maru, MongoosePush.Router,
versioning: [
Expand Down
4 changes: 3 additions & 1 deletion lib/mongoose_push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ defmodule MongoosePush do

defp maybe_log({:error, {type, reason}} = return_value) do
Logger.warn(
~s"Unable to complete push request due to service error: #{reason} in category: #{type}"
~s"Unable to complete push request due to service error: #{inspect(reason)} in category: #{
type
}"
)

return_value
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoose_push/api/v2/response_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule MongoosePush.API.V2.ResponseEncoder do
{non_neg_integer, %{details: atom | String.t()} | nil}
def to_status(:ok), do: {200, nil}

def to_status({:error, :unable_to_connect}) do
def to_status({:error, {:generic, :unable_to_connect}}) do
{503, %{:details => "Please try again later"}}
end

Expand Down
4 changes: 3 additions & 1 deletion lib/mongoose_push/service/apns/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule MongoosePush.Service.APNS.ErrorHandler do

# More information about possible return codes is here:
# https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html
@spec translate_error_reason(Service.error_reason()) :: Service.error()
@spec translate_error_reason(Service.error_reason() | {Service.error_reason(), any()}) ::
Service.error()
def translate_error_reason(:BadCollapseId), do: {:invalid_request, :BadCollapseId}
def translate_error_reason(:BadDeviceToken), do: {:invalid_request, :BadDeviceToken}

Expand Down Expand Up @@ -49,5 +50,6 @@ defmodule MongoosePush.Service.APNS.ErrorHandler do

def translate_error_reason(:PayloadTooLarge), do: {:payload_too_large, :PayloadTooLarge}

def translate_error_reason({reason, _specific}), do: {:unknown, reason}
def translate_error_reason(reason), do: {:unknown, reason}
end
5 changes: 4 additions & 1 deletion lib/mongoose_push/service/fcm/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule MongoosePush.Service.FCM.ErrorHandler do

# More information about possible return codes is here:
# https://firebase.google.com/docs/reference/fcm/rest/v1/ErrorCode
@spec translate_error_reason(Service.error_reason()) :: Service.error()
@spec translate_error_reason(Service.error_reason() | {Service.error_reason(), any()}) ::
Service.error()
def translate_error_reason(:INVALID_ARGUMENT), do: {:invalid_request, :INVALID_ARGUMENT}
def translate_error_reason(:SENDER_ID_MISMATCH), do: {:auth, :SENDER_ID_MISMATCH}
def translate_error_reason(:UNREGISTERED), do: {:unregistered, :UNREGISTERED}
Expand All @@ -16,5 +17,7 @@ defmodule MongoosePush.Service.FCM.ErrorHandler do
def translate_error_reason(:THIRD_PARTY_AUTH_ERROR), do: {:auth, :THIRD_PARTY_AUTH_ERROR}
def translate_error_reason(:UNAVAILABLE), do: {:service_internal, :UNAVAILABLE}
def translate_error_reason(:INTERNAL), do: {:service_internal, :INTERNAL}

def translate_error_reason({reason, _specific}), do: {:unknown, reason}
def translate_error_reason(reason), do: {:unknown, reason}
end
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule MongoosePush.Mixfile do
def project do
[
app: :mongoose_push,
version: "2.0.0-beta.2",
version: "2.0.0",
elixir: "~> 1.5",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand All @@ -30,8 +30,8 @@ defmodule MongoosePush.Mixfile do

defp deps do
[
{:chatterbox, github: "joedevivo/chatterbox", ref: "ff0c2e0", override: true},
{:sparrow, github: "esl/sparrow", ref: "d1a30cf"},
{:chatterbox, github: "rslota/chatterbox", ref: "fe7236a", override: true},
{:sparrow, github: "esl/sparrow", ref: "72f2898f"},
{:maru, github: "rslota/maru", ref: "54fc038", override: true},
{:plug_cowboy, "~> 2.0"},
{:cowboy, "~> 2.3", override: true},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bear": {:hex, :bear, "0.8.7", "16264309ae5d005d03718a5c82641fcc259c9e8f09adeb6fd79ca4271168656f", [:rebar3], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"chatterbox": {:git, "https://github.com/joedevivo/chatterbox.git", "ff0c2e054430d2990b588afa6fb8f2d184dfeaea", [ref: "ff0c2e0"]},
"chatterbox": {:git, "https://github.com/rslota/chatterbox.git", "fe7236a9c19b130ae1189dc48e2b7c3ccc58621d", [ref: "fe7236a"]},
"confex": {:hex, :confex, "3.4.0", "8b1c3cc7a93320291abb31223a178df19d7f722ee816c05a8070c8c9a054560d", [:mix], [], "hexpm"},
"cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm"},
Expand Down Expand Up @@ -51,7 +51,7 @@
"pollution": {:hex, :pollution, "0.9.2", "3f67542631071c99f807d2a8f9da799c07cd983c902f5357b9e1569c20a26e76", [:mix], [], "hexpm"},
"quixir": {:hex, :quixir, "0.9.3", "f01c37386b9e1d0526f01a8734a6d7884af294a0ec360f05c24c7171d74632bd", [:mix], [{:pollution, "~> 0.9.2", [hex: :pollution, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
"sparrow": {:git, "https://github.com/esl/sparrow.git", "d1a30cf370dfaa7e4a7531bf79c0caabc459446f", [ref: "d1a30cf"]},
"sparrow": {:git, "https://github.com/esl/sparrow.git", "72f2898f8baeff34f1a233eb3f91b484de9512d3", [ref: "72f2898f"]},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm"},
Expand Down
13 changes: 9 additions & 4 deletions test/integration/connection_lost_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ defmodule MongoosePush.API.ConnectionTest do
:ok
end

# We have to skip it until MIM-748 is resolved
@tag :skip
test "When connection to FCM is lost and regained a PN succeeds" do
Tools.mock_fcm("/connection", %{"https" => false})

eventually(assert {500, ""} == Tools.post_conn_error(@url, Tools.sample_notification(:fcm)))
Enum.each(1..5, fn _ ->
eventually(assert {503, _} = Tools.post_conn_error(@url, Tools.sample_notification(:fcm)))
Process.sleep(1000)
end)

Tools.mock_fcm("/connection", %{"https" => true})

eventually(assert {200, nil} == Tools.post(@url, Tools.sample_notification(:fcm)))
Enum.each(1..5, fn _ ->
eventually(assert {200, _} = Tools.post_conn_error(@url, Tools.sample_notification(:fcm)))

Process.sleep(1000)
end)
end
end

0 comments on commit 95e8850

Please sign in to comment.