diff --git a/lib/pigeon/connection.ex b/lib/pigeon/connection.ex index f0f04adb..9ead6670 100644 --- a/lib/pigeon/connection.ex +++ b/lib/pigeon/connection.ex @@ -92,13 +92,29 @@ defmodule Pigeon.Connection do end end - def handle_events(events, _from, state) do - state = - Enum.reduce(events, state, fn {:push, notif, opts}, state -> - send_push(state, notif, opts) - end) - - {:noreply, [], state} + def handle_events(events, from, %{config: config, socket: socket} = state) do + if socket && Process.alive?(socket) do + state = + Enum.reduce(events, state, fn {:push, notif, opts}, state -> + send_push(state, notif, opts) + end) + + {:noreply, [], state} + else + case connect_socket(config, 0) do + {:ok, socket} -> + Configurable.schedule_ping(config) + + handle_events(events, from, %Connection{ + config: state.config, + from: state.from, + socket: socket + }) + + {:error, reason} -> + {:stop, reason, state} + end + end end def process_end_stream(%Stream{id: stream_id} = stream, state) do diff --git a/test/adm/config_test.exs b/test/adm/config_test.exs index 8120497e..69df9857 100644 --- a/test/adm/config_test.exs +++ b/test/adm/config_test.exs @@ -5,8 +5,14 @@ defmodule Pigeon.ADM.ConfigTest do @invalid_key_msg ~r/^attempted to start without valid client id and secret/ test "success if configured with client id and secret" do - config = Pigeon.ADM.Config.new(name: :test, client_id: "amzn1.iba-client.abc123", client_secret: "abc123") - {:ok, _} = Pigeon.ADM.Worker.init({:ok, config}) + config = + Pigeon.ADM.Config.new( + name: :test, + client_id: "amzn1.iba-client.abc123", + client_secret: "abc123" + ) + + {:ok, _} = Pigeon.ADM.Worker.init({:ok, config}) end test "raises if configured with invalid client id" do @@ -18,7 +24,13 @@ defmodule Pigeon.ADM.ConfigTest do test "raises if configured with invalid client secret" do assert_raise(Pigeon.ConfigError, @invalid_key_msg, fn -> - config = Pigeon.ADM.Config.new(name: :test, client_id: "amzn1.iba-client.abc123", client_secret: nil) + config = + Pigeon.ADM.Config.new( + name: :test, + client_id: "amzn1.iba-client.abc123", + client_secret: nil + ) + Pigeon.ADM.Worker.init({:ok, config}) end) end