davisp commented on a change in pull request #2585: Mango Indexes on FoundationDB URL: https://github.com/apache/couchdb/pull/2585#discussion_r393819398
########## File path: src/mango/test/eunit/mango_couch_views_indexer_test.erl ########## @@ -0,0 +1,193 @@ +% 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(mango_couch_views_indexer_test). + +-include_lib("couch/include/couch_db.hrl"). +-include_lib("couch/include/couch_eunit.hrl"). +-include_lib("mango/src/mango_cursor.hrl"). +-include_lib("mango/src/mango_idx.hrl"). +-include_lib("couch_views/include/couch_views.hrl"). +-include_lib("couch_mrview/include/couch_mrview.hrl"). +-include_lib("fabric/test/fabric2_test.hrl"). + +indexer_test_() -> + { + "Test indexing", + { + setup, + fun setup/0, + fun cleanup/1, + { + foreach, + fun foreach_setup/0, + fun foreach_teardown/1, + [ + ?TDEF_FE(index_docs), + ?TDEF_FE(index_lots_of_docs, 10), + ?TDEF_FE(index_can_recover_from_crash, 60) + ] + } + } + }. + + +setup() -> + Ctx = test_util:start_couch([ + fabric, + couch_jobs, + couch_views, + mango + ]), + Ctx. + + +cleanup(Ctx) -> + test_util:stop_couch(Ctx). + + +foreach_setup() -> + DbName = ?tempdb(), + {ok, Db} = fabric2_db:create(DbName, [{user_ctx, ?ADMIN_USER}]), + Db. + + +foreach_teardown(Db) -> + meck:unload(), + ok = fabric2_db:delete(fabric2_db:name(Db), []). + + +index_docs(Db) -> + DDoc = generate_docs(Db, 5), + wait_while_ddoc_builds(Db), + Docs = run_query(Db, DDoc), + ?assertEqual([ + [{id, <<"1">>}, {value, 1}], + [{id, <<"2">>}, {value, 2}], + [{id, <<"3">>}, {value, 3}], + [{id, <<"4">>}, {value, 4}], + [{id, <<"5">>}, {value, 5}] + ], Docs). + + +index_lots_of_docs(Db) -> + DDoc = generate_docs(Db, 150), + wait_while_ddoc_builds(Db), + Docs = run_query(Db, DDoc), + ?assertEqual(length(Docs), 150). + + +index_can_recover_from_crash(Db) -> + meck:new(mango_eval, [passthrough]), + meck:expect(mango_eval, index_doc, fun (Indexes, Doc) -> + Id = Doc#doc.id, + case Id == <<"2">> of + true -> + meck:unload(mango_indexer), + throw({fake_crash, test_jobs_restart}); + false -> + meck:passthrough([Indexes, Doc]) + end + end), + DDoc = generate_docs(Db, 3), + wait_while_ddoc_builds(Db), + Docs = run_query(Db, DDoc), + ?assertEqual([ + [{id, <<"1">>}, {value, 1}], + [{id, <<"2">>}, {value, 2}], + [{id, <<"3">>}, {value, 3}] + ], Docs). + + +wait_while_ddoc_builds(Db) -> Review comment: There's a `test_util:wait` function that you could use that would simplify this logic. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services