Skip to content

Commit

Permalink
[#133] create a method for closing the apns connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ferigis committed Jan 26, 2017
1 parent ae73088 commit 2d28207
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 74 deletions.
10 changes: 8 additions & 2 deletions src/apns.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
%%% @copyright Inaka <hello@inaka.net>
%%%
-module(apns).
-author("Felipe Ripoll <felipes@inakanetworks.com>").
-author("Felipe Ripoll <felipe@inakanetworks.com>").

%% API
-export([ start/0
, stop/0
, connect/1
, close_connection/1
]).

%%%===================================================================
Expand All @@ -41,7 +42,7 @@ stop() ->
ok = application:stop(apns),
ok.

%% @doc Connect to APNs service with Provider Certificate
%% @doc Connects to APNs service with Provider Certificate
-spec connect( apns_connection:name()
| apns_connection:connection()) -> {ok, pid()}.
connect(ConnectionName) when is_atom(ConnectionName) ->
Expand All @@ -51,6 +52,11 @@ connect(Connection) ->
{ok, _} = apns_sup:create_connection(Connection),
{ok, whereis(apns_connection:name(Connection))}.

%% @doc Closes the connection with APNs service.
-spec close_connection(apns_connection:name()) -> ok.
close_connection(ConnectionName) ->
apns_connection:close_connection(ConnectionName).

%%%===================================================================
%%% Internal Functions
%%%===================================================================
13 changes: 11 additions & 2 deletions src/apns_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%%% @copyright Inaka <hello@inaka.net>
%%%
-module(apns_connection).
-author("Felipe Ripoll <ferigis@inakanetworks.com>").
-author("Felipe Ripoll <felipe@inakanetworks.com>").

-behaviour(gen_server).

Expand All @@ -30,6 +30,7 @@
, certfile/1
, keyfile/1
, gun_connection/1
, close_connection/1
]).

%% gen_server callbacks
Expand Down Expand Up @@ -89,6 +90,11 @@ default_connection(ConnectionName) ->
, keyfile => Keyfile
}.

%% @doc Close the connection with APNs gracefully
-spec close_connection(name()) -> ok.
close_connection(ConnectionName) ->
gen_server:call(ConnectionName, stop).

%% @doc Returns the gun's connection PID. This function is only used in tests.
-spec gun_connection(name()) -> pid().
gun_connection(ConnectionName) ->
Expand All @@ -113,6 +119,8 @@ init(Connection) ->
) -> {reply, ok, State}.
handle_call(gun_connection, _From, #{gun_connection := GunConn} = State) ->
{reply, GunConn, State};
handle_call(stop, _From, State) ->
{stop, normal, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.

Expand All @@ -136,7 +144,8 @@ handle_info(_Info, State) ->
-spec terminate( Reason :: (normal | shutdown | {shutdown, term()} | term())
, State :: map()
) -> term().
terminate(_Reason, _State) ->
terminate(_Reason, #{gun_connection := GunConn}) ->
ok = gun:shutdown(GunConn),
ok.

-spec code_change(OldVsn :: term() | {down, term()}
Expand Down
66 changes: 0 additions & 66 deletions src/apns_connection_sup.erl

This file was deleted.

8 changes: 4 additions & 4 deletions src/apns_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ init([]) ->
, period => 3600
},

Children = [#{ id => apns_connection_sup
, start => {apns_connection_sup, start_link, []}
Children = [#{ id => apns_connection
, start => {apns_connection, start_link, []}
, restart => transient
, shutdown => 5000
, type => supervisor
, modules => [apns_connection_sup]
, type => worker
, modules => [apns_connection]
}],

{ok, {SupFlags, Children}}.
4 changes: 4 additions & 0 deletions test/connection_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ connect(_Config) ->
{ok, ServerPid} = apns:connect(ConnectionName),
true = is_process_alive(ServerPid),
ServerPid = whereis(ConnectionName),
ok = apns:close_connection(ConnectionName),
undefined = whereis(ConnectionName),
ok.

-spec gun_connection_crashes(config()) -> ok.
Expand All @@ -75,6 +77,8 @@ gun_connection_crashes(_Config) ->
GunPid2 = apns_connection:gun_connection(ConnectionName),
true = is_process_alive(GunPid2),
true = (GunPid =/= GunPid2),
ok = apns:close_connection(ConnectionName),
false = is_process_alive(GunPid2),
[_] = meck:unload(),
ok.

Expand Down

0 comments on commit 2d28207

Please sign in to comment.