1#!/usr/bin/env escript
2%% -*- erlang -*-
3%%! -smp enable
4
5% Licensed under the Apache License, Version 2.0 (the "License"); you may not
6% use this file except in compliance with the License. You may obtain a copy of
7% the License at
8%
9%   http://www.apache.org/licenses/LICENSE-2.0
10%
11% Unless required by applicable law or agreed to in writing, software
12% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14% License for the specific language governing permissions and limitations under
15% the License.
16
17default_config() ->
18    test_util:build_file("etc/couchdb/default_dev.ini").
19
20main(_) ->
21    test_util:init_code_path(),
22    etap:plan(5),
23    case (catch test()) of
24        ok ->
25            etap:end_tests();
26        Other ->
27            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
28            etap:bail(Other)
29    end,
30    ok.
31
32test() ->
33    couch_config:start_link([default_config()]),
34
35    etap:is(
36        couch_config:get("httpd", "port"),
37        "5984",
38        "{httpd, port} is 5984 by default."
39    ),
40
41    ok = couch_config:set("httpd", "port", "4895", false),
42
43    etap:is(
44        couch_config:get("httpd", "port"),
45        "4895",
46        "{httpd, port} changed to 4895"
47    ),
48
49    SentinelFunc = fun() ->
50        % Ping/Pong to make sure we wait for this
51        % process to die
52        receive {ping, From} -> From ! pong end
53    end,
54    SentinelPid = spawn(SentinelFunc),
55
56    couch_config:register(
57        fun("httpd", "port", Value) ->
58            etap:is(Value, "8080", "Registered function got notification.")
59        end,
60        SentinelPid
61    ),
62
63    ok = couch_config:set("httpd", "port", "8080", false),
64
65    {Field, Addr, AddrAny} = case misc:is_ipv6() of
66                true -> {"ip6_bind_address", "::1", "::0"};
67                false -> {"ip4_bind_address", "127.0.0.1", "0.0.0.0"}
68            end,
69
70
71    % Implicitly checking that we *don't* call the function
72    etap:is(
73        couch_config:get("httpd", Field),
74        Addr,
75        "{httpd, " ++ Field ++ "} is not " ++ AddrAny
76    ),
77    ok = couch_config:set("httpd", Field, AddrAny, false),
78
79    % Ping-Pong kill process
80    SentinelPid ! {ping, self()},
81    receive
82        _Any -> ok
83    after 1000 ->
84        throw({timeout_error, registered_pid})
85    end,
86
87    ok = couch_config:set("httpd", "port", "80", false),
88    etap:is(
89        couch_config:get("httpd", "port"),
90        "80",
91        "Implicitly test that the function got de-registered"
92    ),
93
94    % test passing of Persist flag
95    couch_config:register(
96        fun("httpd", _, _, Persist) ->
97            etap:is(Persist, false)
98        end),
99    ok = couch_config:set("httpd", "port", "80", false),
100
101    ok.
102