diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/etap/110-replication-changes-feed.t | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/test/etap/110-replication-changes-feed.t b/test/etap/110-replication-changes-feed.t new file mode 100644 index 00000000..467e43dd --- /dev/null +++ b/test/etap/110-replication-changes-feed.t @@ -0,0 +1,185 @@ +#!/usr/bin/env escript +%% -*- erlang -*- + +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +%% XXX: Figure out how to -include("couch_db.hrl") +-record(doc, {id= <<"">>, revs={0, []}, body={[]}, + attachments=[], deleted=false, meta=[]}). + +main(_) -> + code:add_pathz("src/couchdb"), + code:add_pathz("src/ibrowse"), + code:add_pathz("src/mochiweb"), + + etap:plan(6), + case (catch test()) of + ok -> + etap:end_tests(); + Other -> + etap:diag(io_lib:format("Test died abnormally: ~p", [Other])), + etap:bail(Other) + end, + ok. + +test() -> + couch_server:start( + ["etc/couchdb/default_dev.ini", "etc/couchdb/local_dev.ini"] + ), + ibrowse:start(), + crypto:start(), + + couch_server:delete(<<"etap-test-db">>, []), + {ok, Db} = couch_db:create(<<"etap-test-db">>, []), + + test_unchanged_db(), + test_simple_change(), + test_since_parameter(), + test_continuous_parameter(), + test_conflicts(), + test_deleted_conflicts(), + + couch_db:close(Db), + couch_server:delete(<<"etap-test-db">>, []), + ok. + +test_unchanged_db() -> + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, []), + etap:is( + couch_rep_changes_feed:next(Pid), + complete, + "changes feed for unchanged DB is automatically complete" + ). + +test_simple_change() -> + Expect = generate_change(), + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, []), + etap:is( + {couch_rep_changes_feed:next(Pid), couch_rep_changes_feed:next(Pid)}, + {Expect, complete}, + "change one document, get one row" + ). + +test_since_parameter() -> + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, + [{since, get_update_seq()}]), + etap:is( + couch_rep_changes_feed:next(Pid), + complete, + "since query-string parameter allows us to skip changes" + ). + +test_continuous_parameter() -> + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, + [{since, get_update_seq()}, {continuous, true}]), + + % make the changes_feed request before the next update + Self = self(), + spawn(fun() -> + Change = couch_rep_changes_feed:next(Pid), + Self ! {actual, Change} + end), + + Expect = generate_change(), + etap:is( + receive {actual, Actual} -> Actual end, + Expect, + "continuous query-string parameter picks up new changes" + ), + + ok = couch_rep_changes_feed:stop(Pid). + +test_conflicts() -> + Since = get_update_seq(), + Expect = generate_conflict(), + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, + [{since, Since}]), + etap:is( + {couch_rep_changes_feed:next(Pid), couch_rep_changes_feed:next(Pid)}, + {Expect, complete}, + "conflict revisions show up in feed" + ). + +test_deleted_conflicts() -> + Since = get_update_seq(), + {ExpectProps} = generate_conflict(), + + %% delete the conflict revision + Id = proplists:get_value(<<"id">>, ExpectProps), + [Win, {[{<<"rev">>, Lose}]}] = proplists:get_value(<<"changes">>, ExpectProps), + Doc = couch_doc:from_json_obj({[ + {<<"_id">>, Id}, + {<<"_rev">>, Lose}, + {<<"_deleted">>, true} + ]}), + Db = get_db(), + {ok, Rev} = couch_db:update_doc(Db, Doc, [full_commit]), + couch_db:close(Db), + + Expect = {[ + {<<"seq">>, get_update_seq()}, + {<<"id">>, Id}, + {<<"changes">>, [Win, {[{<<"rev">>, couch_doc:rev_to_str(Rev)}]}]} + ]}, + + {ok, Pid} = couch_rep_changes_feed:start({local, "etap-test-db"}, + [{since, Since}]), + etap:is( + {couch_rep_changes_feed:next(Pid), couch_rep_changes_feed:next(Pid)}, + {Expect, complete}, + "deleted conflict revisions show up in feed" + ). + +generate_change() -> + generate_change(couch_util:new_uuid()). + +generate_change(Id) -> + generate_change(Id, {[]}). + +generate_change(Id, EJson) -> + Doc = couch_doc:from_json_obj(EJson), + Db = get_db(), + {ok, Rev} = couch_db:update_doc(Db, Doc#doc{id = Id}, [full_commit]), + couch_db:close(Db), + {[ + {<<"seq">>, get_update_seq()}, + {<<"id">>, Id}, + {<<"changes">>, [{[{<<"rev">>, couch_doc:rev_to_str(Rev)}]}]} + ]}. + +generate_conflict() -> + Id = couch_util:new_uuid(), + Db = get_db(), + Doc1 = (couch_doc:from_json_obj({[<<"foo">>, <<"bar">>]}))#doc{id = Id}, + Doc2 = (couch_doc:from_json_obj({[<<"foo">>, <<"baz">>]}))#doc{id = Id}, + {ok, Rev1} = couch_db:update_doc(Db, Doc1, [full_commit]), + {ok, Rev2} = couch_db:update_doc(Db, Doc2, [full_commit, all_or_nothing]), + + %% relies on undocumented CouchDB conflict winner algo and revision sorting! + RevList = [{[{<<"rev">>, couch_doc:rev_to_str(R)}]} || R + <- lists:sort(fun(A,B) -> B<A end, [Rev1,Rev2])], + {[ + {<<"seq">>, get_update_seq()}, + {<<"id">>, Id}, + {<<"changes">>, RevList} + ]}. + +get_db() -> + {ok, Db} = couch_db:open(<<"etap-test-db">>, []), + Db. + +get_update_seq() -> + Db = get_db(), + Seq = couch_db:get_update_seq(Db), + couch_db:close(Db), + Seq. |