This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to branch improve-replication-tests in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit e7feef339dfd1ec16d4b3a457911c5eb9b5e05e7 Author: Nick Vatamaniuc <[email protected]> AuthorDate: Thu Oct 8 16:53:59 2026 -0400 Do less inline mocking in replicator tests Replicator tests take forever to run. Granted they do more setup but in some cases we also do silly things like create new mocks in the middle of the tests. That means a new module re-compile and reload. So, move some of them to setup/teardown functions instead. In some cases if we don't mess with the mock we can use a `setup/with/TDEF` construct instead of a `foreach/TDEF_FE` one to do a bit less work. While at change some tests that have the `?_test(begin` to use TDEF* macros and save 4 space (an indentation level) in the process. --- src/couch_replicator/src/couch_replicator.erl | 159 +++++++++------------ .../src/couch_replicator_doc_processor_worker.erl | 50 ++++--- .../src/couch_replicator_fabric.erl | 42 +++--- .../src/couch_replicator_fabric_rpc.erl | 26 ++-- .../test/eunit/couch_replicator_bulk_get_tests.erl | 15 +- .../eunit/couch_replicator_revs_diff_tests.erl | 9 +- 6 files changed, 153 insertions(+), 148 deletions(-) diff --git a/src/couch_replicator/src/couch_replicator.erl b/src/couch_replicator/src/couch_replicator.erl index ebb8f6034..1eb92342d 100644 --- a/src/couch_replicator/src/couch_replicator.erl +++ b/src/couch_replicator/src/couch_replicator.erl @@ -296,50 +296,38 @@ check_authorization(RepId, #user_ctx{name = Name} = Ctx) -> -ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). +-include_lib("couch/include/couch_eunit.hrl"). authorization_test_() -> { - foreach, - fun() -> ok end, + setup, + fun() -> meck:new(couch_replicator_scheduler, [passthrough]) end, fun(_) -> meck:unload() end, - [ - t_admin_is_always_authorized(), - t_username_must_match(), - t_replication_not_found() - ] + with([ + ?TDEF(t_admin_is_always_authorized), + ?TDEF(t_username_must_match), + ?TDEF(t_replication_not_found) + ]) }. -t_admin_is_always_authorized() -> - ?_test(begin - expect_rep_user_ctx(<<"someuser">>, <<"_admin">>), - UserCtx = #user_ctx{name = <<"adm">>, roles = [<<"_admin">>]}, - ?assertEqual(ok, check_authorization(<<"RepId">>, UserCtx)) - end). - -t_username_must_match() -> - ?_test(begin - expect_rep_user_ctx(<<"user">>, <<"somerole">>), - UserCtx1 = #user_ctx{name = <<"user">>, roles = [<<"somerole">>]}, - ?assertEqual(ok, check_authorization(<<"RepId">>, UserCtx1)), - UserCtx2 = #user_ctx{name = <<"other">>, roles = [<<"somerole">>]}, - ?assertThrow( - {unauthorized, _}, - check_authorization( - <<"RepId">>, - UserCtx2 - ) - ) - end). - -t_replication_not_found() -> - ?_test(begin - meck:expect(couch_replicator_scheduler, rep_state, 1, nil), - UserCtx1 = #user_ctx{name = <<"user">>, roles = [<<"somerole">>]}, - ?assertEqual(not_found, check_authorization(<<"RepId">>, UserCtx1)), - UserCtx2 = #user_ctx{name = <<"adm">>, roles = [<<"_admin">>]}, - ?assertEqual(not_found, check_authorization(<<"RepId">>, UserCtx2)) - end). +t_admin_is_always_authorized(_) -> + expect_rep_user_ctx(<<"someuser">>, <<"_admin">>), + UserCtx = #user_ctx{name = <<"adm">>, roles = [<<"_admin">>]}, + ?assertEqual(ok, check_authorization(<<"RepId">>, UserCtx)). + +t_username_must_match(_) -> + expect_rep_user_ctx(<<"user">>, <<"somerole">>), + UserCtx1 = #user_ctx{name = <<"user">>, roles = [<<"somerole">>]}, + ?assertEqual(ok, check_authorization(<<"RepId">>, UserCtx1)), + UserCtx2 = #user_ctx{name = <<"other">>, roles = [<<"somerole">>]}, + ?assertThrow({unauthorized, _}, check_authorization(<<"RepId">>, UserCtx2)). + +t_replication_not_found(_) -> + meck:expect(couch_replicator_scheduler, rep_state, 1, nil), + UserCtx1 = #user_ctx{name = <<"user">>, roles = [<<"somerole">>]}, + ?assertEqual(not_found, check_authorization(<<"RepId">>, UserCtx1)), + UserCtx2 = #user_ctx{name = <<"adm">>, roles = [<<"_admin">>]}, + ?assertEqual(not_found, check_authorization(<<"RepId">>, UserCtx2)). expect_rep_user_ctx(Name, Role) -> meck:expect( @@ -360,59 +348,50 @@ strip_url_creds_test_() -> fun(_) -> meck:unload() end, - [ - t_strip_http_basic_creds(), - t_strip_http_props_creds(), - t_strip_local_db_creds(), - t_strip_url_creds_errors() - ] + with([ + ?TDEF(t_strip_http_basic_creds), + ?TDEF(t_strip_http_props_creds), + ?TDEF(t_strip_local_db_creds), + ?TDEF(t_strip_url_creds_errors) + ]) }. -t_strip_local_db_creds() -> - ?_test(?assertEqual(<<"localdb">>, strip_url_creds(<<"localdb">>))). - -t_strip_http_basic_creds() -> - ?_test(begin - Url1 = <<"http://adm:pass@host/db">>, - ?assertEqual(<<"http://host/db/">>, strip_url_creds(Url1)), - Url2 = <<"https://adm:pass@host/db">>, - ?assertEqual(<<"https://host/db/">>, strip_url_creds(Url2)), - Url3 = <<"http://adm:pass@host:80/db">>, - ?assertEqual(<<"http://host:80/db/">>, strip_url_creds(Url3)), - Url4 = <<"http://adm:pass@host/db?a=b&c=d">>, - ?assertEqual( - <<"http://host/db?a=b&c=d">>, - strip_url_creds(Url4) - ) - end). - -t_strip_http_props_creds() -> - ?_test(begin - Props1 = {[{<<"url">>, <<"http://adm:pass@host/db">>}]}, - ?assertEqual(<<"http://host/db/">>, strip_url_creds(Props1)), - Props2 = - {[ - {<<"url">>, <<"http://host/db">>}, - {<<"headers">>, {[{<<"Authorization">>, <<"Basic pa55">>}]}} - ]}, - ?assertEqual(<<"http://host/db/">>, strip_url_creds(Props2)) - end). - -t_strip_url_creds_errors() -> - ?_test(begin - Bad1 = {[{<<"url">>, <<"http://adm:pass/bad">>}]}, - ?assertEqual(null, strip_url_creds(Bad1)), - Bad2 = {[{<<"garbage">>, <<"more garbage">>}]}, - ?assertEqual(null, strip_url_creds(Bad2)), - Bad3 = <<"http://a:b:c">>, - ?assertEqual(null, strip_url_creds(Bad3)), - Bad4 = <<"http://adm:pass:pass/bad">>, - ?assertEqual(null, strip_url_creds(Bad4)), - ?assertEqual(null, strip_url_creds(null)), - ?assertEqual(null, strip_url_creds(42)), - ?assertEqual(null, strip_url_creds([<<"a">>, <<"b">>])), - Bad5 = {[{<<"source_proxy">>, <<"http://adm:pass/bad">>}]}, - ?assertEqual(null, strip_url_creds(Bad5)) - end). +t_strip_local_db_creds(_) -> + ?assertEqual(<<"localdb">>, strip_url_creds(<<"localdb">>)). + +t_strip_http_basic_creds(_) -> + Url1 = <<"http://adm:pass@host/db">>, + ?assertEqual(<<"http://host/db/">>, strip_url_creds(Url1)), + Url2 = <<"https://adm:pass@host/db">>, + ?assertEqual(<<"https://host/db/">>, strip_url_creds(Url2)), + Url3 = <<"http://adm:pass@host:80/db">>, + ?assertEqual(<<"http://host:80/db/">>, strip_url_creds(Url3)), + Url4 = <<"http://adm:pass@host/db?a=b&c=d">>, + ?assertEqual(<<"http://host/db?a=b&c=d">>, strip_url_creds(Url4)). + +t_strip_http_props_creds(_) -> + Props1 = {[{<<"url">>, <<"http://adm:pass@host/db">>}]}, + ?assertEqual(<<"http://host/db/">>, strip_url_creds(Props1)), + Props2 = + {[ + {<<"url">>, <<"http://host/db">>}, + {<<"headers">>, {[{<<"Authorization">>, <<"Basic pa55">>}]}} + ]}, + ?assertEqual(<<"http://host/db/">>, strip_url_creds(Props2)). + +t_strip_url_creds_errors(_) -> + Bad1 = {[{<<"url">>, <<"http://adm:pass/bad">>}]}, + ?assertEqual(null, strip_url_creds(Bad1)), + Bad2 = {[{<<"garbage">>, <<"more garbage">>}]}, + ?assertEqual(null, strip_url_creds(Bad2)), + Bad3 = <<"http://a:b:c">>, + ?assertEqual(null, strip_url_creds(Bad3)), + Bad4 = <<"http://adm:pass:pass/bad">>, + ?assertEqual(null, strip_url_creds(Bad4)), + ?assertEqual(null, strip_url_creds(null)), + ?assertEqual(null, strip_url_creds(42)), + ?assertEqual(null, strip_url_creds([<<"a">>, <<"b">>])), + Bad5 = {[{<<"source_proxy">>, <<"http://adm:pass/bad">>}]}, + ?assertEqual(null, strip_url_creds(Bad5)). -endif. 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 8bb22ac45..f94bab01f 100644 --- a/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl +++ b/src/couch_replicator/src/couch_replicator_doc_processor_worker.erl @@ -144,18 +144,23 @@ maybe_add_job_to_scheduler({DbName, DocId}, Rep, WRef) -> doc_processor_worker_test_() -> { - foreach, - fun setup/0, - fun teardown/1, - [ - ?TDEF_FE(t_should_add_job), - ?TDEF_FE(t_already_running_same_docid), - ?TDEF_FE(t_already_running_transient), - ?TDEF_FE(t_already_running_other_db_other_doc), - ?TDEF_FE(t_spawn_worker), - ?TDEF_FE(t_ignore_if_doc_deleted), - ?TDEF_FE(t_ignore_if_worker_ref_does_not_match) - ] + setup, + fun setup_all/0, + fun teardown_all/1, + { + foreach, + fun setup/0, + fun teardown/1, + [ + ?TDEF_FE(t_should_add_job), + ?TDEF_FE(t_already_running_same_docid), + ?TDEF_FE(t_already_running_transient), + ?TDEF_FE(t_already_running_other_db_other_doc), + ?TDEF_FE(t_spawn_worker), + ?TDEF_FE(t_ignore_if_doc_deleted), + ?TDEF_FE(t_ignore_if_worker_ref_does_not_match) + ] + } }. % Replication is already running, with same doc id. Ignore change. @@ -246,18 +251,31 @@ t_ignore_if_worker_ref_does_not_match(_) -> % Test helper functions -setup() -> +setup_all() -> meck:expect(couch_replicator_scheduler, add_job, 1, ok), meck:expect(config, get, fun(_, _, Default) -> Default end), 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), - meck:expect(couch_replicator_doc_processor, get_worker_ref, 1, nil), - ok. + meck:expect(couch_replicator_doc_processor, get_worker_ref, 1, nil). -teardown(_) -> +teardown_all(_) -> meck:unload(). +setup() -> + meck:reset([ + config, + couch_server, + couch_replicator_docs, + couch_replicator_doc_processor, + couch_replicator_scheduler + ]), + meck:expect(couch_replicator_scheduler, rep_state, 1, nil), + meck:expect(couch_replicator_doc_processor, get_worker_ref, 1, nil). + +teardown(_) -> + ok. + mock_already_running(DbName, DocId) -> meck:expect( couch_replicator_scheduler, diff --git a/src/couch_replicator/src/couch_replicator_fabric.erl b/src/couch_replicator/src/couch_replicator_fabric.erl index 9f6d945be..ada1e90ee 100644 --- a/src/couch_replicator/src/couch_replicator_fabric.erl +++ b/src/couch_replicator/src/couch_replicator_fabric.erl @@ -176,21 +176,23 @@ maybe_fetch_and_filter_doc(_Id, Doc, _State) -> handle_message_test_() -> { - foreach, + setup, fun() -> meck:new(foo, [non_strict]), - meck:new(fabric_view) + meck:new(fabric_view), + meck:new(rexi, [passthrough]), + meck:new(couch_replicator, [passthrough]) end, fun(_) -> meck:unload() end, - [ - ?TDEF_FE(t_handle_message_rexi_down), - ?TDEF_FE(t_handle_message_rexi_exit), - ?TDEF_FE(t_handle_message_meta_zero), - ?TDEF_FE(t_handle_message_meta), - ?TDEF_FE(t_handle_message_row_skip), - ?TDEF_FE(t_handle_message_row), - ?TDEF_FE(t_handle_message_complete) - ] + with([ + ?TDEF(t_handle_message_rexi_down), + ?TDEF(t_handle_message_rexi_exit), + ?TDEF(t_handle_message_meta_zero), + ?TDEF(t_handle_message_meta), + ?TDEF(t_handle_message_row_skip), + ?TDEF(t_handle_message_row), + ?TDEF(t_handle_message_complete) + ]) }. t_handle_message_rexi_down(_) -> @@ -316,17 +318,17 @@ t_handle_message_complete(_) -> merge_row_test_() -> { - foreach, + setup, fun() -> ok end, fun(_) -> ok end, - [ - ?TDEF_FE(t_merge_row_record_fwd), - ?TDEF_FE(t_merge_row_record_rev), - ?TDEF_FE(t_merge_row_map_fwd), - ?TDEF_FE(t_merge_row_map_rev), - ?TDEF_FE(t_merge_row_mixed_fwd), - ?TDEF_FE(t_merge_row_mixed_rev) - ] + with([ + ?TDEF(t_merge_row_record_fwd), + ?TDEF(t_merge_row_record_rev), + ?TDEF(t_merge_row_map_fwd), + ?TDEF(t_merge_row_map_rev), + ?TDEF(t_merge_row_mixed_fwd), + ?TDEF(t_merge_row_mixed_rev) + ]) }. t_merge_row_record_fwd(_) -> diff --git a/src/couch_replicator/src/couch_replicator_fabric_rpc.erl b/src/couch_replicator/src/couch_replicator_fabric_rpc.erl index 680a758b8..ff53db14e 100644 --- a/src/couch_replicator/src/couch_replicator_fabric_rpc.erl +++ b/src/couch_replicator/src/couch_replicator_fabric_rpc.erl @@ -103,8 +103,12 @@ get_doc_state({Props}) -> docs_test_() -> { foreach, - fun() -> ok end, - fun(_) -> ok end, + fun() -> + meck:new(couch_replicator_scheduler, [passthrough]), + meck:new(couch_db, [passthrough]), + meck:new(couch_mrview, [passthrough]) + end, + fun(_) -> meck:unload() end, [ ?TDEF_FE(t_docs, 15) ] @@ -133,18 +137,20 @@ t_docs(_) -> docs_cb_test_() -> { - foreach, + setup, fun() -> meck:new(mem3), - meck:new(rexi) + meck:new(rexi), + meck:new(couch_replicator, [passthrough]), + meck:new(couch_replicator_doc_processor, [passthrough]) end, fun(_) -> meck:unload() end, - [ - ?TDEF_FE(t_docs_cb_meta, 15), - ?TDEF_FE(t_docs_cb_row_skip, 15), - ?TDEF_FE(t_docs_cb_row, 15), - ?TDEF_FE(t_docs_cb_complete, 15) - ] + with([ + ?TDEF(t_docs_cb_meta, 15), + ?TDEF(t_docs_cb_row_skip, 15), + ?TDEF(t_docs_cb_row, 15), + ?TDEF(t_docs_cb_complete, 15) + ]) }. t_docs_cb_meta(_) -> diff --git a/src/couch_replicator/test/eunit/couch_replicator_bulk_get_tests.erl b/src/couch_replicator/test/eunit/couch_replicator_bulk_get_tests.erl index f0d9569db..300adbe98 100644 --- a/src/couch_replicator/test/eunit/couch_replicator_bulk_get_tests.erl +++ b/src/couch_replicator/test/eunit/couch_replicator_bulk_get_tests.erl @@ -22,7 +22,7 @@ bulk_get_test_() -> "Use _bulk_get when replicating", { foreach, - fun couch_replicator_test_helper:test_setup/0, + fun setup/0, fun couch_replicator_test_helper:test_teardown/1, [ ?TDEF_FE(use_bulk_get), @@ -37,9 +37,13 @@ bulk_get_test_() -> } }. +setup() -> + Ctx = couch_replicator_test_helper:test_setup(), + meck:new(couch_replicator_api_wrap, [passthrough]), + Ctx. + use_bulk_get({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, true), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -51,7 +55,6 @@ use_bulk_get({_Ctx, {Source, Target}}) -> use_bulk_get_with_ddocs({_Ctx, {Source, Target}}) -> populate_db_ddocs(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, true), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -63,7 +66,6 @@ use_bulk_get_with_ddocs({_Ctx, {Source, Target}}) -> use_bulk_get_with_attachments({_Ctx, {Source, Target}}) -> populate_db_atts(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, true), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -75,7 +77,6 @@ use_bulk_get_with_attachments({_Ctx, {Source, Target}}) -> dont_use_bulk_get({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, false), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -87,7 +88,6 @@ dont_use_bulk_get({_Ctx, {Source, Target}}) -> dont_use_bulk_get_ddocs({_Ctx, {Source, Target}}) -> populate_db_ddocs(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, false), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -99,7 +99,6 @@ dont_use_bulk_get_ddocs({_Ctx, {Source, Target}}) -> dont_use_bulk_get_attachments({_Ctx, {Source, Target}}) -> populate_db_atts(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, false), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -113,7 +112,6 @@ job_enable_overrides_global_disable({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), Persist = false, config:set("replicator", "use_bulk_get", "false", Persist), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target, true), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), @@ -125,7 +123,6 @@ global_disable_works({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), Persist = false, config:set("replicator", "use_bulk_get", "false", Persist), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target), BulkGets = meck:num_calls(couch_replicator_api_wrap, bulk_get, 3), JustGets = meck:num_calls(couch_replicator_api_wrap, open_doc_revs, 6), diff --git a/src/couch_replicator/test/eunit/couch_replicator_revs_diff_tests.erl b/src/couch_replicator/test/eunit/couch_replicator_revs_diff_tests.erl index ccdfb506c..af248749a 100644 --- a/src/couch_replicator/test/eunit/couch_replicator_revs_diff_tests.erl +++ b/src/couch_replicator/test/eunit/couch_replicator_revs_diff_tests.erl @@ -23,7 +23,7 @@ revs_diff_test_() -> "Use _revs_diff when replicating", { foreach, - fun couch_replicator_test_helper:test_setup/0, + fun setup/0, fun couch_replicator_test_helper:test_teardown/1, [ ?TDEF_FE(use_revs_diff_when_most_docs_are_present, 15), @@ -32,10 +32,14 @@ revs_diff_test_() -> } }. +setup() -> + Ctx = couch_replicator_test_helper:test_setup(), + meck:new(couch_replicator_api_wrap, [passthrough]), + Ctx. + use_revs_diff_when_most_docs_are_present({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), populate_db(Target, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target), Calls = meck:num_calls(couch_replicator_api_wrap, get_missing_revs, 2), ExpectAtLeast = ?DOC_COUNT / ?BATCH_SIZE, @@ -43,7 +47,6 @@ use_revs_diff_when_most_docs_are_present({_Ctx, {Source, Target}}) -> skip_revs_diff_when_most_docs_are_missing({_Ctx, {Source, Target}}) -> populate_db(Source, ?DOC_COUNT), - meck:new(couch_replicator_api_wrap, [passthrough]), replicate(Source, Target), Calls = meck:num_calls(couch_replicator_api_wrap, get_missing_revs, 2), % This is not exact. But expect to skip at least half the revs_diffs calls.
