nickva commented on code in PR #4261: URL: https://github.com/apache/couchdb/pull/4261#discussion_r1019569233
########## src/fabric/test/eunit/fabric_tests.erl: ########## @@ -12,48 +12,229 @@ -module(fabric_tests). +-include_lib("couch/include/couch_db.hrl"). -include_lib("couch/include/couch_eunit.hrl"). cleanup_index_files_test_() -> { - setup, + foreach, fun setup/0, fun teardown/1, - fun(Ctx) -> - [ - t_cleanup_index_files(), - t_cleanup_index_files_with_existing_db(Ctx), - t_cleanup_index_files_with_deleted_db(Ctx) - ] - end + [ + ?TDEF_FE(t_cleanup_index_files), + ?TDEF_FE(t_cleanup_index_files_with_existing_db), + ?TDEF_FE(t_cleanup_index_files_with_view_data), + ?TDEF_FE(t_cleanup_index_files_with_deleted_db), + ?TDEF_FE(t_cleanup_index_file_after_ddoc_update), + ?TDEF_FE(t_cleanup_index_file_after_ddoc_delete) + ] }. setup() -> Ctx = test_util:start_couch([fabric]), - % TempDb is deleted in the test "t_cleanup_index_files_with_deleted_db". - TempDb = ?tempdb(), - fabric:create_db(TempDb), - {Ctx, TempDb}. + DbName = ?tempdb(), + fabric:create_db(DbName, [{q, 1}]), + create_ddoc(DbName, <<"_design/foo">>, <<"bar">>), + {ok, _} = fabric:query_view(DbName, <<"foo">>, <<"bar">>), + create_ddoc(DbName, <<"_design/boo">>, <<"baz">>), + {ok, _} = fabric:query_view(DbName, <<"boo">>, <<"baz">>), + {Ctx, DbName}. -teardown({Ctx, _TempDb}) -> +teardown({Ctx, DbName}) -> + fabric:delete_db(DbName), test_util:stop_couch(Ctx). -t_cleanup_index_files() -> - ?_assert( - lists:all(fun(Res) -> Res =:= ok end, fabric:cleanup_index_files()) +t_cleanup_index_files(_) -> + CheckFun = fun(Res) -> Res =:= ok end, + ?assert(lists:all(CheckFun, fabric:cleanup_index_files())). + +t_cleanup_index_files_with_existing_db({_, DbName}) -> + ?assertEqual(ok, fabric:cleanup_index_files(DbName)). + +t_cleanup_index_files_with_view_data({_, DbName}) -> + Sigs = sigs(DbName), + Indices = indices(DbName), + Purges = purges(DbName), + ok = fabric:cleanup_index_files(DbName), + % We haven't inadvertently removed any active index bits + ?assertEqual(Sigs, sigs(DbName)), + ?assertEqual(Indices, indices(DbName)), + ?assertEqual(Purges, purges(DbName)). + +t_cleanup_index_files_with_deleted_db(_) -> + SomeDb = ?tempdb(), + ?assertEqual(ok, fabric:cleanup_index_files(SomeDb)). + +t_cleanup_index_file_after_ddoc_update({_, DbName}) -> + ?assertEqual( + [ + "4bcdf852098ff6b0578ddf472c320e9c.view", + "da817c3d3f7413c1a610f25635a0c521.view" + ], + indices(DbName) + ), + ?assertEqual( + [ + <<"_local/purge-mrview-4bcdf852098ff6b0578ddf472c320e9c">>, + <<"_local/purge-mrview-da817c3d3f7413c1a610f25635a0c521">> + ], + purges(DbName) + ), + + update_ddoc(DbName, <<"_design/foo">>, <<"bar1">>), + ok = fabric:cleanup_index_files(DbName), + {ok, _} = fabric:query_view(DbName, <<"foo">>, <<"bar1">>), + + % One 4bc stays, da8 should gone and 9e3 is added + ?assertEqual( + [ + "4bcdf852098ff6b0578ddf472c320e9c.view", + "9e355b0fee411b4257036b8fca56f263.view" + ], + indices(DbName) + ), + ?assertEqual( + [ + <<"_local/purge-mrview-4bcdf852098ff6b0578ddf472c320e9c">>, + <<"_local/purge-mrview-9e355b0fee411b4257036b8fca56f263">> + ], + purges(DbName) ). -t_cleanup_index_files_with_existing_db({_Ctx, TempDb}) -> - ?_assertEqual(ok, fabric:cleanup_index_files(TempDb)). +t_cleanup_index_file_after_ddoc_delete({_, DbName}) -> + ?assertEqual( + [ + "4bcdf852098ff6b0578ddf472c320e9c.view", + "da817c3d3f7413c1a610f25635a0c521.view" + ], + indices(DbName) + ), + ?assertEqual( + [ + <<"_local/purge-mrview-4bcdf852098ff6b0578ddf472c320e9c">>, + <<"_local/purge-mrview-da817c3d3f7413c1a610f25635a0c521">> + ], + purges(DbName) + ), + + delete_ddoc(DbName, <<"_design/foo">>), + ok = fabric:cleanup_index_files(DbName), + + % 4bc stays the same, da8 should be gone + ?assertEqual( + [ + "4bcdf852098ff6b0578ddf472c320e9c.view" + ], + indices(DbName) + ), + ?assertEqual( + [ + <<"_local/purge-mrview-4bcdf852098ff6b0578ddf472c320e9c">> + ], + purges(DbName) + ), + + delete_ddoc(DbName, <<"_design/boo">>), + ok = fabric:cleanup_index_files(DbName), + + ?assertEqual([], indices(DbName)), + ?assertEqual([], purges(DbName)), + + % cleaning a db with all deleted indices should still work + ok = fabric:cleanup_index_files(DbName), -t_cleanup_index_files_with_deleted_db({_Ctx, TempDb}) -> - ?_test( - begin - fabric:delete_db(TempDb, []), - ?assertError( - database_does_not_exist, - fabric:inactive_index_files(TempDb) + ?assertEqual([], indices(DbName)), + ?assertEqual([], purges(DbName)). + +shard_names(DbName) -> + [mem3:name(S) || S <- mem3:local_shards(DbName)]. + +% Sorted list of sigs +% +sigs(DbName) -> + case shard_names(DbName) of + [] -> + []; + [SomeDb | _] -> + Sigs = couch_mrview_util:get_signatures(SomeDb), + lists:sort(maps:keys(Sigs)) + end. + +% Sorted list of index files +% +indices(DbName) -> + case shard_names(DbName) of + [] -> + []; + [_ | _] = Dbs -> + AllIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs), + AsList = lists:sort( + lists:foldl( + fun(Indices, Acc) -> + maps:values(Indices) ++ Acc + end, + [], + AllIndices + ) ), - ?assertEqual(ok, fabric:cleanup_index_files(TempDb)) - end - ). + % Keep only file names and extensions. Since we use q=1, we shouldn't + % have any duplicates + [filename:basename(F) || F <- AsList] + end. + +% Sorted list of purge checkpoint doc ids +% +purges(DbName) -> + case shard_names(DbName) of + [] -> + []; + [_ | _] = Dbs -> + AllPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs), + lists:sort( + lists:foldl( + fun(Purges, Acc) -> + maps:values(Purges) ++ Acc + end, + [], + AllPurges + ) + ) + end. + +create_ddoc(DbName, DDName, ViewName) -> Review Comment: Good idea. That was a copy-paste from somewhere. I'll change it! -- 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. To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org