nickva commented on code in PR #5625:
URL: https://github.com/apache/couchdb/pull/5625#discussion_r2298380859


##########
src/couch/test/eunit/couch_bt_engine_cache_test.erl:
##########
@@ -0,0 +1,118 @@
+% 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(couch_bt_engine_cache_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+-define(PTERM_KEY, {couch_bt_engine_cache, caches}).
+
+couch_bt_engine_cache_test_() ->
+    {
+        foreach,
+        fun setup/0,
+        fun teardown/1,
+        [
+            ?TDEF_FE(t_created),
+            ?TDEF_FE(t_insert_and_lookup),
+            ?TDEF_FE(t_decay_and_removal_works, 10),
+            ?TDEF_FE(t_pid_cleanup_works, 10)
+        ]
+    }.
+
+setup() ->
+    Ctx = test_util:start_applications([config]),
+    couch_bt_engine_cache:create_tables(),
+    N = tuple_size(persistent_term:get(?PTERM_KEY)),
+    Pids = lists:map(
+        fun(I) ->
+            {ok, Pid} = couch_bt_engine_cache:start_link(I),
+            Pid
+        end,
+        lists:seq(1, N)
+    ),
+    {Ctx, Pids}.
+
+teardown({Ctx, [_ | _] = Pids}) ->
+    lists:foreach(
+        fun(Pid) ->
+            catch unlink(Pid),
+            exit(Pid, kill)
+        end,
+        Pids
+    ),
+    clear_tables(),
+    config:delete("bt_engine_cache", "max_size", false),
+    config:delete("bt_engine_cache", "leave_percent", false),
+    test_util:stop_applications(Ctx).
+
+clear_tables() ->
+    case persistent_term:get(?PTERM_KEY, undefined) of
+        Caches when is_tuple(Caches) ->
+            Fun = fun({cache, Tid, _}) -> ets:delete(Tid) end,
+            lists:foreach(Fun, tuple_to_list(Caches));
+        undefined ->
+            ok
+    end.
+
+t_created(_) ->
+    Caches = persistent_term:get(?PTERM_KEY),
+    N = erlang:system_info(schedulers),
+    ?assert(tuple_size(Caches) >= 1),
+    ?assertEqual(N, tuple_size(Caches)),
+    Stats = couch_bt_engine_cache:status(),
+    ?assertMatch(#{size := 0, memory := Mem} when is_integer(Mem), Stats).
+
+t_insert_and_lookup(_) ->
+    % Can only insert pids and positions
+    ?assertError(function_clause, couch_bt_engine_cache:insert(not_a_pid, 1, 
foo)),
+    ?assertError(function_clause, couch_bt_engine_cache:insert(self(), xyz, 
foo)),
+    ?assertMatch(#{size := 0}, couch_bt_engine_cache:status()),
+
+    ?assert(couch_bt_engine_cache:insert({pid, 42}, term)),
+    ?assertMatch(#{size := 1}, couch_bt_engine_cache:status()),
+
+    % Already inserted
+    ?assertNot(couch_bt_engine_cache:insert({pid, 42}, term)),
+
+    % Should be there when we look it up
+    ?assertEqual(term, couch_bt_engine_cache:lookup({pid, 42})),
+
+    % Other stuff we didn't insert should return undefined
+    ?assertEqual(undefined, couch_bt_engine_cache:lookup({pid, 43})).
+
+t_decay_and_removal_works(_) ->
+    config:set("bt_engine_cache", "leave_percent", "0", false),
+    Term = [foo, bar, baz, lists:seq(1, 100)],
+    [couch_bt_engine_cache:insert({pid, I}, Term) || I <- lists:seq(1, 10000)],
+    WaitFun = fun() ->
+        #{size := Size} = couch_bt_engine_cache:status(),
+        case Size > 0 of
+            true -> wait;
+            false -> ok
+        end
+    end,
+    test_util:wait(WaitFun, 7500),
+    ?assertMatch(#{size := 0}, couch_bt_engine_cache:status()).
+
+t_pid_cleanup_works(_) ->
+    Pid = spawn(fun() -> timer:sleep(2000) end),
+    [couch_bt_engine_cache:insert({Pid, I}, baz) || I <- lists:seq(1, 1000)],
+    WaitFun = fun() ->
+        #{size := Size} = couch_bt_engine_cache:status(),
+        case Size > 0 of
+            true -> wait;
+            false -> ok
+        end
+    end,
+    test_util:wait(WaitFun, 7500),

Review Comment:
   This one has a longer timeout set as well in `?TDEF_FE(t_pid_cleanup_works, 
10)`



-- 
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

Reply via email to