summaryrefslogtreecommitdiff
path: root/src/couch_expiring_cache/test/couch_expiring_cache_tests.erl
blob: 2e06fcc5a1e89c59c730dc1afdc18ed1beea0a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
% 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_expiring_cache_tests).


-include_lib("couch/include/couch_eunit.hrl").

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


-define(CACHE_NAME, <<"test">>).


start_link() ->
    Opts = #{
        cache_name => ?CACHE_NAME,
        period => 20,
        max_jitter => 0
    },
    couch_expiring_cache_server:start_link(?MODULE, Opts).


couch_expiring_cache_basic_test_() ->
    {
        "Test expiring cache basics",
        {
            setup,
            fun setup_couch/0, fun teardown_couch/1,
            {
                foreach,
                fun setup/0, fun teardown/1,
                [
                    fun simple_lifecycle/1
                ]
            }
        }
    }.


setup_couch() ->
    test_util:start_couch([fabric, couch_jobs]).


teardown_couch(Ctx) ->
    test_util:stop_couch(Ctx).


setup() ->
    {ok, Pid} = start_link(),
    true = unlink(Pid),
    #{pid => Pid}.


teardown(#{pid := Pid}) ->
    exit(Pid, kill).


simple_lifecycle(_) ->
    {timeout, 10, ?_test(begin
        Now = erlang:system_time(?TIME_UNIT),
        StaleTS = Now + 100,
        ExpiresTS = Now + 200,
        Name = ?CACHE_NAME,
        Key = <<"key">>,
        Val = <<"val">>,

        ?assertEqual(ok, couch_expiring_cache_fdb:clear_all(Name)),
        ?assertEqual(not_found, couch_expiring_cache:lookup(Name, Key)),
        ?assertEqual([], entries(Name)),
        ?assertEqual(ok,
            couch_expiring_cache:insert(Name, Key, Val, StaleTS, ExpiresTS)),
        ?assertEqual({fresh, Val}, couch_expiring_cache:lookup(Name, Key)),
        ok = wait_lookup(Name, Key, {stale, Val}),

        % Refresh the existing key with updated timestamps
        ?assertEqual(ok,
            couch_expiring_cache:insert(Name, Key, Val,
                StaleTS + 100, ExpiresTS + 100)),
        ?assertEqual({fresh, Val}, couch_expiring_cache:lookup(Name, Key)),
        ?assertEqual(1, length(entries(Name))),
        ok = wait_lookup(Name, Key, {stale, Val}),
        ok = wait_lookup(Name, Key, expired),
        ok = wait_lookup(Name, Key, not_found),
        ?assertEqual([], entries(Name)),
        ?assertEqual(not_found, couch_expiring_cache:lookup(Name, Key))
    end)}.


entries(Name) ->
    FarFuture = erlang:system_time(?TIME_UNIT) * 2,
    couch_expiring_cache_fdb:get_range_to(Name, FarFuture, _Limit=100).


wait_lookup(Name, Key, Expect) ->
    test_util:wait(fun() ->
        case couch_expiring_cache:lookup(Name, Key) of
            Expect -> ok;
            _ -> wait
        end
    end, _Timeout = 1000, _PollingInterval = 10).