4
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
% use this file except in compliance with the License. You may obtain a copy of
8
% http://www.apache.org/licenses/LICENSE-2.0
10
% Unless required by applicable law or agreed to in writing, software
11
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
% License for the specific language governing permissions and limitations under
16
%% XXX: Figure out how to -include("couch_db.hrl")
18
-record(doc, {id= <<"">>, revs={0, []}, body={[]},
19
attachments=[], deleted=false, meta=[]}).
26
{"User-Agent", "CouchDb/"++couch_server:get_version()},
27
{"Accept", "application/json"},
28
{"Accept-Encoding", "gzip"}
34
{response_format,binary},
35
{inactivity_timeout, 30000}
43
code:add_patha("src/etap"),
44
code:add_pathz("src/couchdb"),
45
code:add_pathz("src/ibrowse"),
46
code:add_pathz("src/mochiweb"),
47
code:add_pathz("src/erlang-oauth"),
50
case (catch test()) of
54
etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
61
["etc/couchdb/default_dev.ini", "etc/couchdb/local_dev.ini"]
66
couch_server:delete(<<"etap-test-source">>, []),
67
couch_server:delete(<<"etap-test-target">>, []),
70
test_all(local, local),
74
test_all(local, remote),
78
test_all(remote, local),
82
test_all(remote, remote),
87
test_all(SrcType, TgtType) ->
88
test_unchanged_db(SrcType, TgtType),
89
test_multiple_changes(SrcType, TgtType),
90
test_changes_not_missing(SrcType, TgtType).
92
test_unchanged_db(SrcType, TgtType) ->
93
{ok, Pid1} = start_changes_feed(SrcType, 0, false),
94
{ok, Pid2} = start_missing_revs(TgtType, Pid1),
96
couch_rep_missing_revs:next(Pid2),
99
"(~p, ~p) no missing revs if source is unchanged",
103
test_multiple_changes(SrcType, TgtType) ->
104
Expect = {2, [generate_change(), generate_change()]},
105
{ok, Pid1} = start_changes_feed(SrcType, 0, false),
106
{ok, Pid2} = start_missing_revs(TgtType, Pid1),
108
get_all_missing_revs(Pid2, {0, []}),
110
io_lib:format("(~p, ~p) add src docs, get missing tgt revs + high seq",
114
test_changes_not_missing(SrcType, TgtType) ->
115
%% put identical changes on source and target
116
Id = couch_util:new_uuid(),
117
{Id, _Seq, [Rev]} = Expect = generate_change(Id, {[]}, get_db(source)),
118
{Id, _, [Rev]} = generate_change(Id, {[]}, get_db(target)),
120
%% confirm that this change is not in missing revs feed
121
{ok, Pid1} = start_changes_feed(SrcType, 0, false),
122
{ok, Pid2} = start_missing_revs(TgtType, Pid1),
123
{HighSeq, AllRevs} = get_all_missing_revs(Pid2, {0, []}),
125
%% etap:none/3 has a bug, so just define it correctly here
127
lists:member(Expect, AllRevs),
130
"(~p, ~p) skip revs that already exist on target",
135
generate_change(couch_util:new_uuid()).
137
generate_change(Id) ->
138
generate_change(Id, {[]}).
140
generate_change(Id, EJson) ->
141
generate_change(Id, EJson, get_db(source)).
143
generate_change(Id, EJson, Db) ->
144
Doc = couch_doc:from_json_obj(EJson),
145
Seq = get_update_seq(),
146
{ok, Rev} = couch_db:update_doc(Db, Doc#doc{id = Id}, [full_commit]),
150
get_all_missing_revs(Pid, {HighSeq, Revs}) ->
151
case couch_rep_missing_revs:next(Pid) of
153
{HighSeq, lists:flatten(lists:reverse(Revs))};
155
get_all_missing_revs(Pid, {Seq, [More|Revs]})
159
{ok, Db} = couch_db:open(<<"etap-test-source">>, []),
162
{ok, Db} = couch_db:open(<<"etap-test-target">>, []),
167
Seq = couch_db:get_update_seq(Db),
172
{ok, DbA} = couch_db:create(<<"etap-test-source">>, []),
173
{ok, DbB} = couch_db:create(<<"etap-test-target">>, []),
176
teardown([DbA, DbB]) ->
179
couch_server:delete(<<"etap-test-source">>, []),
180
couch_server:delete(<<"etap-test-target">>, []),
183
start_changes_feed(local, Since, Continuous) ->
184
Props = [{<<"continuous">>, Continuous}],
185
couch_rep_changes_feed:start_link(self(), get_db(source), Since, Props);
186
start_changes_feed(remote, Since, Continuous) ->
187
Props = [{<<"continuous">>, Continuous}],
188
Db = #http_db{url = "http://127.0.0.1:5984/etap-test-source/"},
189
couch_rep_changes_feed:start_link(self(), Db, Since, Props).
191
start_missing_revs(local, Changes) ->
192
couch_rep_missing_revs:start_link(self(), get_db(target), Changes, []);
193
start_missing_revs(remote, Changes) ->
194
Db = #http_db{url = "http://127.0.0.1:5984/etap-test-target/"},
195
couch_rep_missing_revs:start_link(self(), Db, Changes, []).