Skip to content

Commit

Permalink
Handle 'close' frames and 'normal' gun_down messages (#33)
Browse files Browse the repository at this point in the history
* Handle 'close' frames and 'normal' gun_down messages

* Remain generic on the Error Code

* Add test case

* Add info log about normal exit
  • Loading branch information
ziopio authored Jun 13, 2024
1 parent 3b57b80 commit 2ccd077
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/grisp_connect_ws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ handle_info({gun_ws, Pid, Stream, {text, Text}},
#state{gun_pid = Pid, ws_stream = Stream} = S) ->
grisp_connect_client:handle_message(Text),
{noreply, S};
handle_info({gun_ws, Pid, Stream, {close, Code, Message}},
#state{gun_pid = Pid, ws_stream = Stream} = S) ->
?LOG_WARNING(#{event => stream_closed, code => Code, reason => Message}),
{noreply, S};
handle_info({gun_down, Pid, ws, closed, [Stream]}, #state{gun_pid = Pid, ws_stream = Stream} = S) ->
?LOG_WARNING(#{event => ws_closed}),
grisp_connect_client:disconnected(),
{noreply, shutdown_gun(S)};
handle_info({gun_down, Pid, ws, normal, _}, #state{gun_pid = Pid} = S) ->
?LOG_INFO(#{event => ws_closed, reason => normal}),
grisp_connect_client:disconnected(),
{noreply, shutdown_gun(S)};
handle_info({'DOWN', _, process, Pid, Reason}, #state{gun_pid = Pid,
ping_timer = Tref} = S) ->
?LOG_WARNING(#{event => gun_crash, reason => Reason}),
Expand Down
19 changes: 19 additions & 0 deletions test/grisp_connect_reconnect_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ init_per_testcase(_, Config) ->
KraftRef = grisp_connect_manager:kraft_start(?config(cert_dir, Config)),
{ok, _} = application:ensure_all_started(grisp_emulation),
application:set_env(grisp_connect, test_cert_dir, ?config(cert_dir, Config)),
application:set_env(grisp_connect, ws_ping_timeout, 60_000),
{ok, _} = application:ensure_all_started(grisp_connect),
[{kraft_instance, KraftRef} | Config].

Expand Down Expand Up @@ -86,6 +87,24 @@ reconnect_on_ping_timeout_test(_) ->
?assertMatch(ok, wait_connection(100)),
?assertMatch(ok, wait_disconnection()).

reconnect_on_closed_frame_test(_) ->
?assertMatch(ok, wait_connection()),
% Delete a table to cause an internal crash, this is handled by cowboy
% and generates a close frame,
% triggering a normal exit from gun on the client
mnesia:delete_table(grisp_manager_token),
TestProc = self(),
spawn(fun() ->
Result = grisp_connect:link_device(<<"">>),
TestProc ! Result
end),
?assertMatch(ok, wait_disconnection()),
?assertMatch(ok, wait_connection(100)),
receive
{error, timeout} -> ok
after 5000 -> error(timeout_not_reached)
end.

%--- Internal ------------------------------------------------------------------

flush() ->
Expand Down

0 comments on commit 2ccd077

Please sign in to comment.