|
| 1 | +% Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +% use this file except in compliance with the License. You may obtain a copy of |
| 3 | +% the License at |
| 4 | +% |
| 5 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +% |
| 7 | +% Unless required by applicable law or agreed to in writing, software |
| 8 | +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +% License for the specific language governing permissions and limitations under |
| 11 | +% the License. |
| 12 | + |
| 13 | +-module(couch_auto_purge_plugin_tests). |
| 14 | + |
| 15 | +-include_lib("couch/include/couch_eunit.hrl"). |
| 16 | +-include_lib("couch/include/couch_db.hrl"). |
| 17 | + |
| 18 | +-define(PLUGIN, couch_auto_purge_plugin). |
| 19 | + |
| 20 | +couch_quickjs_scanner_plugin_test_() -> |
| 21 | + { |
| 22 | + foreach, |
| 23 | + fun setup/0, |
| 24 | + fun teardown/1, |
| 25 | + [ |
| 26 | + ?TDEF_FE(t_no_auto_purge_by_default, 10), |
| 27 | + ?TDEF_FE(t_auto_purge_after_config_ttl, 10), |
| 28 | + ?TDEF_FE(t_auto_purge_after_db_ttl, 10) |
| 29 | + ] |
| 30 | + }. |
| 31 | + |
| 32 | +setup() -> |
| 33 | + {module, _} = code:ensure_loaded(?PLUGIN), |
| 34 | + meck:new(?PLUGIN, [passthrough]), |
| 35 | + meck:new(couch_scanner_server, [passthrough]), |
| 36 | + meck:new(couch_scanner_util, [passthrough]), |
| 37 | + Ctx = test_util:start_couch([fabric, couch_scanner]), |
| 38 | + DbName = ?tempdb(), |
| 39 | + ok = fabric:create_db(DbName, [{q, "2"}, {n, "1"}]), |
| 40 | + config:set(atom_to_list(?PLUGIN), "max_batch_items", "1", false), |
| 41 | + reset_stats(), |
| 42 | + {Ctx, DbName}. |
| 43 | + |
| 44 | +teardown({Ctx, DbName}) -> |
| 45 | + config_delete_section("couch_scanner"), |
| 46 | + config_delete_section("couch_scanner_plugins"), |
| 47 | + config_delete_section(atom_to_list(?PLUGIN)), |
| 48 | + couch_scanner:reset_checkpoints(), |
| 49 | + couch_scanner:resume(), |
| 50 | + fabric:delete_db(DbName), |
| 51 | + test_util:stop_couch(Ctx), |
| 52 | + meck:unload(). |
| 53 | + |
| 54 | +t_no_auto_purge_by_default({_, DbName}) -> |
| 55 | + ok = add_doc(DbName, <<"doc1">>, #{<<"_deleted">> => true}), |
| 56 | + ?assertEqual(1, doc_del_count(DbName)), |
| 57 | + meck:reset(couch_scanner_server), |
| 58 | + meck:reset(?PLUGIN), |
| 59 | + config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false), |
| 60 | + wait_exit(10000), |
| 61 | + ?assertEqual(1, doc_del_count(DbName)), |
| 62 | + ok. |
| 63 | + |
| 64 | +t_auto_purge_after_config_ttl({_, DbName}) -> |
| 65 | + config:set(atom_to_list(?PLUGIN), "deleted_document_ttl", "-1000000", false), |
| 66 | + ok = add_doc(DbName, <<"doc1">>, #{<<"_deleted">> => true}), |
| 67 | + ?assertEqual(1, doc_del_count(DbName)), |
| 68 | + meck:reset(couch_scanner_server), |
| 69 | + meck:reset(?PLUGIN), |
| 70 | + config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false), |
| 71 | + wait_exit(10000), |
| 72 | + ?assertEqual(0, doc_del_count(DbName)), |
| 73 | + ok. |
| 74 | + |
| 75 | +t_auto_purge_after_db_ttl({_, DbName}) -> |
| 76 | + ok = fabric:set_auto_purge_props(DbName, [{<<"deleted_document_ttl">>, -1000000}]), |
| 77 | + ok = add_doc(DbName, <<"doc1">>, #{<<"_deleted">> => true}), |
| 78 | + ?assertEqual(1, doc_del_count(DbName)), |
| 79 | + meck:reset(couch_scanner_server), |
| 80 | + meck:reset(?PLUGIN), |
| 81 | + config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false), |
| 82 | + wait_exit(10000), |
| 83 | + ?assertEqual(0, doc_del_count(DbName)), |
| 84 | + ok. |
| 85 | + |
| 86 | +reset_stats() -> |
| 87 | + Counters = [ |
| 88 | + [couchdb, query_server, process_error_exits], |
| 89 | + [couchdb, query_server, process_errors], |
| 90 | + [couchdb, query_server, process_exits] |
| 91 | + ], |
| 92 | + [reset_counter(C) || C <- Counters]. |
| 93 | + |
| 94 | +reset_counter(Counter) -> |
| 95 | + case couch_stats:sample(Counter) of |
| 96 | + 0 -> |
| 97 | + ok; |
| 98 | + N when is_integer(N), N > 0 -> |
| 99 | + couch_stats:decrement_counter(Counter, N) |
| 100 | + end. |
| 101 | + |
| 102 | +config_delete_section(Section) -> |
| 103 | + [config:delete(K, V, false) || {K, V} <- config:get(Section)]. |
| 104 | + |
| 105 | +add_doc(DbName, DocId, Body) -> |
| 106 | + {ok, _} = fabric:update_doc(DbName, mkdoc(DocId, Body), [?ADMIN_CTX]), |
| 107 | + ok. |
| 108 | + |
| 109 | +mkdoc(Id, #{} = Body) -> |
| 110 | + Body1 = Body#{<<"_id">> => Id}, |
| 111 | + jiffy:decode(jiffy:encode(Body1)). |
| 112 | + |
| 113 | +wait_exit(MSec) -> |
| 114 | + meck:wait(couch_scanner_server, handle_info, [{'EXIT', '_', '_'}, '_'], MSec). |
| 115 | + |
| 116 | +doc_del_count(DbName) -> |
| 117 | + {ok, DbInfo} = fabric:get_db_info(DbName), |
| 118 | + couch_util:get_value(doc_del_count, DbInfo). |
0 commit comments