From 1e51ffbabf05356fcd26cfc5c8d19207b09de0b7 Mon Sep 17 00:00:00 2001 From: Russell Branca Date: Mon, 4 Dec 2023 10:00:01 -0800 Subject: [PATCH 1/5] Ensure all config get's throw on no ets table --- src/config/src/config.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/config/src/config.erl b/src/config/src/config.erl index 3ece5326b6a..30c666bb236 100644 --- a/src/config/src/config.erl +++ b/src/config/src/config.erl @@ -68,8 +68,9 @@ all() -> lists:sort(gen_server:call(?MODULE, all, infinity)). get_integer(Section, Key, Default) when is_integer(Default) -> + Val = get(Section, Key, Default), try - to_integer(get(Section, Key, Default)) + to_integer(Val) catch error:badarg -> Default @@ -91,8 +92,9 @@ to_integer(Bin) when is_binary(Bin) -> list_to_integer(binary_to_list(Bin)). get_float(Section, Key, Default) when is_float(Default) -> + Val = get(Section, Key, Default), try - to_float(get(Section, Key, Default)) + to_float(Val) catch error:badarg -> Default @@ -116,8 +118,9 @@ to_float(Bin) when is_binary(Bin) -> list_to_float(binary_to_list(Bin)). get_boolean(Section, Key, Default) when is_boolean(Default) -> + Val = get(Section, Key, Default), try - to_boolean(get(Section, Key, Default)) + to_boolean(Val) catch error:badarg -> Default From 2e50baff8d1fab40e78a45faa222f8ff5fd93249 Mon Sep 17 00:00:00 2001 From: Russell Branca Date: Tue, 5 Dec 2023 12:55:58 -0800 Subject: [PATCH 2/5] Stub out config:get_* accessors in eunit suites --- src/chttpd/src/chttpd_db.erl | 3 +++ src/couch/src/test_util.erl | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl index 9b1aff54f24..14c763c4a16 100644 --- a/src/chttpd/src/chttpd_db.erl +++ b/src/chttpd/src/chttpd_db.erl @@ -2610,6 +2610,9 @@ parse_shards_opt_test_() -> setup() -> meck:expect(config, get, fun(_, _, Default) -> Default end), + meck:expect(config, get_boolean, fun(_, _, Default) -> Default end), + meck:expect(config, get_float, fun(_, _, Default) -> Default end), + meck:expect(config, get_integer, fun(_, _, Default) -> Default end), ok. teardown(_) -> diff --git a/src/couch/src/test_util.erl b/src/couch/src/test_util.erl index e53b111494e..46bea331a2f 100644 --- a/src/couch/src/test_util.erl +++ b/src/couch/src/test_util.erl @@ -362,6 +362,9 @@ mock(config) -> meck:new(config, [passthrough]), meck:expect(config, get, fun(_, _) -> undefined end), meck:expect(config, get, fun(_, _, Default) -> Default end), + meck:expect(config, get_boolean, fun(_, _, Default) -> Default end), + meck:expect(config, get_float, fun(_, _, Default) -> Default end), + meck:expect(config, get_integer, fun(_, _, Default) -> Default end), ok; mock(couch_stats) -> meck:new(couch_stats, [passthrough]), From c5b6323d5d1ba341f8506d0c7f559da7157852a0 Mon Sep 17 00:00:00 2001 From: Russell Branca Date: Tue, 5 Dec 2023 13:41:31 -0800 Subject: [PATCH 3/5] Mock config in chttpd_view tests --- src/chttpd/src/chttpd_view.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/chttpd/src/chttpd_view.erl b/src/chttpd/src/chttpd_view.erl index 1d721d18988..3f38012d3cf 100644 --- a/src/chttpd/src/chttpd_view.erl +++ b/src/chttpd/src/chttpd_view.erl @@ -197,7 +197,8 @@ setup_all() -> meck:expect(chttpd, start_delayed_json_response, 4, {ok, resp}), meck:expect(fabric, query_view, 7, {ok, #vacc{}}), meck:expect(chttpd, send_delayed_chunk, 2, {ok, resp}), - meck:expect(chttpd, end_delayed_json_response, 1, ok). + meck:expect(chttpd, end_delayed_json_response, 1, ok), + test_util:mock(config). teardown_all(_) -> meck:unload(). From 14e9359f256483ff28d2b9acec84ea98af1e9de2 Mon Sep 17 00:00:00 2001 From: Russell Branca Date: Tue, 5 Dec 2023 13:46:30 -0800 Subject: [PATCH 4/5] Export test_util:mock/1 --- src/couch/src/test_util.erl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/couch/src/test_util.erl b/src/couch/src/test_util.erl index 46bea331a2f..7ccc26e930c 100644 --- a/src/couch/src/test_util.erl +++ b/src/couch/src/test_util.erl @@ -39,6 +39,8 @@ -export([as_selector/1]). +-export([mock/1]). + -include_lib("couch/include/couch_eunit.hrl"). -include_lib("couch/include/couch_db.hrl"). -include("couch_db_int.hrl"). From 4da1245783cee95fca6bdb9ea4f89ad8a652404c Mon Sep 17 00:00:00 2001 From: Russell Branca Date: Tue, 5 Dec 2023 15:07:35 -0800 Subject: [PATCH 5/5] Switch to test_util:mock(config) --- src/chttpd/src/chttpd_db.erl | 2 +- src/couch/src/couch_db.erl | 2 +- src/couch/src/couch_query_servers.erl | 2 +- src/couch/src/test_util.erl | 2 +- src/couch_replicator/src/couch_replicator.erl | 2 +- src/couch_replicator/src/couch_replicator_auth_session.erl | 2 +- src/couch_replicator/src/couch_replicator_clustering.erl | 2 +- src/couch_replicator/src/couch_replicator_doc_processor.erl | 2 +- .../src/couch_replicator_doc_processor_worker.erl | 2 +- src/couch_replicator/src/couch_replicator_parse.erl | 2 +- src/couch_replicator/src/couch_replicator_scheduler_job.erl | 2 +- src/smoosh/src/smoosh_persist.erl | 2 +- src/smoosh/src/smoosh_server.erl | 4 ++-- src/smoosh/src/smoosh_utils.erl | 2 +- 14 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl index 14c763c4a16..0a4299ba84a 100644 --- a/src/chttpd/src/chttpd_db.erl +++ b/src/chttpd/src/chttpd_db.erl @@ -2609,7 +2609,7 @@ parse_shards_opt_test_() -> }. setup() -> - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), meck:expect(config, get_boolean, fun(_, _, Default) -> Default end), meck:expect(config, get_float, fun(_, _, Default) -> Default end), meck:expect(config, get_integer, fun(_, _, Default) -> Default end), diff --git a/src/couch/src/couch_db.erl b/src/couch/src/couch_db.erl index 2ef89ced3a6..c64c9bb45ac 100644 --- a/src/couch/src/couch_db.erl +++ b/src/couch/src/couch_db.erl @@ -2395,7 +2395,7 @@ setup_purge_seq() -> meck:new(couch_log, [passthrough]), meck:new(config, [passthrough]), meck:new(couch_db_plugin, [passthrough]), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), ok. teardown_purge_seq(_) -> diff --git a/src/couch/src/couch_query_servers.erl b/src/couch/src/couch_query_servers.erl index 151bdc805ee..442411d4e77 100644 --- a/src/couch/src/couch_query_servers.erl +++ b/src/couch/src/couch_query_servers.erl @@ -964,7 +964,7 @@ db_key_test_() -> setup() -> meck:new(config, [passthrough]), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), ok. teardown(_) -> diff --git a/src/couch/src/test_util.erl b/src/couch/src/test_util.erl index 7ccc26e930c..684ea228e1e 100644 --- a/src/couch/src/test_util.erl +++ b/src/couch/src/test_util.erl @@ -363,7 +363,7 @@ mock(Modules) when is_list(Modules) -> mock(config) -> meck:new(config, [passthrough]), meck:expect(config, get, fun(_, _) -> undefined end), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), meck:expect(config, get_boolean, fun(_, _, Default) -> Default end), meck:expect(config, get_float, fun(_, _, Default) -> Default end), meck:expect(config, get_integer, fun(_, _, Default) -> Default end), diff --git a/src/couch_replicator/src/couch_replicator.erl b/src/couch_replicator/src/couch_replicator.erl index 34c745c5d03..29eec509eeb 100644 --- a/src/couch_replicator/src/couch_replicator.erl +++ b/src/couch_replicator/src/couch_replicator.erl @@ -359,7 +359,7 @@ strip_url_creds_test_() -> { setup, fun() -> - meck:expect(config, get, fun(_, _, Default) -> Default end) + test_util:mock(config) end, fun(_) -> meck:unload() diff --git a/src/couch_replicator/src/couch_replicator_auth_session.erl b/src/couch_replicator/src/couch_replicator_auth_session.erl index 31fa0a1b11d..1897d75d17b 100644 --- a/src/couch_replicator/src/couch_replicator_auth_session.erl +++ b/src/couch_replicator/src/couch_replicator_auth_session.erl @@ -665,7 +665,7 @@ httpdb(Url) -> setup_all() -> meck:expect(couch_replicator_httpc_pool, get_worker, 1, {ok, worker}), meck:expect(couch_replicator_httpc_pool, release_worker_sync, 2, ok), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), mock_http_cookie_response("Abc"), ok. diff --git a/src/couch_replicator/src/couch_replicator_clustering.erl b/src/couch_replicator/src/couch_replicator_clustering.erl index 1b41229555a..b6ed3f7cfa0 100644 --- a/src/couch_replicator/src/couch_replicator_clustering.erl +++ b/src/couch_replicator/src/couch_replicator_clustering.erl @@ -223,7 +223,7 @@ t_unstable_callback() -> setup_all() -> meck:expect(couch_log, notice, 2, ok), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), meck:expect(config, listen_for_changes, 2, ok), meck:expect(couch_stats, update_gauge, 2, ok), meck:expect(couch_replicator_notifier, notify, 1, ok). diff --git a/src/couch_replicator/src/couch_replicator_doc_processor.erl b/src/couch_replicator/src/couch_replicator_doc_processor.erl index 4e26bdd974c..0901593a7d9 100644 --- a/src/couch_replicator/src/couch_replicator_doc_processor.erl +++ b/src/couch_replicator/src/couch_replicator_doc_processor.erl @@ -801,7 +801,7 @@ setup_all() -> meck:expect(couch_log, notice, 2, ok), meck:expect(couch_log, warning, 2, ok), meck:expect(couch_log, error, 2, ok), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), meck:expect(config, listen_for_changes, 2, ok), meck:expect(couch_replicator_clustering, owner, 2, node()), meck:expect( diff --git a/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl b/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl index b1014ffa5a1..29795ac4d16 100644 --- a/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl +++ b/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl @@ -262,7 +262,7 @@ t_ignore_if_worker_ref_does_not_match() -> setup() -> meck:expect(couch_replicator_scheduler, add_job, 1, ok), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), meck:expect(couch_server, get_uuid, 0, this_is_snek), meck:expect(couch_replicator_docs, update_failed, 3, ok), meck:expect(couch_replicator_scheduler, rep_state, 1, nil), diff --git a/src/couch_replicator/src/couch_replicator_parse.erl b/src/couch_replicator/src/couch_replicator_parse.erl index d5b366d0da5..fdc8f0f6d99 100644 --- a/src/couch_replicator/src/couch_replicator_parse.erl +++ b/src/couch_replicator/src/couch_replicator_parse.erl @@ -629,7 +629,7 @@ local_replication_endpoint_error_test_() -> }. meck_config() -> - meck:expect(config, get, fun(_, _, Default) -> Default end). + test_util:mock(config). t_error_on_local_endpoint(_) -> RepDoc = diff --git a/src/couch_replicator/src/couch_replicator_scheduler_job.erl b/src/couch_replicator/src/couch_replicator_scheduler_job.erl index dab8f8a48ed..51b6ec6b187 100644 --- a/src/couch_replicator/src/couch_replicator_scheduler_job.erl +++ b/src/couch_replicator/src/couch_replicator_scheduler_job.erl @@ -1197,7 +1197,7 @@ format_status_test_() -> }. meck_config() -> - meck:expect(config, get, fun(_, _, Default) -> Default end). + test_util:mock(config). t_scheduler_job_format_status(_) -> Source = <<"http://u:p@h1/d1">>, diff --git a/src/smoosh/src/smoosh_persist.erl b/src/smoosh/src/smoosh_persist.erl index c1519f65fa4..e960dcefe05 100644 --- a/src/smoosh/src/smoosh_persist.erl +++ b/src/smoosh/src/smoosh_persist.erl @@ -142,7 +142,7 @@ persist_unpersist_test_() -> { foreach, fun() -> - meck:expect(config, get, fun(_, _, Default) -> Default end) + test_util:mock(config) end, fun(_) -> meck:unload() diff --git a/src/smoosh/src/smoosh_server.erl b/src/smoosh/src/smoosh_server.erl index 0ae759016e9..d1043895414 100644 --- a/src/smoosh/src/smoosh_server.erl +++ b/src/smoosh/src/smoosh_server.erl @@ -569,13 +569,13 @@ setup_all() -> Ctx = setup_all_no_mock(), Pid = list_to_pid("<0.0.0>"), meck:expect(couch_index_server, get_index, 3, {ok, Pid}), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), Ctx. setup_all_no_mock() -> Ctx = test_util:start_couch([couch_log]), meck:new([config, couch_index, couch_index_server], [passthrough]), - meck:expect(config, get, fun(_, _, Default) -> Default end), + test_util:mock(config), Ctx. teardown_all(Ctx) -> diff --git a/src/smoosh/src/smoosh_utils.erl b/src/smoosh/src/smoosh_utils.erl index 9a2f59086da..14b32200893 100644 --- a/src/smoosh/src/smoosh_utils.erl +++ b/src/smoosh/src/smoosh_utils.erl @@ -162,7 +162,7 @@ smoosh_utils_test_() -> foreach, fun() -> meck:new(calendar, [passthrough, unstick]), - meck:expect(config, get, fun(_, _, Default) -> Default end) + test_util:mock(config) end, fun(_) -> meck:unload()