From 04378f242c2cd9c1d31981ba4cbe24a976ef17f7 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Tue, 14 Mar 2023 09:22:41 -0300 Subject: [PATCH] feat(ocsp_cache): give ocsp cache table an heir --- apps/emqx/src/emqx_ocsp_cache.erl | 5 +++-- apps/emqx/test/emqx_ocsp_cache_SUITE.erl | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/apps/emqx/src/emqx_ocsp_cache.erl b/apps/emqx/src/emqx_ocsp_cache.erl index 2fe3aa5d5..25c6200ae 100644 --- a/apps/emqx/src/emqx_ocsp_cache.erl +++ b/apps/emqx/src/emqx_ocsp_cache.erl @@ -149,9 +149,10 @@ inject_sni_fun(ListenerID, Conf0) -> init(_Args) -> logger:set_process_metadata(#{domain => [emqx, ocsp, cache]}), - _ = ets:new(?CACHE_TAB, [ + emqx_tables:new(?CACHE_TAB, [ named_table, - protected, + public, + {heir, whereis(emqx_kernel_sup), none}, {read_concurrency, true} ]), ?tp(ocsp_cache_init, #{}), diff --git a/apps/emqx/test/emqx_ocsp_cache_SUITE.erl b/apps/emqx/test/emqx_ocsp_cache_SUITE.erl index 890d2ecaa..e0c29e440 100644 --- a/apps/emqx/test/emqx_ocsp_cache_SUITE.erl +++ b/apps/emqx/test/emqx_ocsp_cache_SUITE.erl @@ -147,6 +147,7 @@ init_per_testcase(t_ocsp_responder_error_responses, Config) -> hocon_tconf:check_plain(emqx_schema, ConfBin, #{required => false, atom_keys => false}), emqx_config:put_listener_conf(Type, Name, [], ListenerOpts), snabbkaffe:start_trace(), + _Heir = spawn_dummy_heir(), {ok, CachePid} = emqx_ocsp_cache:start_link(), [ {cache_pid, CachePid} @@ -164,6 +165,7 @@ init_per_testcase(_TestCase, Config) -> {ok, {{"HTTP/1.0", 200, 'OK'}, [], <<"ocsp response">>}} end ), + _Heir = spawn_dummy_heir(), {ok, CachePid} = emqx_ocsp_cache:start_link(), DataDir = ?config(data_dir, Config), Type = ssl, @@ -225,6 +227,17 @@ end_per_testcase(_TestCase, Config) -> %% Helper functions %%-------------------------------------------------------------------- +%% The real cache makes `emqx_kernel_sup' the heir to its ETS table. +%% In some tests, we don't start the full supervision tree, so we need +%% this dummy process. +spawn_dummy_heir() -> + spawn_link(fun() -> + true = register(emqx_kernel_sup, self()), + receive + stop -> ok + end + end). + does_module_exist(Mod) -> case erlang:module_loaded(Mod) of true -> @@ -508,11 +521,13 @@ t_request_ocsp_response_restart_cache(Config) -> ok end, fun(Trace) -> + %% Only one fetch because the cache table was preserved by + %% its heir ("emqx_kernel_sup"). ?assertMatch( - [_, _], + [_], ?of_kind(ocsp_http_fetch_and_cache, Trace) ), - assert_http_get(2), + assert_http_get(1), ok end ).