Skip to content

Commit

Permalink
Test ping timeout and move reconnection tests in dedicated SUITE
Browse files Browse the repository at this point in the history
  • Loading branch information
ziopio committed May 27, 2024
1 parent 340acc0 commit b690a5f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 20 deletions.
18 changes: 3 additions & 15 deletions test/grisp_connect_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-import(grisp_connect_test_client, [wait_connection/0]).
-import(grisp_connect_test_client, [wait_connection/1]).
-import(grisp_connect_test_client, [wait_disconnection/0]).
-import(grisp_connect_test_client, [wait_disconnection/1]).
-import(grisp_connect_test_client, [serial_number/0]).
-import(grisp_connect_test_client, [cert_dir/0]).

Expand All @@ -30,7 +31,8 @@ init_per_suite(Config) ->
?assertEqual(ok, file:write_file(PolicyFile, <<>>)),
application:set_env(seabac, policy_file, PolicyFile),

Config2 = grisp_connect_manager:start(CertDir, Config),
Config2 = grisp_connect_manager:start(Config),
grisp_connect_manager:kraft_start(CertDir),
[{cert_dir, CertDir} | Config2].

end_per_suite(Config) ->
Expand Down Expand Up @@ -71,20 +73,6 @@ link_device_test(_) ->
?assertMatch({ok, <<"ok">>}, grisp_connect:link_device()),
?assertMatch({ok, <<"pong">>}, grisp_connect:ping()).

reconnect_on_gun_crash_test(_) ->
{state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws),
proc_lib:stop(GunPid),
?assertMatch(ok, wait_disconnection()),
?assertMatch(ok, wait_connection()).

reconnect_on_disconnection_test(Config) ->
KraftRef = ?config(kraft_instance, Config),
kraft:stop(KraftRef),
?assertMatch(ok, wait_disconnection()),
KraftRef2 = grisp_connect_manager:kraft_start(cert_dir()),
?assertMatch(ok, wait_connection(100)),
[{kraft_instance, KraftRef2} | proplists:delete(kraft_instance, Config)].

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

flush() ->
Expand Down
3 changes: 2 additions & 1 deletion test/grisp_connect_log_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ init_per_suite(Config) ->
?assertEqual(ok, file:write_file(PolicyFile, <<>>)),
application:set_env(seabac, policy_file, PolicyFile),

Config2 = grisp_connect_manager:start(CertDir, Config),
Config2 = grisp_connect_manager:start(Config),
grisp_connect_manager:kraft_start(CertDir),
grisp_connect_manager:link_device(),
[{cert_dir, CertDir} | Config2].

Expand Down
12 changes: 8 additions & 4 deletions test/grisp_connect_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

-include_lib("common_test/include/ct.hrl").

start(CertDir, Config) ->
start(Config) ->
PrivDir = ?config(priv_dir, Config),
application:set_env(mnesia, dir, PrivDir),

Expand All @@ -18,23 +18,27 @@ start(CertDir, Config) ->
application:start(mnesia),

{ok, Started2} = application:ensure_all_started(kraft),
KraftRef = kraft_start(CertDir),

{ok, Started3} = application:ensure_all_started(grisp_manager),
[{apps, Started1 ++ Started2 ++ Started3}, {kraft_instance , KraftRef} | Config].
Apps = Started1 ++ Started2 ++ Started3,
[{apps, Apps} | Config].

kraft_start(CertDir) ->
kraft_start(CertDir, #{}).

kraft_start(CertDir, OverrideOpts) ->
SslOpts = [
{verify, verify_peer},
{keyfile, filename:join(CertDir, "server.key")},
{certfile, filename:join(CertDir, "server.crt")},
{cacertfile, filename:join(CertDir, "CA.crt")}
],
KraftOpts = #{
Opts = #{
port => 3030,
ssl_opts => SslOpts,
app => grisp_manager
},
KraftOpts = mapz:deep_merge(Opts, OverrideOpts),
KraftRoutes = [
{"/grisp-connect/ws",
{ws, grisp_manager_device_api}, #{}, #{type => json_rpc}}
Expand Down
94 changes: 94 additions & 0 deletions test/grisp_connect_reconnect_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
-module(grisp_connect_reconnect_SUITE).

-behaviour(ct_suite).
-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").

-compile([export_all, nowarn_export_all]).

-import(grisp_connect_test_client, [wait_connection/0]).
-import(grisp_connect_test_client, [wait_connection/1]).
-import(grisp_connect_test_client, [wait_disconnection/0]).
-import(grisp_connect_test_client, [wait_disconnection/1]).
-import(grisp_connect_test_client, [serial_number/0]).
-import(grisp_connect_test_client, [cert_dir/0]).

%--- API -----------------------------------------------------------------------

all() ->
[
F
||
{F, 1} <- ?MODULE:module_info(exports),
lists:suffix("_test", atom_to_list(F))
].

init_per_suite(Config) ->
PrivDir = ?config(priv_dir, Config),
CertDir = cert_dir(),

PolicyFile = filename:join(PrivDir, "policies.term"),
?assertEqual(ok, file:write_file(PolicyFile, <<>>)),
application:set_env(seabac, policy_file, PolicyFile),

Config2 = grisp_connect_manager:start(Config),
[{cert_dir, CertDir} | Config2].

end_per_suite(Config) ->
grisp_connect_manager:cleanup_apps(?config(apps, Config)).

init_per_testcase(_, Config) ->
% the kraf instance links to this process
process_flag(trap_exit, true),
{ok, _} = application:ensure_all_started(kraft),
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)),
{ok, _} = application:ensure_all_started(grisp_connect),
[{kraft_instance, KraftRef} | Config].

end_per_testcase(_, Config) ->
ok = application:stop(grisp_connect),
kraft:stop(?config(kraft_instance, Config)),
ok = application:stop(kraft),
mnesia:activity(transaction, fun() ->
mnesia:delete({grisp_device, serial_number()})
end),
flush(),
Config.

%--- Tests ---------------------------------------------------------------------

reconnect_on_gun_crash_test(_) ->
?assertMatch(ok, wait_connection(100)),
{state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws),
proc_lib:stop(GunPid),
?assertMatch(ok, wait_disconnection()),
?assertMatch(ok, wait_connection()).

reconnect_on_disconnection_test(Config) ->
?assertMatch(ok, wait_connection()),
ok = kraft:stop(?config(kraft_instance, Config)),
?assertMatch(ok, wait_disconnection()),
KraftRef2 = grisp_connect_manager:kraft_start(cert_dir()),
?assertMatch(ok, wait_connection(100)),
[{kraft_instance, KraftRef2} | proplists:delete(kraft_instance, Config)].

reconnect_on_ping_timeout_test(_) ->
?assertMatch(ok, wait_connection()),
{state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws),
proc_lib:stop(GunPid),
% Now decrease ping timeout so that the WS closes after just 1 second
application:set_env(grisp_connect, ws_ping_timeout, 1000),
?assertMatch(ok, wait_disconnection()),
?assertMatch(ok, wait_connection(100)),
?assertMatch(ok, wait_disconnection()),
?assertMatch(ok, wait_connection(100)),
?assertMatch(ok, wait_disconnection()).

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

flush() ->
receive Any -> ct:pal("Flushed: ~p", [Any]), flush()
after 0 -> ok
end.
1 change: 1 addition & 0 deletions test/grisp_connect_test_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
-export([wait_connection/0]).
-export([wait_connection/1]).
-export([wait_disconnection/0]).
-export([wait_disconnection/1]).

%--- API -----------------------------------------------------------------------

Expand Down

0 comments on commit b690a5f

Please sign in to comment.