couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [1/3] couch-replicator commit: updated refs/heads/master to fb6826f
Date Mon, 12 Jan 2015 22:48:21 GMT
Repository: couchdb-couch-replicator
Updated Branches:
  refs/heads/master 0785198ff -> fb6826fe7


Fix test suite

COUCHDB-2540


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/commit/e6f839b3
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/tree/e6f839b3
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/diff/e6f839b3

Branch: refs/heads/master
Commit: e6f839b3acf305eb46a4d7aac580d3799609e530
Parents: 0785198
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Fri Jan 9 14:21:35 2015 -0800
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Mon Jan 12 09:42:15 2015 -0800

----------------------------------------------------------------------
 src/couch_replicator.erl                        |  37 ++++--
 test/couch_replicator_compact_tests.erl         |  50 ++++----
 test/couch_replicator_large_atts_tests.erl      | 108 ++--------------
 test/couch_replicator_many_leaves_tests.erl     |  18 +--
 test/couch_replicator_missing_stubs_tests.erl   | 124 +++----------------
 test/couch_replicator_test_helper.erl           | 107 ++++++++++++++++
 test/couch_replicator_use_checkpoints_tests.erl |   3 +-
 7 files changed, 196 insertions(+), 251 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/src/couch_replicator.erl
----------------------------------------------------------------------
diff --git a/src/couch_replicator.erl b/src/couch_replicator.erl
index e7d2ee0..5686283 100644
--- a/src/couch_replicator.erl
+++ b/src/couch_replicator.erl
@@ -25,6 +25,8 @@
 -export([init/1, terminate/2, code_change/3]).
 -export([handle_call/3, handle_cast/2, handle_info/2]).
 
+-export([details/1]).
+
 -include_lib("couch/include/couch_db.hrl").
 -include("couch_replicator_api_wrap.hrl").
 -include("couch_replicator.hrl").
@@ -210,26 +212,41 @@ cancel_replication(RepId, #user_ctx{name = Name, roles = Roles}) ->
     true ->
         cancel_replication(RepId);
     false ->
-        {BaseId, Ext} = RepId,
-        case lists:keysearch(
-            BaseId ++ Ext, 1, supervisor:which_children(couch_replicator_job_sup)) of
-        {value, {_, Pid, _, _}} when is_pid(Pid) ->
-            case (catch gen_server:call(Pid, get_details, infinity)) of
+        case find_replicator(RepId) of
+        {ok, Pid} ->
+            case details(Pid) of
             {ok, #rep{user_ctx = #user_ctx{name = Name}}} ->
                 cancel_replication(RepId);
             {ok, _} ->
                 throw({unauthorized,
                     <<"Can't cancel a replication triggered by another user">>});
-            {'EXIT', {noproc, {gen_server, call, _}}} ->
-                {error, not_found};
             Error ->
-                throw(Error)
+                Error
             end;
-        _ ->
-            {error, not_found}
+        Error ->
+            Error
         end
     end.
 
+find_replicator({BaseId, Ext} = _RepId) ->
+    case lists:keysearch(
+        BaseId ++ Ext, 1, supervisor:which_children(couch_replicator_job_sup)) of
+    {value, {_, Pid, _, _}} when is_pid(Pid) ->
+            {ok, Pid};
+    _ ->
+            {error, not_found}
+    end.
+
+details(Pid) ->
+    case (catch gen_server:call(Pid, get_details, infinity)) of
+    {ok, Rep} ->
+        {ok, Rep};
+    {'EXIT', {noproc, {gen_server, call, _}}} ->
+        {error, not_found};
+    Error ->
+        throw(Error)
+    end.
+
 init(InitArgs) ->
     {ok, InitArgs, 0}.
 

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_compact_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_compact_tests.erl b/test/couch_replicator_compact_tests.erl
index 8378567..55db96a 100644
--- a/test/couch_replicator_compact_tests.erl
+++ b/test/couch_replicator_compact_tests.erl
@@ -23,8 +23,6 @@
 -define(TIMEOUT_WRITER, 3000).
 -define(TIMEOUT_EUNIT, ?TIMEOUT div 1000 + 5).
 
--ifdef(run_broken_tests).
-
 setup() ->
     DbName = ?tempdb(),
     {ok, Db} = couch_db:create(DbName, [?ADMIN_USER]),
@@ -36,7 +34,7 @@ setup(local) ->
 setup(remote) ->
     {remote, setup()};
 setup({A, B}) ->
-    ok = test_util:start_couch(),
+    ok = test_util:start_couch([couch_replicator]),
     Source = setup(A),
     Target = setup(B),
     {Source, Target}.
@@ -50,7 +48,7 @@ teardown(DbName) ->
 teardown(_, {Source, Target}) ->
     teardown(Source),
     teardown(Target),
-
+    ok = application:stop(couch_replicator),
     ok = test_util:stop_couch().
 
 compact_test_() ->
@@ -110,6 +108,7 @@ check_active_tasks(RepPid, {BaseId, Ext} = _RepId, Src, Tgt) ->
     end,
     FullRepId = ?l2b(BaseId ++ Ext),
     Pid = ?l2b(pid_to_list(RepPid)),
+    ok = wait_for_replicator(RepPid),
     [RepTask] = couch_task_status:all(),
     ?assertEqual(Pid, couch_util:get_value(pid, RepTask)),
     ?assertEqual(FullRepId, couch_util:get_value(replication_id, RepTask)),
@@ -123,9 +122,15 @@ check_active_tasks(RepPid, {BaseId, Ext} = _RepId, Src, Tgt) ->
     ?assert(is_integer(couch_util:get_value(missing_revisions_found, RepTask))),
     ?assert(is_integer(couch_util:get_value(checkpointed_source_seq, RepTask))),
     ?assert(is_integer(couch_util:get_value(source_seq, RepTask))),
-    Progress = couch_util:get_value(progress, RepTask),
-    ?assert(is_integer(Progress)),
-    ?assert(Progress =< 100).
+    Pending = couch_util:get_value(changes_pending, RepTask),
+    ?assert(is_integer(Pending)).
+
+wait_for_replicator(Pid) ->
+    %% since replicator started asynchronously
+    %% we need to wait when it would be in couch_task_status
+    %% we query replicator:details to ensure that do_init happen
+    ?assertMatch({ok, _}, couch_replicator:details(Pid)),
+    ok.
 
 should_cancel_replication(RepId, RepPid) ->
     ?_assertNot(begin
@@ -146,12 +151,12 @@ should_populate_and_compact(RepPid, Source, Target, BatchSize, Rounds)
->
                 compact_db("source", SourceDb),
                 ?assert(is_process_alive(RepPid)),
                 ?assert(is_process_alive(SourceDb#db.main_pid)),
-                check_ref_counter("source", SourceDb),
+                wait_for_compaction("source", SourceDb),
 
                 compact_db("target", TargetDb),
                 ?assert(is_process_alive(RepPid)),
                 ?assert(is_process_alive(TargetDb#db.main_pid)),
-                check_ref_counter("target", TargetDb),
+                wait_for_compaction("target", TargetDb),
 
                 {ok, SourceDb2} = reopen_db(SourceDb),
                 {ok, TargetDb2} = reopen_db(TargetDb),
@@ -163,14 +168,14 @@ should_populate_and_compact(RepPid, Source, Target, BatchSize, Rounds)
->
                 ?assert(is_process_alive(RepPid)),
                 ?assert(is_process_alive(SourceDb2#db.main_pid)),
                 pause_writer(Writer),
-                check_ref_counter("source", SourceDb2),
+                wait_for_compaction("source", SourceDb2),
                 resume_writer(Writer),
 
                 compact_db("target", TargetDb2),
                 ?assert(is_process_alive(RepPid)),
                 ?assert(is_process_alive(TargetDb2#db.main_pid)),
                 pause_writer(Writer),
-                check_ref_counter("target", TargetDb2),
+                wait_for_compaction("target", TargetDb2),
                 resume_writer(Writer)
             end, lists:seq(1, Rounds)),
         stop_writer(Writer)
@@ -275,22 +280,17 @@ compact_db(Type, #db{name = Name}) ->
     end,
     ok = couch_db:close(Db).
 
-check_ref_counter(Type, #db{name = Name, fd_ref_counter = OldRefCounter}) ->
-    MonRef = erlang:monitor(process, OldRefCounter),
-    receive
-        {'DOWN', MonRef, process, OldRefCounter, _} ->
-            ok
-        after ?TIMEOUT ->
+wait_for_compaction(Type, Db) ->
+    case couch_db:wait_for_compaction(Db) of
+        ok ->
+            ok;
+        {error, Reason} ->
             erlang:error(
                 {assertion_failed,
                  [{module, ?MODULE}, {line, ?LINE},
-                  {reason, lists:concat(["Old ", Type,
-                                         " database ref counter didn't"
-                                         " terminate"])}]})
-    end,
-    {ok, #db{fd_ref_counter = NewRefCounter} = Db} = couch_db:open_int(Name, []),
-    ok = couch_db:close(Db),
-    ?assertNotEqual(OldRefCounter, NewRefCounter).
+                  {reason, lists:concat(["Compaction of", Type,
+                                         " database failed with: ", Reason])}]})
+    end.
 
 db_url(DbName) ->
     iolist_to_binary([
@@ -437,5 +437,3 @@ maybe_pause(Parent, Counter) ->
     after 0 ->
         ok
     end.
-
--endif.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_large_atts_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_large_atts_tests.erl b/test/couch_replicator_large_atts_tests.erl
index e8c0d1a..3b5c2e8 100644
--- a/test/couch_replicator_large_atts_tests.erl
+++ b/test/couch_replicator_large_atts_tests.erl
@@ -15,6 +15,12 @@
 -include_lib("couch/include/couch_eunit.hrl").
 -include_lib("couch/include/couch_db.hrl").
 
+-import(couch_replicator_test_helper, [
+    db_url/1,
+    replicate/2,
+    compare_dbs/2
+]).
+
 -define(ATT_SIZE_1, 2 * 1024 * 1024).
 -define(ATT_SIZE_2, round(6.6 * 1024 * 1024)).
 -define(DOCS_COUNT, 11).
@@ -33,7 +39,7 @@ setup(local) ->
 setup(remote) ->
     {remote, setup()};
 setup({A, B}) ->
-    ok = test_util:start_couch(),
+    ok = test_util:start_couch([couch_replicator]),
     config:set("attachments", "compressible_types", "text/*", false),
     Source = setup(A),
     Target = setup(B),
@@ -49,6 +55,7 @@ teardown(_, {Source, Target}) ->
     teardown(Source),
     teardown(Target),
 
+    ok = application:stop(couch_replicator),
     ok = test_util:stop_couch().
 
 large_atts_test_() ->
@@ -109,99 +116,10 @@ populate_db(DbName, DocCount) ->
     {ok, _} = couch_db:update_docs(Db, Docs, []),
     couch_db:close(Db).
 
-compare_dbs(Source, Target) ->
-    {ok, SourceDb} = couch_db:open_int(Source, []),
-    {ok, TargetDb} = couch_db:open_int(Target, []),
-
-    Fun = fun(FullDocInfo, _, Acc) ->
-        {ok, DocSource} = couch_db:open_doc(SourceDb, FullDocInfo),
-        Id = DocSource#doc.id,
-
-        {ok, DocTarget} = couch_db:open_doc(TargetDb, Id),
-        ?assertEqual(DocSource#doc.body, DocTarget#doc.body),
-
-        #doc{atts = SourceAtts} = DocSource,
-        #doc{atts = TargetAtts} = DocTarget,
-        ?assertEqual(lists:sort([N || #att{name = N} <- SourceAtts]),
-                     lists:sort([N || #att{name = N} <- TargetAtts])),
-
-        FunCompareAtts = fun(#att{name = AttName} = Att) ->
-            {ok, AttTarget} = find_att(TargetAtts, AttName),
-            SourceMd5 = att_md5(Att),
-            TargetMd5 = att_md5(AttTarget),
-            case AttName of
-                <<"att1">> ->
-                    ?assertEqual(gzip, Att#att.encoding),
-                    ?assertEqual(gzip, AttTarget#att.encoding),
-                    DecSourceMd5 = att_decoded_md5(Att),
-                    DecTargetMd5 = att_decoded_md5(AttTarget),
-                    ?assertEqual(DecSourceMd5, DecTargetMd5);
-                _ ->
-                    ?assertEqual(identity, Att#att.encoding),
-                    ?assertEqual(identity, AttTarget#att.encoding)
-            end,
-            ?assertEqual(SourceMd5, TargetMd5),
-            ?assert(is_integer(Att#att.disk_len)),
-            ?assert(is_integer(Att#att.att_len)),
-            ?assert(is_integer(AttTarget#att.disk_len)),
-            ?assert(is_integer(AttTarget#att.att_len)),
-            ?assertEqual(Att#att.disk_len, AttTarget#att.disk_len),
-            ?assertEqual(Att#att.att_len, AttTarget#att.att_len),
-            ?assertEqual(Att#att.type, AttTarget#att.type),
-            ?assertEqual(Att#att.md5, AttTarget#att.md5)
-        end,
-
-        lists:foreach(FunCompareAtts, SourceAtts),
-
-        {ok, Acc}
-    end,
-
-    {ok, _, _} = couch_db:enum_docs(SourceDb, Fun, [], []),
-    ok = couch_db:close(SourceDb),
-    ok = couch_db:close(TargetDb).
-
 att(Name, Size, Type) ->
-    #att{
-        name = Name,
-        type = Type,
-        att_len = Size,
-        data = fun(Count) -> crypto:rand_bytes(Count) end
-    }.
-
-find_att([], _Name) ->
-    nil;
-find_att([#att{name = Name} = Att | _], Name) ->
-    {ok, Att};
-find_att([_ | Rest], Name) ->
-    find_att(Rest, Name).
-
-att_md5(Att) ->
-    Md50 = couch_doc:att_foldl(
-        Att,
-        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
-        couch_util:md5_init()),
-    couch_util:md5_final(Md50).
-
-att_decoded_md5(Att) ->
-    Md50 = couch_doc:att_foldl_decode(
-        Att,
-        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
-        couch_util:md5_init()),
-    couch_util:md5_final(Md50).
-
-db_url(DbName) ->
-    iolist_to_binary([
-        "http://", config:get("httpd", "bind_address", "127.0.0.1"),
-        ":", integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
-        "/", DbName
+    couch_att:new([
+        {name, Name},
+        {type, Type},
+        {att_len, Size},
+        {data, fun(Count) -> crypto:rand_bytes(Count) end}
     ]).
-
-replicate(Source, Target) ->
-    RepObject = {[{<<"source">>, Source}, {<<"target">>, Target}]},
-    {ok, Rep} = couch_replicator_utils:parse_rep_doc(RepObject, ?ADMIN_ROLE),
-    {ok, Pid} = couch_replicator:async_replicate(Rep),
-    MonRef = erlang:monitor(process, Pid),
-    receive
-        {'DOWN', MonRef, process, Pid, _} ->
-            ok
-    end.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_many_leaves_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_many_leaves_tests.erl b/test/couch_replicator_many_leaves_tests.erl
index 04d21ac..c7b99ba 100644
--- a/test/couch_replicator_many_leaves_tests.erl
+++ b/test/couch_replicator_many_leaves_tests.erl
@@ -38,7 +38,7 @@ setup(local) ->
 setup(remote) ->
     {remote, setup()};
 setup({A, B}) ->
-    ok = test_util:start_couch(),
+    ok = test_util:start_couch([couch_replicator]),
     Source = setup(A),
     Target = setup(B),
     {Source, Target}.
@@ -52,7 +52,7 @@ teardown(DbName) ->
 teardown(_, {Source, Target}) ->
     teardown(Source),
     teardown(Target),
-
+    ok = application:stop(couch_replicator),
     ok = test_util:stop_couch().
 
 docs_with_many_leaves_test_() ->
@@ -183,13 +183,13 @@ add_attachments(SourceDb, NumAtts,  [{DocId, NumConflicts} | Rest])
->
         fun(#doc{atts = Atts, revs = {Pos, [Rev | _]}} = Doc, Acc) ->
             NewAtts = lists:foldl(fun(I, AttAcc) ->
                 AttData = crypto:rand_bytes(100),
-                NewAtt = #att{
-                    name = ?io2b(["att_", ?i2l(I), "_",
-                                  couch_doc:rev_to_str({Pos, Rev})]),
-                    type = <<"application/foobar">>,
-                    att_len = byte_size(AttData),
-                    data = AttData
-                },
+                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]
             end, [], lists:seq(1, NumAtts)),
             [Doc#doc{atts = Atts ++ NewAtts} | Acc]

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_missing_stubs_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_missing_stubs_tests.erl b/test/couch_replicator_missing_stubs_tests.erl
index 0bf2f6a..a7798c1 100644
--- a/test/couch_replicator_missing_stubs_tests.erl
+++ b/test/couch_replicator_missing_stubs_tests.erl
@@ -15,13 +15,16 @@
 -include_lib("couch/include/couch_eunit.hrl").
 -include_lib("couch/include/couch_db.hrl").
 
+-import(couch_replicator_test_helper, [
+    db_url/1,
+    replicate/2,
+    compare_dbs/2
+]).
+
 -define(REVS_LIMIT, 3).
 -define(TIMEOUT_STOP, 1000).
 -define(TIMEOUT_EUNIT, 30).
 
-
--ifdef(run_broken_tests).
-
 setup() ->
     DbName = ?tempdb(),
     {ok, Db} = couch_db:create(DbName, [?ADMIN_USER]),
@@ -33,7 +36,7 @@ setup(local) ->
 setup(remote) ->
     {remote, setup()};
 setup({A, B}) ->
-    ok = test_util:start_couch(),
+    ok = test_util:start_couch([couch_replicator]),
     Source = setup(A),
     Target = setup(B),
     {Source, Target}.
@@ -47,7 +50,7 @@ teardown(DbName) ->
 teardown(_, {Source, Target}) ->
     teardown(Source),
     teardown(Target),
-
+    ok = application:stop(couch_replicator),
     ok = test_util:stop_couch().
 
 missing_stubs_test_() ->
@@ -116,12 +119,12 @@ populate_db(DbName) ->
     Doc = #doc{
         id = <<"doc1">>,
         atts = [
-            #att{
-                name = <<"doc1_att1">>,
-                type = <<"application/foobar">>,
-                att_len = byte_size(AttData),
-                data = AttData
-            }
+            couch_att:new([
+                {name, <<"doc1_att1">>},
+                {type, <<"application/foobar">>},
+                {att_len, byte_size(AttData)},
+                {data, AttData}
+           ])
         ]
     },
     {ok, _} = couch_db:update_doc(Db, Doc, []),
@@ -153,102 +156,3 @@ db_fold_fun(FullDocInfo, {DbName, Times}) ->
         lists:seq(1, Times)),
     ok = couch_db:close(Db),
     {ok, {DbName, Times}}.
-
-compare_dbs(Source, Target) ->
-    {ok, SourceDb} = couch_db:open_int(Source, []),
-    {ok, TargetDb} = couch_db:open_int(Target, []),
-
-    Fun = fun(FullDocInfo, _, Acc) ->
-        {ok, DocSource} = couch_db:open_doc(SourceDb, FullDocInfo,
-                                            [conflicts, deleted_conflicts]),
-        Id = DocSource#doc.id,
-
-        {ok, DocTarget} = couch_db:open_doc(TargetDb, Id,
-                                            [conflicts, deleted_conflicts]),
-        ?assertEqual(DocSource#doc.body, DocTarget#doc.body),
-
-        ?assertEqual(couch_doc:to_json_obj(DocSource, []),
-                     couch_doc:to_json_obj(DocTarget, [])),
-
-        #doc{atts = SourceAtts} = DocSource,
-        #doc{atts = TargetAtts} = DocTarget,
-        ?assertEqual(lists:sort([N || #att{name = N} <- SourceAtts]),
-                     lists:sort([N || #att{name = N} <- TargetAtts])),
-
-        lists:foreach(
-            fun(#att{name = AttName} = Att) ->
-                {ok, AttTarget} = find_att(TargetAtts, AttName),
-                SourceMd5 = att_md5(Att),
-                TargetMd5 = att_md5(AttTarget),
-                case AttName of
-                    <<"att1">> ->
-                        ?assertEqual(gzip, Att#att.encoding),
-                        ?assertEqual(gzip, AttTarget#att.encoding),
-                        DecSourceMd5 = att_decoded_md5(Att),
-                        DecTargetMd5 = att_decoded_md5(AttTarget),
-                        ?assertEqual(DecSourceMd5, DecTargetMd5);
-                    _ ->
-                        ?assertEqual(identity, Att#att.encoding),
-                        ?assertEqual(identity, AttTarget#att.encoding)
-                end,
-                ?assertEqual(SourceMd5, TargetMd5),
-                ?assert(is_integer(Att#att.disk_len)),
-                ?assert(is_integer(Att#att.att_len)),
-                ?assert(is_integer(AttTarget#att.disk_len)),
-                ?assert(is_integer(AttTarget#att.att_len)),
-                ?assertEqual(Att#att.disk_len, AttTarget#att.disk_len),
-                ?assertEqual(Att#att.att_len, AttTarget#att.att_len),
-                ?assertEqual(Att#att.type, AttTarget#att.type),
-                ?assertEqual(Att#att.md5, AttTarget#att.md5)
-            end,
-            SourceAtts),
-        {ok, Acc}
-    end,
-
-    {ok, _, _} = couch_db:enum_docs(SourceDb, Fun, [], []),
-    ok = couch_db:close(SourceDb),
-    ok = couch_db:close(TargetDb).
-
-find_att([], _Name) ->
-    nil;
-find_att([#att{name = Name} = Att | _], Name) ->
-    {ok, Att};
-find_att([_ | Rest], Name) ->
-    find_att(Rest, Name).
-
-att_md5(Att) ->
-    Md50 = couch_doc:att_foldl(
-        Att,
-        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
-        couch_util:md5_init()),
-    couch_util:md5_final(Md50).
-
-att_decoded_md5(Att) ->
-    Md50 = couch_doc:att_foldl_decode(
-        Att,
-        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
-        couch_util:md5_init()),
-    couch_util:md5_final(Md50).
-
-db_url(DbName) ->
-    iolist_to_binary([
-        "http://", config:get("httpd", "bind_address", "127.0.0.1"),
-        ":", integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
-        "/", DbName
-    ]).
-
-replicate(Source, Target) ->
-    RepObject = {[
-        {<<"source">>, Source},
-        {<<"target">>, Target}
-    ]},
-    {ok, Rep} = couch_replicator_utils:parse_rep_doc(RepObject, ?ADMIN_ROLE),
-    {ok, Pid} = couch_replicator:async_replicate(Rep),
-    MonRef = erlang:monitor(process, Pid),
-    receive
-        {'DOWN', MonRef, process, Pid, _} ->
-            ok
-    end.
-
--endif.
-

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_test_helper.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_test_helper.erl b/test/couch_replicator_test_helper.erl
new file mode 100644
index 0000000..519c7d0
--- /dev/null
+++ b/test/couch_replicator_test_helper.erl
@@ -0,0 +1,107 @@
+-module(couch_replicator_test_helper).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-export([compare_dbs/2, db_url/1, replicate/2]).
+
+compare_dbs(Source, Target) ->
+    {ok, SourceDb} = couch_db:open_int(Source, []),
+    {ok, TargetDb} = couch_db:open_int(Target, []),
+
+    Fun = fun(FullDocInfo, _, Acc) ->
+        {ok, DocSource} = couch_db:open_doc(SourceDb, FullDocInfo),
+        Id = DocSource#doc.id,
+
+        {ok, DocTarget} = couch_db:open_doc(TargetDb, Id),
+        ?assertEqual(DocSource#doc.body, DocTarget#doc.body),
+
+        #doc{atts = SourceAtts} = DocSource,
+        #doc{atts = TargetAtts} = DocTarget,
+        ?assertEqual(lists:sort([couch_att:fetch(name, Att) || Att <- SourceAtts]),
+                     lists:sort([couch_att:fetch(name, Att) || Att <- TargetAtts])),
+
+        FunCompareAtts = fun(Att) ->
+            AttName = couch_att:fetch(name, Att),
+            {ok, AttTarget} = find_att(TargetAtts, AttName),
+            SourceMd5 = att_md5(Att),
+            TargetMd5 = att_md5(AttTarget),
+            case AttName of
+                <<"att1">> ->
+                    ?assertEqual(gzip, couch_att:fetch(encoding, Att)),
+                    ?assertEqual(gzip, couch_att:fetch(encoding, AttTarget)),
+                    DecSourceMd5 = att_decoded_md5(Att),
+                    DecTargetMd5 = att_decoded_md5(AttTarget),
+                    ?assertEqual(DecSourceMd5, DecTargetMd5);
+                _ ->
+                    ?assertEqual(identity, couch_att:fetch(encoding, AttTarget)),
+                    ?assertEqual(identity, couch_att:fetch(encoding, AttTarget))
+            end,
+            ?assertEqual(SourceMd5, TargetMd5),
+            ?assert(is_integer(couch_att:fetch(disk_len, Att))),
+            ?assert(is_integer(couch_att:fetch(att_len, Att))),
+            ?assert(is_integer(couch_att:fetch(disk_len, AttTarget))),
+            ?assert(is_integer(couch_att:fetch(att_len, AttTarget))),
+            ?assertEqual(couch_att:fetch(disk_len, Att),
+                         couch_att:fetch(disk_len, AttTarget)),
+            ?assertEqual(couch_att:fetch(att_len, Att),
+                         couch_att:fetch(att_len, AttTarget)),
+            ?assertEqual(couch_att:fetch(type, Att),
+                         couch_att:fetch(type, AttTarget)),
+            ?assertEqual(couch_att:fetch(md5, Att),
+                         couch_att:fetch(md5, AttTarget))
+        end,
+
+        lists:foreach(FunCompareAtts, SourceAtts),
+
+        {ok, Acc}
+    end,
+
+    {ok, _, _} = couch_db:enum_docs(SourceDb, Fun, [], []),
+    ok = couch_db:close(SourceDb),
+    ok = couch_db:close(TargetDb).
+
+find_att([], _Name) ->
+    nil;
+find_att([Att | Rest], Name) ->
+    case couch_att:fetch(name, Att) of
+        Name ->
+            {ok, Att};
+        _ ->
+            find_att(Rest, Name)
+    end.
+
+
+att_md5(Att) ->
+    Md50 = couch_att:foldl(
+        Att,
+        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
+        couch_util:md5_init()),
+    couch_util:md5_final(Md50).
+
+att_decoded_md5(Att) ->
+    Md50 = couch_att:foldl_decode(
+        Att,
+        fun(Chunk, Acc) -> couch_util:md5_update(Acc, Chunk) end,
+        couch_util:md5_init()),
+    couch_util:md5_final(Md50).
+
+db_url(DbName) ->
+    iolist_to_binary([
+        "http://", config:get("httpd", "bind_address", "127.0.0.1"),
+        ":", integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
+        "/", DbName
+    ]).
+
+replicate(Source, Target) ->
+    RepObject = {[
+        {<<"source">>, Source},
+        {<<"target">>, Target}
+    ]},
+    {ok, Rep} = couch_replicator_utils:parse_rep_doc(RepObject, ?ADMIN_ROLE),
+    {ok, Pid} = couch_replicator:async_replicate(Rep),
+    MonRef = erlang:monitor(process, Pid),
+    receive
+        {'DOWN', MonRef, process, Pid, _} ->
+            ok
+    end.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-replicator/blob/e6f839b3/test/couch_replicator_use_checkpoints_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_replicator_use_checkpoints_tests.erl b/test/couch_replicator_use_checkpoints_tests.erl
index 5b5668d..a1e5581 100644
--- a/test/couch_replicator_use_checkpoints_tests.erl
+++ b/test/couch_replicator_use_checkpoints_tests.erl
@@ -52,7 +52,7 @@ setup(local) ->
 setup(remote) ->
     {remote, setup()};
 setup({_, Fun, {A, B}}) ->
-    ok = test_util:start_couch(),
+    ok = test_util:start_couch([couch_replicator]),
     {ok, Listener} = couch_replicator_notifier:start_link(Fun),
     Source = setup(A),
     Target = setup(B),
@@ -69,6 +69,7 @@ teardown(_, {Source, Target, Listener}) ->
     teardown(Target),
 
     couch_replicator_notifier:stop(Listener),
+    ok = application:stop(couch_replicator),
     ok = test_util:stop_couch().
 
 use_checkpoints_test_() ->


Mime
View raw message