summaryrefslogtreecommitdiff
path: root/src/fabric/test/fabric2_changes_fold_tests.erl
blob: 2f638838815bb302efdccb44a4aeb52c2c57ab1b (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
% 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.

-module(fabric2_changes_fold_tests).


-include_lib("couch/include/couch_db.hrl").
-include_lib("couch/include/couch_eunit.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("fabric2_test.hrl").


-define(DOC_COUNT, 25).


next_vs_function_with_txid_test() ->
    Cases = [
        {{0, 0, 1}, {0, 0, 0}},
        {{0, 0, 2}, {0, 0, 1}},
        {{0, 1, 0}, {0, 0, 16#FFFF}},
        {{0, 2, 0}, {0, 1, 16#FFFF}},
        {{1, 0, 0}, {0, 16#FFFF, 16#FFFF}},
        {{2, 0, 0}, {1, 16#FFFF, 16#FFFF}}
    ],
    Next = fun({V, B, T}) -> fabric2_fdb:next_vs({versionstamp, V, B, T}) end,
    [?assertEqual({versionstamp, RV, RB, RT}, Next({V, B, T})) ||
        {{RV, RB, RT}, {V, B, T}} <- Cases].


next_vs_function_without_txid_test() ->
    Cases = [
        {{0, 1}, {0, 0}},
        {{0, 2}, {0, 1}},
        {{1, 0}, {0, 16#FFFF}},
        {{2, 0}, {1, 16#FFFF}}
    ],
    Next = fun({V, B}) -> fabric2_fdb:next_vs({versionstamp, V, B}) end,
    [?assertEqual({versionstamp, RV, RB}, Next({V, B})) ||
        {{RV, RB}, {V, B}} <- Cases].


changes_fold_test_() ->
    {
        "Test changes fold operations",
        {
            setup,
            fun setup_all/0,
            fun teardown_all/1,
            {
                foreach,
                fun setup/0,
                fun cleanup/1,
                [
                    ?TDEF_FE(fold_changes_basic),
                    ?TDEF_FE(fold_changes_since_now),
                    ?TDEF_FE(fold_changes_since_seq),
                    ?TDEF_FE(fold_changes_basic_rev),
                    ?TDEF_FE(fold_changes_since_now_rev),
                    ?TDEF_FE(fold_changes_since_seq_rev),
                    ?TDEF_FE(fold_changes_with_end_key),
                    ?TDEF_FE(fold_changes_basic_tx_too_old),
                    ?TDEF_FE(fold_changes_reverse_tx_too_old),
                    ?TDEF_FE(fold_changes_tx_too_old_with_single_row_emits),
                    ?TDEF_FE(fold_changes_since_seq_tx_too_old),
                    ?TDEF_FE(fold_changes_not_progressing)
                ]
            }
        }
    }.


setup_all() ->
    Ctx = test_util:start_couch([fabric]),
    meck:new(erlfdb, [passthrough]),
    meck:new(fabric2_server, [passthrough]),
    Ctx.


teardown_all(Ctx) ->
    meck:unload(),
    test_util:stop_couch(Ctx).


setup() ->
    fabric2_test_util:tx_too_old_mock_erlfdb(),
    meck:expect(fabric2_server, get_retry_limit, 0, 3),
    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
    Rows = lists:map(fun(Val) ->
        DocId = fabric2_util:uuid(),
        Doc = #doc{
            id = DocId,
            body = {[{<<"value">>, Val}]}
        },
        {ok, RevId} = fabric2_db:update_doc(Db, Doc, []),
        UpdateSeq = fabric2_db:get_update_seq(Db),
        #{
            id => DocId,
            sequence => UpdateSeq,
            deleted => false,
            rev_id => RevId
        }
    end, lists:seq(1, ?DOC_COUNT)),
    {Db, Rows}.


cleanup({Db, _DocIdRevs}) ->
    meck:reset(fabric2_server),
    meck:expect(fabric2_server, get_retry_limit, 0, meck:passthrough()),
    fabric2_test_util:tx_too_old_reset_errors(),
    ok = fabric2_db:delete(fabric2_db:name(Db), []).


fold_changes_basic({Db, DocRows}) ->
    ?assertEqual(lists:reverse(DocRows), changes(Db)).


fold_changes_since_now({Db, _}) ->
    ?assertEqual([], changes(Db, now, [])).


fold_changes_since_seq({_, []}) ->
    ok;

fold_changes_since_seq({Db, [Row | RestRows]}) ->
    #{sequence := Since} = Row,
    ?assertEqual(lists:reverse(RestRows), changes(Db, Since, [])),
    fold_changes_since_seq({Db, RestRows}).


fold_changes_basic_rev({Db, _}) ->
    ?assertEqual([], changes(Db, 0, [{dir, rev}])).


fold_changes_since_now_rev({Db, DocRows}) ->
    ?assertEqual(DocRows, changes(Db, now, [{dir, rev}])).


fold_changes_since_seq_rev({_, []}) ->
    ok;

fold_changes_since_seq_rev({Db, DocRows}) ->
    #{sequence := Since} = lists:last(DocRows),
    Opts = [{dir, rev}],
    ?assertEqual(DocRows, changes(Db, Since, Opts)),
    RestRows = lists:sublist(DocRows, length(DocRows) - 1),
    fold_changes_since_seq_rev({Db, RestRows}).


fold_changes_with_end_key({Db, DocRows}) ->
    lists:foldl(fun(DocRow, Acc) ->
        EndSeq = maps:get(sequence, DocRow),
        Changes = changes(Db, 0, [{end_key, EndSeq}]),
        NewAcc = [DocRow | Acc],
        ?assertEqual(Changes, NewAcc),
        NewAcc
    end, [], DocRows).


fold_changes_basic_tx_too_old({Db, DocRows0}) ->
    DocRows = lists:reverse(DocRows0),

    fabric2_test_util:tx_too_old_setup_errors(0, 1),
    ?assertEqual(DocRows, changes(Db)),

    fabric2_test_util:tx_too_old_setup_errors(1, 0),
    ?assertEqual(DocRows, changes(Db)),

    % Blow up in user fun but after emitting one row successfully.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, 0),
    ?assertEqual(DocRows, changes(Db)),

    % Blow up before last document
    fabric2_test_util:tx_too_old_setup_errors({?DOC_COUNT - 1, 1}, 0),
    ?assertEqual(DocRows, changes(Db)),

    % Emit one value, then blow up in user function and then blow up twice in
    % fold_range. But it is not enough to stop the iteration.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, {1, 2}),
    ?assertEqual(DocRows, changes(Db)).


fold_changes_reverse_tx_too_old({Db, DocRows}) ->
    Opts = [{dir, rev}],

    fabric2_test_util:tx_too_old_setup_errors(0, 1),
    ?assertEqual([], changes(Db, 0, Opts)),

    fabric2_test_util:tx_too_old_setup_errors(1, 0),
    ?assertEqual([], changes(Db, 0, Opts)),

    fabric2_test_util:tx_too_old_setup_errors(1, 0),
    ?assertEqual(DocRows, changes(Db, now, Opts)),

    fabric2_test_util:tx_too_old_setup_errors(1, 0),
    ?assertEqual(DocRows, changes(Db, now, Opts)),

    % Blow up in user fun but after emitting one row successfully.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, 0),
    ?assertEqual(DocRows, changes(Db, now, Opts)),

    % Blow up before last document
    fabric2_test_util:tx_too_old_setup_errors({?DOC_COUNT - 1, 1}, 0),
    ?assertEqual(DocRows, changes(Db, now, Opts)),

    % Emit value, blow up in user function, and twice in fold_range
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, {1, 2}),
    ?assertEqual(DocRows, changes(Db, now, Opts)).


fold_changes_tx_too_old_with_single_row_emits({Db, DocRows0}) ->
    % This test does a few basic operations while forcing erlfdb range fold to
    % emit a single row at a time, thus forcing it to use continuations while
    % also inducing tx errors
    Opts = [{target_bytes, 1}],
    DocRows = lists:reverse(DocRows0),

    fabric2_test_util:tx_too_old_setup_errors(0, 1),
    ?assertEqual(DocRows, changes(Db, 0, Opts)),

    fabric2_test_util:tx_too_old_setup_errors(1, 0),
    ?assertEqual(DocRows, changes(Db, 0, Opts)),

    % Blow up in user fun but after emitting one row successfully.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, 0),
    ?assertEqual(DocRows, changes(Db, 0, Opts)),

    % Blow up before last document
    fabric2_test_util:tx_too_old_setup_errors({?DOC_COUNT - 1, 1}, 0),
    ?assertEqual(DocRows, changes(Db, 0, Opts)).


fold_changes_since_seq_tx_too_old({Db, Rows}) ->
    % Blow up after after a successful emit, then twice
    % in range fold call. Also re-use already existing basic
    % fold_changes_since_seq test function.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, {1, 2}),
    fold_changes_since_seq({Db, Rows}).


fold_changes_not_progressing({Db, _}) ->
    % Fail in first fold range call.
    fabric2_test_util:tx_too_old_setup_errors(5, 0),
    ?assertError(fold_range_not_progressing, changes(Db)),

    % Fail in first user fun call.
    fabric2_test_util:tx_too_old_setup_errors(0, 5),
    ?assertError(fold_range_not_progressing, changes(Db)),

    % Blow up in last user fun call
    fabric2_test_util:tx_too_old_setup_errors({?DOC_COUNT - 1, 5}, 0),
    ?assertError(fold_range_not_progressing, changes(Db)),

    % Blow up in user function after one success.
    fabric2_test_util:tx_too_old_setup_errors({1, 5}, 0),
    ?assertError(fold_range_not_progressing, changes(Db)),

    % Emit value, blow up in user function, then keep blowing up in fold_range.
    fabric2_test_util:tx_too_old_setup_errors({1, 1}, {1, 4}),
    ?assertError(fold_range_not_progressing, changes(Db)).


fold_fun(#{} = Change, Acc) ->
    fabric2_test_util:tx_too_old_raise_in_user_fun(),
    {ok, [Change | Acc]}.


changes(Db) ->
    changes(Db, 0, []).


changes(Db, Since, Opts) ->
    {ok, Rows} = fabric2_db:fold_changes(Db, Since, fun fold_fun/2, [], Opts),
    Rows.