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 fd69b81c630811e2a50a2e58e288810aeee0dfab
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.
---
 src/couch_replicator/src/couch_replicator.erl      |  4 +-
 .../src/couch_replicator_doc_processor_worker.erl  | 50 +++++++++++++++-------
 .../src/couch_replicator_fabric.erl                | 24 ++++++-----
 .../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, 77 insertions(+), 51 deletions(-)

diff --git a/src/couch_replicator/src/couch_replicator.erl 
b/src/couch_replicator/src/couch_replicator.erl
index ebb8f6034..7ce27f697 100644
--- a/src/couch_replicator/src/couch_replicator.erl
+++ b/src/couch_replicator/src/couch_replicator.erl
@@ -300,8 +300,8 @@ check_authorization(RepId, #user_ctx{name = Name} = Ctx) ->
 
 authorization_test_() ->
     {
-        foreach,
-        fun() -> ok end,
+        setup,
+        fun() -> meck:new(couch_replicator_scheduler, [passthrough]) end,
         fun(_) -> meck:unload() end,
         [
             t_admin_is_always_authorized(),
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..cc0873368 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(_) ->
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.

Reply via email to