summaryrefslogtreecommitdiff
path: root/src/smoosh/test/smoosh_tests.erl
blob: adabc8c49c10daf956119081d8617558d4cad932 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-module(smoosh_tests).

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

-define(KILOBYTE, binary:copy(<<"x">>, 1024)).

%% ==========
%% Setup
%% ----------

setup(ChannelType) ->
    DbName = ?tempdb(),
    {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
    couch_db:close(Db),
    {ok, ChannelPid} = smoosh_server:get_channel(ChannelType),
    smoosh_channel:flush(ChannelPid),
    ok = config:set(config_section(ChannelType), "min_size", "200000", false),
    DbName.

teardown(ChannelType, DbName) ->
    ok = couch_server:delete(DbName, [?ADMIN_CTX]),
    ok = config:delete(config_section(DbName), "min_size", false),
    {ok, ChannelPid} = smoosh_server:get_channel(ChannelType),
    smoosh_channel:flush(ChannelPid),
    ok.

config_section(ChannelType) ->
    "smoosh." ++ ChannelType.

%% ==========
%% Tests
%% ----------

smoosh_test_() ->
    {
        "Testing smoosh",
        {
            setup,
            fun() -> test_util:start_couch([smoosh]) end,
            fun test_util:stop/1,
            [
                channels_tests(),
                persistence_tests()
            ]
        }
    }.

persistence_tests() ->
    Tests = [
        fun should_persist_queue/2
    ],
    {
        "Should persist queue state",
        [
            make_test_case("ratio_dbs", Tests)
        ]
    }.

channels_tests() ->
    Tests = [
        fun should_enqueue/2
    ],
    {
        "Various channels tests",
        [
            make_test_case("ratio_dbs", Tests)
        ]
    }.

make_test_case(Type, Funs) ->
    {foreachx, fun setup/1, fun teardown/2, [{Type, Fun} || Fun <- Funs]}.

should_enqueue(ChannelType, DbName) ->
    ?_test(begin
        ok = grow_db_file(DbName, 300),
        ok = wait_enqueue(ChannelType, DbName),
        ?assert(is_enqueued(ChannelType, DbName)),
        ok
    end).

should_persist_queue(ChannelType, DbName) ->
    ?_test(begin
        {ok, ChannelPid} = smoosh_server:get_channel(ChannelType),
        ok = grow_db_file(DbName, 300),
        ok = wait_enqueue(ChannelType, DbName),
        ok = smoosh_channel:persist(ChannelPid),
        Q0 = channel_queue(ChannelType),
        ok = application:stop(smoosh),
        ok = application:start(smoosh),
        Q1 = channel_queue(ChannelType),
        ?assertEqual(Q0, Q1),
        ok
    end).

grow_db_file(DbName, SizeInKb) ->
    {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]),
    FilePath = couch_db:get_filepath(Db),
    {ok, Fd} = file:open(FilePath, [append]),
    Bytes = binary:copy(?KILOBYTE, SizeInKb),
    file:write(Fd, Bytes),
    ok = file:close(Fd),
    Doc = couch_doc:from_json_obj(
        {[
            {<<"_id">>, ?l2b(?docid())},
            {<<"value">>, ?l2b(?docid())}
        ]}
    ),
    {ok, _} = couch_db:update_docs(Db, [Doc], []),
    couch_db:close(Db),
    ok.

is_enqueued(ChannelType, DbName) ->
    {ok, ChannelPid} = smoosh_server:get_channel(ChannelType),
    smoosh_channel:is_key(ChannelPid, DbName).

wait_enqueue(ChannelType, DbName) ->
    test_util:wait(fun() ->
        case is_enqueued(ChannelType, DbName) of
            false ->
                wait;
            true ->
                ok
        end
    end).

channel_queue(ChannelType) ->
    Q0 = smoosh_priority_queue:new(ChannelType),
    smoosh_priority_queue:recover(Q0).