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

[#129] monitor gun connection #130

Merged
merged 1 commit into from
Jan 25, 2017
Merged
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
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
{test, [
{deps, [
{katana_test, "0.1.1"},
{mixer, "0.1.5", {pkg, inaka_mixer}}
{mixer, "0.1.5", {pkg, inaka_mixer}},
{meck, "0.8.4"}
]}
]}
]}.
Expand Down
47 changes: 38 additions & 9 deletions src/apns_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
, port/1
, certfile/1
, keyfile/1
, gun_connection/1
]).

%% gen_server callbacks
Expand Down Expand Up @@ -59,6 +60,7 @@

-type state() :: #{ connection := connection()
, gun_connection := pid()
, gun_monitor := reference()
}.

%%%===================================================================
Expand Down Expand Up @@ -87,27 +89,30 @@ default_connection(ConnectionName) ->
, keyfile => Keyfile
}.

%% @doc Returns the gun's connection PID. This function is only used in tests.
-spec gun_connection(name()) -> pid().
gun_connection(ConnectionName) ->
gen_server:call(ConnectionName, gun_connection).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

-spec init(connection()) -> {ok, State :: state()}.
init(Connection) ->
Certfile = certfile(Connection),
Keyfile = keyfile(Connection),
Host = host(Connection),
Port = port(Connection),
TransportOpts = [{certfile, Certfile}, {keyfile, Keyfile}],
{ok, ConnPid} = gun:open(Host, Port, #{protocols => [http2]
, transport_opts => TransportOpts
}),
{ok, #{connection => Connection, gun_connection => ConnPid}}.
{GunMonitor, GunConnectionPid} = open_gun_connection(Connection),
{ok, #{ connection => Connection
, gun_connection => GunConnectionPid
, gun_monitor => GunMonitor
}}.

-spec handle_call( Request :: term()
, From :: {pid()
, Tag :: term()}
, State
) -> {reply, ok, State}.
handle_call(gun_connection, _From, #{gun_connection := GunConn} = State) ->
{reply, GunConn, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.

Expand All @@ -118,6 +123,13 @@ handle_cast(_Request, State) ->

-spec handle_info(Info :: timeout() | term(), State) ->
{noreply, State}.
handle_info( {'DOWN', GunMonitor, process, GunConnPid, _}
, #{ gun_connection := GunConnPid
, gun_monitor := GunMonitor
, connection := Connection
} = State) ->
{GunMonitor2, GunConnPid2} = open_gun_connection(Connection),
{noreply, State#{gun_connection => GunConnPid2, gun_monitor => GunMonitor2}};
handle_info(_Info, State) ->
{noreply, State}.

Expand Down Expand Up @@ -161,3 +173,20 @@ keyfile(#{keyfile := Keyfile}) ->
%%%===================================================================
%%% Internal Functions
%%%===================================================================

-spec open_gun_connection(connection()) -> { GunMonitor :: reference()
, GunConnectionPid :: pid()
}.
open_gun_connection(Connection) ->
Certfile = certfile(Connection),
Keyfile = keyfile(Connection),
Host = host(Connection),
Port = port(Connection),
TransportOpts = [{certfile, Certfile}, {keyfile, Keyfile}],
{ok, GunConnectionPid} = gun:open( Host
, Port
, #{ protocols => [http2]
, transport_opts => TransportOpts
}),
GunMonitor = monitor(process, GunConnectionPid),
{GunMonitor, GunConnectionPid}.
31 changes: 31 additions & 0 deletions test/connection_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

-export([ default_connection/1
, connect/1
, gun_connection_crashes/1
]).

-type config() :: [{atom(), term()}].
Expand All @@ -19,6 +20,7 @@
-spec all() -> [atom()].
all() -> [ default_connection
, connect
, gun_connection_crashes
].

-spec init_per_suite(config()) -> config().
Expand Down Expand Up @@ -57,3 +59,32 @@ connect(_Config) ->
true = is_process_alive(ServerPid),
ServerPid = whereis(ConnectionName),
ok.

-spec gun_connection_crashes(config()) -> ok.
gun_connection_crashes(_Config) ->
ok = meck:expect(gun, open, fun(_, _, _) ->
{ok, spawn(fun crash/0)}
end),
ConnectionName = my_connection2,
{ok, _ServerPid} = apns:connect(ConnectionName),
GunPid = apns_connection:gun_connection(ConnectionName),
true = is_process_alive(GunPid),
GunPid ! crash,
timer:sleep(1000),
false = is_process_alive(GunPid),
GunPid2 = apns_connection:gun_connection(ConnectionName),
true = is_process_alive(GunPid2),
true = (GunPid =/= GunPid2),
[_] = meck:unload(),
ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Internal Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec crash() -> ok.
crash() ->
receive
crash -> exit(crashed);
_ -> ok
end.