Skip to content

Commit

Permalink
Add test for mongoose_internal_databases:wait_for_mnesia
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Dec 11, 2024
1 parent 7ea781b commit c6131e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/mongoose_internal_databases.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
-export([probe/2]).

%% For tests
-export([instrumentation/0]).
-ignore_xref([instrumentation/0]).
-export([instrumentation/0, wait_for_mnesia/0]).
-ignore_xref([instrumentation/0, wait_for_mnesia/0]).

-include("mongoose_logger.hrl").

Expand All @@ -30,11 +30,14 @@ init(mnesia, #{}) ->
ok
end,
application:start(mnesia, permanent),
wait_for_tables_loop(mnesia:system_info(local_tables), 10000, 0),
wait_for_mnesia(),
mongoose_node_num_mnesia:init();
init(cets, #{}) ->
ok.

wait_for_mnesia() ->
wait_for_tables_loop(mnesia:system_info(local_tables), 10000, 0).

%% Sometimes mnesia:wait_for_tables/2 could hang on startup.
%% This function logs which tables are not ready and their status.
wait_for_tables_loop(Tables, Interval, Total) ->
Expand Down
47 changes: 47 additions & 0 deletions test/mnesia_db_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-module(mnesia_db_SUITE).
-compile([export_all, nowarn_export_all]).

-include_lib("exml/include/exml.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("mongoose.hrl").
-include("jlib.hrl").

local() ->
#{node => node()}.

all() ->
[mnesia_wait_for_tables].

init_per_suite(Config) ->
Config.

end_per_suite(Config) ->
ok.

init_per_testcase(_, C) ->
mock_mnesia(),
logger_ct_backend:start(local()),
C.

end_per_testcase(_, _) ->
meck:unload(),
logger_ct_backend:stop(local()).

mock_mnesia() ->
meck:new(mnesia, []),
meck:expect(mnesia, system_info, fun(local_tables) -> [test_table_fast, test_table_slow] end),
meck:expect(mnesia, table_info, fun(_, _) -> [] end),
meck:expect(mnesia, wait_for_tables, fun(Tables, Interval) ->
case meck:num_calls(mnesia, wait_for_tables, '_') > 5 of
true -> ok;
false -> {timeout, Tables -- [test_table_fast]}
end
end),
ok.

mnesia_wait_for_tables(_Config) ->
logger_ct_backend:capture(warning, local()),
mongoose_internal_databases:wait_for_mnesia(),
logger_ct_backend:stop_capture(local()),
FilterFun = fun(_, Msg) -> re:run(Msg, "what: mnesia_wait_for_tables_progress") /= nomatch end,
6 = length(logger_ct_backend:recv(FilterFun)).

0 comments on commit c6131e5

Please sign in to comment.