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

Main #2

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 23 additions & 7 deletions lib/pigeon/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions test/adm/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down