This is an automated email from the ASF dual-hosted git repository. vatamane pushed a commit to branch refactor-replication-tests in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 2468a8682e6d03b20cc043b3534e5e17e3b04183 Author: Nick Vatamaniuc <[email protected]> AuthorDate: Thu Aug 18 21:28:13 2022 -0400 Update couch_replicator_many_leaves_tests Use comon setup and teardown function and the TDEF_FE macros. Also, remove quite a bit of foreachx and remote boilerplate which is not needed any longer. Most of the changes however consisted in update all the db operations to use fabric instead of couch. Luckily, most of those have fabric equivalents, and fabric calls are even shorter as they don't need open, re-open and close operations. --- .../eunit/couch_replicator_many_leaves_tests.erl | 190 ++++++++------------- 1 file changed, 67 insertions(+), 123 deletions(-) diff --git a/src/couch_replicator/test/eunit/couch_replicator_many_leaves_tests.erl b/src/couch_replicator/test/eunit/couch_replicator_many_leaves_tests.erl index cdc90e2ea..aa5752650 100644 --- a/src/couch_replicator/test/eunit/couch_replicator_many_leaves_tests.erl +++ b/src/couch_replicator/test/eunit/couch_replicator_many_leaves_tests.erl @@ -14,11 +14,7 @@ -include_lib("couch/include/couch_eunit.hrl"). -include_lib("couch/include/couch_db.hrl"). - --import(couch_replicator_test_helper, [ - db_url/1, - replicate/1 -]). +-include("couch_replicator_test.hrl"). -define(DOCS_CONFLICTS, [ {<<"doc1">>, 10}, @@ -32,35 +28,15 @@ -define(i2l(I), integer_to_list(I)). -define(io2b(Io), iolist_to_binary(Io)). -setup_db() -> - DbName = ?tempdb(), - {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]), - ok = couch_db:close(Db), - DbName. - -teardown_db(DbName) -> - ok = couch_server:delete(DbName, [?ADMIN_CTX]). - -setup() -> - Ctx = test_util:start_couch([couch_replicator]), - Source = setup_db(), - Target = setup_db(), - {Ctx, {Source, Target}}. - -teardown({Ctx, {Source, Target}}) -> - teardown_db(Source), - teardown_db(Target), - ok = test_util:stop_couch(Ctx). - docs_with_many_leaves_test_() -> { "Replicate documents with many leaves", { foreach, - fun setup/0, - fun teardown/1, + fun couch_replicator_test_helper:test_setup/0, + fun couch_replicator_test_helper:test_teardown/1, [ - fun should_populate_replicate_compact/1 + ?TDEF_FE(should_populate_replicate_compact, ?TIMEOUT_EUNIT) ] } }. @@ -70,72 +46,40 @@ docs_with_many_leaves_test_winning_revs_only_test_() -> "Replicate winning revs only for documents with many leaves", { foreach, - fun setup/0, - fun teardown/1, + fun couch_replicator_test_helper:test_setup/0, + fun couch_replicator_test_helper:test_teardown/1, [ - fun should_replicate_winning_revs_only/1 + ?TDEF_FE(should_replicate_winning_revs_only, ?TIMEOUT_EUNIT) ] } }. should_populate_replicate_compact({_Ctx, {Source, Target}}) -> - {inorder, [ - should_populate_source(Source), - should_replicate(Source, Target), - should_verify_target(Source, Target, all_revs), - should_add_attachments_to_source(Source), - should_replicate(Source, Target), - should_verify_target(Source, Target, all_revs) - ]}. + populate_db(Source), + replicate(Source, Target, []), + verify_target(Source, Target, ?DOCS_CONFLICTS, all_revs), + add_attachments(Source, ?NUM_ATTS, ?DOCS_CONFLICTS), + replicate(Source, Target, []), + verify_target(Source, Target, ?DOCS_CONFLICTS, all_revs). should_replicate_winning_revs_only({_Ctx, {Source, Target}}) -> - {inorder, [ - should_populate_source(Source), - should_replicate(Source, Target, [{<<"winning_revs_only">>, true}]), - should_verify_target(Source, Target, winning_revs), - should_add_attachments_to_source(Source), - should_replicate(Source, Target, [{<<"winning_revs_only">>, true}]), - should_verify_target(Source, Target, winning_revs) - ]}. - -should_populate_source(Source) -> - {timeout, ?TIMEOUT_EUNIT, ?_test(populate_db(Source))}. - -should_replicate(Source, Target) -> - should_replicate(Source, Target, []). - -should_replicate(Source, Target, Options) -> - {timeout, ?TIMEOUT_EUNIT, - ?_test(begin - RepObj = { - [ - {<<"source">>, db_url(Source)}, - {<<"target">>, db_url(Target)} - ] ++ Options - }, - replicate(RepObj) - end)}. - -should_verify_target(Source, Target, Mode) -> - {timeout, ?TIMEOUT_EUNIT, - ?_test(begin - {ok, SourceDb} = couch_db:open_int(Source, []), - {ok, TargetDb} = couch_db:open_int(Target, []), - verify_target(SourceDb, TargetDb, ?DOCS_CONFLICTS, Mode), - ok = couch_db:close(SourceDb), - ok = couch_db:close(TargetDb) - end)}. - -should_add_attachments_to_source(Source) -> - {timeout, ?TIMEOUT_EUNIT, - ?_test(begin - {ok, SourceDb} = couch_db:open_int(Source, [?ADMIN_CTX]), - add_attachments(SourceDb, ?NUM_ATTS, ?DOCS_CONFLICTS), - ok = couch_db:close(SourceDb) - end)}. + populate_db(Source), + replicate(Source, Target, [{<<"winning_revs_only">>, true}]), + verify_target(Source, Target, ?DOCS_CONFLICTS, winning_revs), + add_attachments(Source, ?NUM_ATTS, ?DOCS_CONFLICTS), + replicate(Source, Target, [{<<"winning_revs_only">>, true}]), + verify_target(Source, Target, ?DOCS_CONFLICTS, winning_revs). + +replicate(Source, Target, Options) -> + RepObj = { + [ + {<<"source">>, db_url(Source)}, + {<<"target">>, db_url(Target)} + ] ++ Options + }, + couch_replicator_test_helper:replicate(RepObj). populate_db(DbName) -> - {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]), lists:foreach( fun({DocId, NumConflicts}) -> Value = <<"0">>, @@ -143,22 +87,20 @@ populate_db(DbName) -> id = DocId, body = {[{<<"value">>, Value}]} }, - {ok, {Pos, Rev}} = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]), + {ok, {Pos, Rev}} = fabric:update_doc(DbName, Doc, [?ADMIN_CTX]), % Update first initial doc rev twice to ensure it's always a winner - {ok, Db2} = couch_db:reopen(Db), Doc1 = Doc#doc{revs = {Pos, [Rev]}}, - {ok, _} = couch_db:update_doc(Db2, Doc1, [?ADMIN_CTX]), - {ok, _} = add_doc_siblings(Db, DocId, NumConflicts) + {ok, _} = fabric:update_doc(DbName, Doc1, [?ADMIN_CTX]), + {ok, _} = add_doc_siblings(DbName, DocId, NumConflicts) end, ?DOCS_CONFLICTS - ), - couch_db:close(Db). + ). add_doc_siblings(Db, DocId, NumLeaves) when NumLeaves > 0 -> add_doc_siblings(Db, DocId, NumLeaves, [], []). add_doc_siblings(Db, _DocId, 0, AccDocs, AccRevs) -> - {ok, []} = couch_db:update_docs(Db, AccDocs, [], ?REPLICATED_CHANGES), + {ok, []} = fabric:update_docs(Db, AccDocs, [?REPLICATED_CHANGES, ?ADMIN_CTX]), {ok, AccRevs}; add_doc_siblings(Db, DocId, NumLeaves, AccDocs, AccRevs) -> Value = ?l2b(?i2l(NumLeaves)), @@ -179,18 +121,8 @@ add_doc_siblings(Db, DocId, NumLeaves, AccDocs, AccRevs) -> verify_target(_SourceDb, _TargetDb, [], _Mode) -> ok; verify_target(SourceDb, TargetDb, [{DocId, NumConflicts} | Rest], all_revs) -> - {ok, SourceLookups} = couch_db:open_doc_revs( - SourceDb, - DocId, - all, - [conflicts, deleted_conflicts] - ), - {ok, TargetLookups} = couch_db:open_doc_revs( - TargetDb, - DocId, - all, - [conflicts, deleted_conflicts] - ), + SourceLookups = open_revs_conflicts(SourceDb, DocId), + TargetLookups = open_revs_conflicts(TargetDb, DocId), SourceDocs = [Doc || {ok, Doc} <- SourceLookups], TargetDocs = [Doc || {ok, Doc} <- TargetLookups], Total = NumConflicts + 1, @@ -205,14 +137,13 @@ verify_target(SourceDb, TargetDb, [{DocId, NumConflicts} | Rest], all_revs) -> ), verify_target(SourceDb, TargetDb, Rest, all_revs); verify_target(SourceDb, TargetDb, [{DocId, _NumConflicts} | Rest], winning_revs) -> - {ok, SourceWinner} = couch_db:open_doc(SourceDb, DocId), - {ok, TargetWinner} = couch_db:open_doc(TargetDb, DocId), + SourceWinner = open_doc(SourceDb, DocId), + TargetWinner = open_doc(TargetDb, DocId), SourceWinnerJson = couch_doc:to_json_obj(SourceWinner, [attachments]), TargetWinnerJson = couch_doc:to_json_obj(TargetWinner, [attachments]), % Source winner is the same as the target winner ?assertEqual(SourceWinnerJson, TargetWinnerJson), - Opts = [conflicts, deleted_conflicts], - {ok, TargetAll} = couch_db:open_doc_revs(TargetDb, DocId, all, Opts), + TargetAll = open_revs_conflicts(TargetDb, DocId), % There is only one version on the target ?assert(length(TargetAll) == 1), verify_target(SourceDb, TargetDb, Rest, winning_revs). @@ -220,7 +151,7 @@ verify_target(SourceDb, TargetDb, [{DocId, _NumConflicts} | Rest], winning_revs) add_attachments(_SourceDb, _NumAtts, []) -> ok; add_attachments(SourceDb, NumAtts, [{DocId, NumConflicts} | Rest]) -> - {ok, SourceLookups} = couch_db:open_doc_revs(SourceDb, DocId, all, []), + SourceLookups = open_revs(SourceDb, DocId, []), SourceDocs = [Doc || {ok, Doc} <- SourceLookups], Total = NumConflicts + 1, ?assertEqual(Total, length(SourceDocs)), @@ -228,20 +159,7 @@ add_attachments(SourceDb, NumAtts, [{DocId, NumConflicts} | Rest]) -> fun(#doc{atts = Atts, revs = {Pos, [Rev | _]}} = Doc, Acc) -> NewAtts = lists:foldl( fun(I, AttAcc) -> - AttData = crypto:strong_rand_bytes(100), - NewAtt = couch_att:new([ - {name, - ?io2b([ - "att_", - ?i2l(I), - "_", - couch_doc:rev_to_str({Pos, Rev}) - ])}, - {type, <<"application/foobar">>}, - {att_len, byte_size(AttData)}, - {data, AttData} - ]), - [NewAtt | AttAcc] + [make_att(I, Pos, Rev, 100) | AttAcc] end, [], lists:seq(1, NumAtts) @@ -251,7 +169,33 @@ add_attachments(SourceDb, NumAtts, [{DocId, NumConflicts} | Rest]) -> [], SourceDocs ), - {ok, UpdateResults} = couch_db:update_docs(SourceDb, NewDocs, []), + {ok, UpdateResults} = fabric:update_docs(SourceDb, NewDocs, [?ADMIN_CTX]), NewRevs = [R || {ok, R} <- UpdateResults], ?assertEqual(length(NewDocs), length(NewRevs)), add_attachments(SourceDb, NumAtts, Rest). + +make_att(Id, Pos, Rev, Size) -> + AttData = crypto:strong_rand_bytes(Size), + RevStr = couch_doc:rev_to_str({Pos, Rev}), + couch_att:new([ + {name, ?io2b(["att_", ?i2l(Id), "_", RevStr])}, + {type, <<"application/foobar">>}, + {att_len, byte_size(AttData)}, + {data, AttData} + ]). + +db_url(DbName) -> + couch_replicator_test_helper:cluster_db_url(DbName). + +open_revs_conflicts(DbName, Id) -> + Opts = [conflicts, deleted_conflicts], + {ok, Lookups} = fabric:open_revs(DbName, Id, all, Opts), + Lookups. + +open_revs(DbName, Id, Opts) -> + {ok, Lookups} = fabric:open_revs(DbName, Id, all, Opts), + Lookups. + +open_doc(DbName, Id) -> + {ok, Doc} = fabric:open_doc(DbName, Id, [?ADMIN_CTX]), + Doc.
