From c6131e5a18e841e3a44096705455c83b1d2b0ace Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Wed, 11 Dec 2024 13:31:39 +0100 Subject: [PATCH] Add test for mongoose_internal_databases:wait_for_mnesia --- src/mongoose_internal_databases.erl | 9 ++++-- test/mnesia_db_SUITE.erl | 47 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/mnesia_db_SUITE.erl diff --git a/src/mongoose_internal_databases.erl b/src/mongoose_internal_databases.erl index 8b2c55f25c..813ac39d57 100644 --- a/src/mongoose_internal_databases.erl +++ b/src/mongoose_internal_databases.erl @@ -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"). @@ -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) -> diff --git a/test/mnesia_db_SUITE.erl b/test/mnesia_db_SUITE.erl new file mode 100644 index 0000000000..21d8f9a57f --- /dev/null +++ b/test/mnesia_db_SUITE.erl @@ -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)).