summaryrefslogtreecommitdiff
path: root/test/etap/041-uuid-gen.t
blob: dd88ae93b12b5d20a9f8fbdbb829eef132a0c9b5 (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
#!/usr/bin/env escript
%% -*- erlang -*-

% 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.

default_config() ->
    "etc/couchdb/default_dev.ini".

seq_alg_config() ->
    "test/etap/041-uuid-gen-seq.ini".

utc_alg_config() ->
    "test/etap/041-uuid-gen-utc.ini".

% Run tests and wait for the gen_servers to shutdown
run_test(IniFiles, Test) ->
    {ok, Pid} = couch_config:start_link(IniFiles),
    erlang:monitor(process, Pid),
    couch_uuids:start(),
    Test(),
    couch_uuids:stop(),
    couch_config:stop(),
    receive
        {'DOWN', _, _, Pid, _} -> ok;
        _Other -> etap:diag("OTHER: ~p~n", [_Other])
    after
        1000 -> throw({timeout_error, config_stop})
    end.

main(_) ->
    code:add_patha("src/etap"),
    code:add_pathz("src/couchdb"),
    application:start(crypto),
    etap:plan(unknown),

    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail(Other)
    end,
    ok.

test() ->

    TestUnique = fun() ->
        etap:is(
            test_unique(10000, couch_uuids:new()),
            true,
            "Can generate 10K unique IDs"
        )
    end,
    run_test([default_config()], TestUnique),
    run_test([default_config(), seq_alg_config()], TestUnique),
    run_test([default_config(), utc_alg_config()], TestUnique),

    TestMonotonic = fun () ->
        etap:is(
            couch_uuids:new() < couch_uuids:new(),
            true,
            "should produce monotonically increasing ids"
        )
    end,
    run_test([default_config(), seq_alg_config()], TestMonotonic),
    run_test([default_config(), utc_alg_config()], TestMonotonic),

    % Pretty sure that the average of a uniform distribution is the
    % midpoint of the range. Thus, to exceed a threshold, we need
    % approximately Total / (Range/2 + RangeMin) samples.
    %
    % In our case this works out to be 8194. (0xFFF000 / 0x7FF)
    % These tests just fudge the limits for a good generator at 25%
    % in either direction. Technically it should be possible to generate
    % bounds that will show if your random number generator is not
    % sufficiently random but I hated statistics in school.
    TestRollOver = fun() ->
        UUID = binary_to_list(couch_uuids:new()),
        Prefix = element(1, lists:split(26, UUID)),
        N = gen_until_pref_change(Prefix,0),
        etap:diag("N is: ~p~n",[N]),                           
        etap:is(
            N >= 5000 andalso N =< 11000,
            true,
            "should roll over every so often."
        )
    end,
    run_test([default_config(), seq_alg_config()], TestRollOver).

test_unique(0, _) ->
    true;
test_unique(N, UUID) ->
    case couch_uuids:new() of
        UUID ->
            etap:diag("N: ~p~n", [N]),
            false;
        Else -> test_unique(N-1, Else)
    end.

get_prefix(UUID) ->
    element(1, lists:split(26, binary_to_list(UUID))).

gen_until_pref_change(_, Count) when Count > 8251 ->
    Count;
gen_until_pref_change(Prefix, N) ->
    case get_prefix(couch_uuids:new()) of
        Prefix -> gen_until_pref_change(Prefix, N+1);
        _ -> N
    end.