summaryrefslogtreecommitdiff
path: root/src/fabric/test/eunit/fabric_bench_test.erl
blob: 95d0dc41c8cbe1f1171767208e78d8075b4170c3 (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
% 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(fabric_bench_test).

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

fabric_bench_test_() ->
    {
        setup,
        fun setup/0,
        fun teardown/1,
        with([
            ?TDEF(t_default_doc_size, 15),
            ?TDEF(t_small_doc_size, 15),
            ?TDEF(t_large_doc_size, 15),
            ?TDEF(t_old_db_deletion_works),
            ?TDEF(t_newer_db_deletion_doesnt_work),
            ?TDEF(t_db_deletion_ignores_other_dbs)
        ])
    }.

setup() ->
    test_util:start_couch([fabric]).

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

t_default_doc_size(_Ctx) ->
    Opts = #{docs => 100, individual_docs => 5},
    % The goal is to just have it not crash
    ?assertEqual(ok, fabric_bench:go(Opts)).

t_small_doc_size(_Ctx) ->
    Opts = #{q => 4, docs => 100, doc_size => small, individual_docs => 5},
    % The goal is to just have it not crash
    ?assertEqual(ok, fabric_bench:go(Opts)).

t_large_doc_size(_Ctx) ->
    Opts = #{q => 1, docs => 5, doc_size => large, individual_docs => 1},
    % The goal is to just have it not crash
    ?assertEqual(ok, fabric_bench:go(Opts)).

t_old_db_deletion_works(_Ctx) ->
    NineHoursAgoUsec = os:system_time(microsecond) - (9 * 60 * 60 * 1000000),
    Suffix = integer_to_binary(NineHoursAgoUsec),
    Db = <<"fabricbenchdb-", Suffix/binary>>,
    ok = fabric:create_db(Db, [{q, 1}, {n, 1}]),
    fabric_bench:delete_old_dbs(),
    ?assertError(database_does_not_exist, fabric:get_doc_count(Db)).

t_newer_db_deletion_doesnt_work(_Ctx) ->
    SevenHoursAgoUsec = os:system_time(microsecond) - (7 * 60 * 60 * 1000000),
    Suffix = integer_to_binary(SevenHoursAgoUsec),
    Db = <<"fabricbenchdb-", Suffix/binary>>,
    ok = fabric:create_db(Db, [{q, 1}, {n, 1}]),
    fabric_bench:delete_old_dbs(),
    ?assertEqual({ok, 0}, fabric:get_doc_count(Db)).

t_db_deletion_ignores_other_dbs(_Ctx) ->
    Db1 = <<"fabricbenchdb-">>,
    Db2 = <<"fabricbenchdb">>,
    Db3 = <<"fabricbenchdb-xyz">>,
    ok = fabric:create_db(Db1, [{q, 1}, {n, 1}]),
    ok = fabric:create_db(Db2, [{q, 1}, {n, 1}]),
    ok = fabric:create_db(Db3, [{q, 1}, {n, 1}]),
    fabric_bench:delete_old_dbs(),
    ?assertEqual({ok, 0}, fabric:get_doc_count(Db1)),
    ?assertEqual({ok, 0}, fabric:get_doc_count(Db2)),
    ?assertEqual({ok, 0}, fabric:get_doc_count(Db3)).