summaryrefslogtreecommitdiff
path: root/test/etap/110-replication-changes-feed.t
blob: 467e43dd5645b8097dbd86e943a337b0d9653a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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.