<commit_before>-module(z_depcache_tests).

-include_lib("zotonic.hrl").

-include_lib("eunit/include/eunit.hrl").


%% simple_test() ->
%% 	C = z_context:new(default),
%% 	z_depcache:flush(test_m_key, C),
%% 	[ test_m(C) || _N <- lists:seq(1,100) ],
%% 	ok.

%% test_m(Context) ->
%%     ?DEBUG(z_depcache:memo(fun test_f/0, test_m_key, Context)).

%% test_f() ->
%%     ?DEBUG(waiting),
%%     receive after 5000 -> y end.


get_set_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),
    z_depcache:set(test_key, 123, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),
    z_depcache:flush(test_key, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


flush_all_test() ->
    C = z_context:new(default),
    z_depcache:set(test_key, 123, C),
    z_depcache:set(test_key2, 123, C),
    z_depcache:set(test_key3, 123, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),
    ?assertEqual({ok,123}, z_depcache:get(test_key2, C)),
    ?assertEqual({ok,123}, z_depcache:get(test_key3, C)),
    z_depcache:flush(C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),
    ?assertEqual(undefined, z_depcache:get(test_key2, C)),
    ?assertEqual(undefined, z_depcache:get(test_key3, C)).


get_set_maxage_test_skip() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key and hold it for one second.
    z_depcache:set(test_key, 123, 1, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),

    %% Let the depcache time out.
    receive after 2000 -> ok end,
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


get_set_maxage0_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key and hold it for 0 seconds
    z_depcache:set(test_key, 123, 0, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


get_set_depend_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key  and hold it for one second.
    z_depcache:set(test_key, 123, 10, [test_key_dep], C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),

    %% flush the dependency; test_key should be gone as well.
    z_depcache:flush(test_key_dep, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% 3-link dependency: test_key <- test_key_dep <- test_key_dep2
    z_depcache:set(test_key, 123, 10, [test_key_dep], C),
    z_depcache:set(test_key_dep, 1234, 10, [test_key_dep2], C),

    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),
    ?assertEqual({ok,1234}, z_depcache:get(test_key_dep, C)),

    z_depcache:flush(test_key_dep2, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),
    ?assertEqual(undefined, z_depcache:get(test_key_dep, C)).


memo_test() ->
    C = z_context:new(default),
    z_depcache:flush(C),

    IncreaserFun = fun() ->
                           I = case erlang:get(incr) of
                                   undefined -> 1;
                                   Num -> Num + 1
                               end,
                           erlang:put(incr, I),
                           I
                   end,
    
    ?assertEqual(1, z_depcache:memo(IncreaserFun, test_key, C)), % uncached
    ?assertEqual(1, z_depcache:memo(IncreaserFun, test_key, C)), % cached (no increase)
    z_depcache:flush(test_key, C),
    ?assertEqual(2, z_depcache:memo(IncreaserFun, test_key, C)), % uncached again
    ?assertEqual(2, z_depcache:memo(IncreaserFun, test_key, C)). % cached again<commit_msg>Enabled maxage test for depcache; removed hierarchical dependency test (was based on wrong assumption).<commit_after>-module(z_depcache_tests).

-include_lib("zotonic.hrl").

-include_lib("eunit/include/eunit.hrl").


%% simple_test() ->
%% 	C = z_context:new(default),
%% 	z_depcache:flush(test_m_key, C),
%% 	[ test_m(C) || _N <- lists:seq(1,100) ],
%% 	ok.

%% test_m(Context) ->
%%     ?DEBUG(z_depcache:memo(fun test_f/0, test_m_key, Context)).

%% test_f() ->
%%     ?DEBUG(waiting),
%%     receive after 5000 -> y end.


get_set_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),
    z_depcache:set(test_key, 123, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),
    z_depcache:flush(test_key, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


flush_all_test() ->
    C = z_context:new(default),
    z_depcache:set(test_key, 123, C),
    z_depcache:set(test_key2, 123, C),
    z_depcache:set(test_key3, 123, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),
    ?assertEqual({ok,123}, z_depcache:get(test_key2, C)),
    ?assertEqual({ok,123}, z_depcache:get(test_key3, C)),
    z_depcache:flush(C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),
    ?assertEqual(undefined, z_depcache:get(test_key2, C)),
    ?assertEqual(undefined, z_depcache:get(test_key3, C)).


get_set_maxage_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key and hold it for one second.
    z_depcache:set(test_key, 123, 1, C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),

    %% Let the depcache time out.
    receive after 2000 -> ok end,
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


get_set_maxage0_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key and hold it for 0 seconds
    z_depcache:set(test_key, 123, 0, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


get_set_depend_test() ->
    C = z_context:new(default),
    ?assertEqual(undefined, z_depcache:get(test_key, C)),

    %% Set a key  and hold it for one second.
    z_depcache:set(test_key, 123, 10, [test_key_dep], C),
    ?assertEqual({ok,123}, z_depcache:get(test_key, C)),

    %% flush the dependency; test_key should be gone as well.
    z_depcache:flush(test_key_dep, C),
    ?assertEqual(undefined, z_depcache:get(test_key, C)).


memo_test() ->
    C = z_context:new(default),
    z_depcache:flush(C),

    IncreaserFun = fun() ->
                           I = case erlang:get(incr) of
                                   undefined -> 1;
                                   Num -> Num + 1
                               end,
                           erlang:put(incr, I),
                           I
                   end,
    
    ?assertEqual(1, z_depcache:memo(IncreaserFun, test_key, C)), % uncached
    ?assertEqual(1, z_depcache:memo(IncreaserFun, test_key, C)), % cached (no increase)
    z_depcache:flush(test_key, C),
    ?assertEqual(2, z_depcache:memo(IncreaserFun, test_key, C)), % uncached again
    ?assertEqual(2, z_depcache:memo(IncreaserFun, test_key, C)). % cached again
{'fraction_non_alphanumeric': 0.15169280375201524, 'fraction_numerical': 0.023743221456837167, 'mean_word_length': 3.4282933160285527, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068576', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2478, 'n_words': 465}