summaryrefslogtreecommitdiff
path: root/src/couch_replicator/test/eunit/couch_replicator_rate_limiter_tests.erl
blob: 034550aeceba7379adc4cd955a3c2c10afc0b2d9 (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
-module(couch_replicator_rate_limiter_tests).

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


rate_limiter_test_() ->
    {
        foreach,
        fun setup/0,
        fun teardown/1,
        [
            t_new_key(),
            t_1_failure(),
            t_2_failures_back_to_back(),
            t_2_failures(),
            t_success_threshold(),
            t_1_failure_2_successes()
        ]
    }.


t_new_key() ->
    ?_test(begin
        ?assertEqual(0, couch_replicator_rate_limiter:interval({"foo", get}))
    end).


t_1_failure() ->
    ?_test(begin
        ?assertEqual(24, couch_replicator_rate_limiter:failure({"foo", get}))
    end).


t_2_failures() ->
    ?_test(begin
        couch_replicator_rate_limiter:failure({"foo", get}),
        low_pass_filter_delay(),
        Interval = couch_replicator_rate_limiter:failure({"foo", get}),
        ?assertEqual(29, Interval)
    end).


t_2_failures_back_to_back() ->
    ?_test(begin
        couch_replicator_rate_limiter:failure({"foo", get}),
        Interval = couch_replicator_rate_limiter:failure({"foo", get}),
        ?assertEqual(24, Interval)
    end).


t_success_threshold() ->
    ?_test(begin
        Interval = couch_replicator_rate_limiter:success({"foo", get}),
        ?assertEqual(0, Interval),
        Interval = couch_replicator_rate_limiter:success({"foo", get}),
        ?assertEqual(0, Interval)
    end).


t_1_failure_2_successes() ->
    ?_test(begin
        couch_replicator_rate_limiter:failure({"foo", get}),
        low_pass_filter_delay(),
        Succ1 = couch_replicator_rate_limiter:success({"foo", get}),
        ?assertEqual(20, Succ1),
        low_pass_filter_delay(),
        Succ2 = couch_replicator_rate_limiter:success({"foo", get}),
        ?assertEqual(0, Succ2)
    end).


low_pass_filter_delay() ->
    timer:sleep(100).


setup() ->
    {ok, Pid} = couch_replicator_rate_limiter:start_link(),
    Pid.


teardown(Pid) ->
    Ref = erlang:monitor(process, Pid),
    unlink(Pid),
    exit(Pid, kill),
    receive
        {'DOWN', Ref, process, Pid, _} ->
            ok
    end,
    ok.