You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by kx...@apache.org on 2015/02/04 16:49:36 UTC

[1/5] config commit: updated refs/heads/master to b281825

Repository: couchdb-config
Updated Branches:
  refs/heads/master 8e8ac7006 -> b2818252e


Remove tests for dropped register functionality

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/e19b7d5c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/e19b7d5c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/e19b7d5c

Branch: refs/heads/master
Commit: e19b7d5c02b70e14138c9fe1e21f2650100d0a21
Parents: 8e8ac70
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Thu Jan 29 12:52:29 2015 -0800
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Thu Jan 29 12:52:29 2015 -0800

----------------------------------------------------------------------
 test/config_tests.erl | 144 ---------------------------------------------
 1 file changed, 144 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/e19b7d5c/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 256ba99..636cb05 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -50,32 +50,6 @@ setup(Chain) ->
 setup_empty() ->
     setup([]).
 
-setup_register() ->
-    ConfigPid = setup(),
-    SentinelFunc = fun() ->
-        % Ping/Pong to make sure we wait for this
-        % process to die
-        receive
-            {ping, From} ->
-                From ! pong
-        end
-    end,
-    SentinelPid = spawn(SentinelFunc),
-    {ConfigPid, SentinelPid}.
-
-teardown({ConfigPid, SentinelPid}) ->
-    teardown(ConfigPid),
-    case process_info(SentinelPid) of
-        undefined -> ok;
-        _ ->
-            SentinelPid ! {ping, self()},
-            receive
-                pong ->
-                    ok
-            after 100 ->
-                throw({timeout_error, registered_pid})
-            end
-    end;
 teardown(Pid) ->
     config:stop(),
     [ok = application:stop(App) || App <- ?DEPS],
@@ -99,7 +73,6 @@ config_test_() ->
             %% config_del_tests(),
             config_override_tests()
             %% config_persistent_changes_tests(),
-            %% config_register_tests(),
             %% config_no_files_tests()
         ]
     }.
@@ -188,21 +161,6 @@ config_persistent_changes_tests() ->
         }
     }.
 
-config_register_tests() ->
-    {
-        "Config changes subscriber",
-        {
-            foreach,
-            fun setup_register/0, fun teardown/1,
-            [
-                fun should_handle_port_changes/1,
-                fun should_pass_persistent_flag/1,
-                fun should_not_trigger_handler_on_other_options_changes/1,
-                fun should_not_trigger_handler_after_related_process_death/1
-            ]
-        }
-    }.
-
 config_no_files_tests() ->
     {
         "Test config with no files",
@@ -347,108 +305,6 @@ should_ensure_that_written_to_last_config_in_chain(_, _) ->
                      config:get("httpd", "bind_address"))
     end).
 
-should_handle_port_changes({_, SentinelPid}) ->
-    ?_assert(begin
-        MainProc = self(),
-        Port = "8080",
-
-        config:register(
-            fun("httpd", "port", Value) ->
-                % config catches every error raised from handler
-                % so it's not possible to just assert on wrong value.
-                % We have to return the result as message
-                MainProc ! (Value =:= Port)
-            end,
-            SentinelPid
-        ),
-        ok = config:set("httpd", "port", Port, false),
-
-        receive
-            R ->
-                R
-        after ?TIMEOUT ->
-             erlang:error({assertion_failed,
-                           [{module, ?MODULE},
-                            {line, ?LINE},
-                            {reason, "Timeout"}]})
-        end
-    end).
-
-should_pass_persistent_flag({_, SentinelPid}) ->
-    ?_assert(begin
-        MainProc = self(),
-
-        config:register(
-            fun("httpd", "port", _, Persist) ->
-                % config catches every error raised from handler
-                % so it's not possible to just assert on wrong value.
-                % We have to return the result as message
-                MainProc ! Persist
-            end,
-            SentinelPid
-        ),
-        ok = config:set("httpd", "port", "8080", false),
-
-        receive
-            false ->
-                true
-        after ?SHORT_TIMEOUT ->
-            false
-        end
-    end).
-
-should_not_trigger_handler_on_other_options_changes({_, SentinelPid}) ->
-    ?_assert(begin
-        MainProc = self(),
-
-        config:register(
-            fun("httpd", "port", _) ->
-                MainProc ! ok
-            end,
-            SentinelPid
-        ),
-        ok = config:set("httpd", "bind_address", "0.0.0.0", false),
-
-        receive
-            ok ->
-                false
-        after ?SHORT_TIMEOUT ->
-            true
-        end
-    end).
-
-should_not_trigger_handler_after_related_process_death({_, SentinelPid}) ->
-    ?_assert(begin
-        MainProc = self(),
-
-        config:register(
-            fun("httpd", "port", _) ->
-                MainProc ! ok
-            end,
-            SentinelPid
-        ),
-
-        SentinelPid ! {ping, MainProc},
-        receive
-            pong ->
-                ok
-        after ?SHORT_TIMEOUT ->
-             erlang:error({assertion_failed,
-                           [{module, ?MODULE},
-                            {line, ?LINE},
-                            {reason, "Timeout"}]})
-        end,
-
-        ok = config:set("httpd", "port", "12345", false),
-
-        receive
-            ok ->
-                false
-        after ?SHORT_TIMEOUT ->
-            true
-        end
-    end).
-
 should_ensure_that_no_ini_files_loaded() ->
     ?_assertEqual(0, length(config:all())).
 


[2/5] config commit: updated refs/heads/master to b281825

Posted by kx...@apache.org.
Change setup/teardown logic and enable some tests

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/ee24f3ef
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/ee24f3ef
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/ee24f3ef

Branch: refs/heads/master
Commit: ee24f3ef1a40c1441852dea41061998ac30eb3de
Parents: e19b7d5
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Thu Jan 29 12:53:11 2015 -0800
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Thu Jan 29 14:19:59 2015 -0800

----------------------------------------------------------------------
 test/config_tests.erl | 49 +++++++++++++++++++++-------------------------
 1 file changed, 22 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/ee24f3ef/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 636cb05..dcfb071 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -33,7 +33,8 @@
         FileName
     end).
 
--define(DEPS, [couch_stats, couch_log, folsom, lager, goldrush, syntax_tools, compiler]).
+-define(DEPS, [couch_stats, couch_log, folsom, lager,
+               goldrush, syntax_tools, compiler, config]).
 
 
 setup() ->
@@ -43,37 +44,31 @@ setup({temporary, Chain}) ->
 setup({persistent, Chain}) ->
     setup(lists:append(Chain, [?CONFIG_FIXTURE_TEMP]));
 setup(Chain) ->
-    [ok = application:start(App) || App <- lists:reverse(?DEPS)],
-    {ok, Pid} = test_util:start_config(Chain),
-    Pid.
+    ok = application:set_env(config, ini_files, Chain),
+    test_util:start_applications(?DEPS).
 
 setup_empty() ->
     setup([]).
 
-teardown(Pid) ->
-    config:stop(),
-    [ok = application:stop(App) || App <- ?DEPS],
-    erlang:monitor(process, Pid),
-    receive
-        {'DOWN', _, _, Pid, _} ->
-            ok
-    after ?TIMEOUT ->
-        throw({timeout_error, config_stop})
-    end.
-teardown(_, Pid) ->
-    teardown(Pid).
+teardown({Pid, _}) ->
+    stop_listener(Pid),
+    [application:stop(App) || App <- ?DEPS];
+teardown(_) ->
+    [application:stop(App) || App <- ?DEPS].
 
+teardown(_, _) ->
+    [application:stop(App) || App <- ?DEPS].
 
 config_test_() ->
     {
         "CouchDB config tests",
         [
             config_get_tests(),
-            %% config_set_tests(),
-            %% config_del_tests(),
-            config_override_tests()
-            %% config_persistent_changes_tests(),
-            %% config_no_files_tests()
+            config_set_tests(),
+            config_del_tests(),
+            config_override_tests(),
+            config_persistent_changes_tests(),
+            config_no_files_tests()
         ]
     }.
 
@@ -90,8 +85,8 @@ config_get_tests() ->
                 should_return_undefined_atom_on_missed_section(),
                 should_return_undefined_atom_on_missed_option(),
                 should_return_custom_default_value_on_missed_option(),
-                should_only_return_default_on_missed_option(),
-                should_get_binary_option()
+                should_only_return_default_on_missed_option()%,
+                %%should_get_binary_option()
             ]
         }
     }.
@@ -104,8 +99,8 @@ config_set_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_update_option(),
-                should_create_new_section(),
-                should_set_binary_option()
+                should_create_new_section()%,
+                %%should_set_binary_option()
             ]
         }
     }.
@@ -118,8 +113,8 @@ config_del_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_return_undefined_atom_after_option_deletion(),
-                should_be_ok_on_deleting_unknown_options(),
-                should_delete_binary_option()
+                should_be_ok_on_deleting_unknown_options()%,
+                %%should_delete_binary_option()
             ]
         }
     }.


[3/5] config commit: updated refs/heads/master to b281825

Posted by kx...@apache.org.
Callback driven API for config_listener behaviour

This implementaion replaces a mix of message driven and callback driven
API for config_listener. In particular it replaces gen_event_EXIT
message with a call to Module:handle_config_stop(Pid, Reason, State).
This fixes the problem of using config:listen_for_changes in supervisor
context where there is no way to handle arbitrary messages.

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/e151ae43
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/e151ae43
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/e151ae43

Branch: refs/heads/master
Commit: e151ae430454121db74a051b61bd23f582bcc39b
Parents: ee24f3e
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Thu Jan 29 12:55:27 2015 -0800
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Wed Feb 4 06:00:10 2015 -0800

----------------------------------------------------------------------
 src/config.erl          |  18 +++++-
 src/config_listener.erl |  15 ++---
 test/config_tests.erl   | 137 ++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 159 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/e151ae43/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index d123b12..dcffe86 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -175,7 +175,7 @@ delete(Section, Key, Persist, Reason) when is_list(Section), is_list(Key) ->
 
 
 listen_for_changes(CallbackModule, InitialState) ->
-    config_listener:start(CallbackModule, InitialState).
+    gen_server:call(?MODULE, {listen_for_changes, CallbackModule, InitialState}).
 
 init(IniFiles) ->
     ets:new(?MODULE, [named_table, set, protected]),
@@ -250,14 +250,26 @@ handle_call(reload, _From, Config) ->
                 ets:delete(?MODULE, K)
         end
     end, nil, ?MODULE),
-    {reply, ok, Config}.
-
+    {reply, ok, Config};
+handle_call({listen_for_changes, CallbackModule, InitialState},
+        {Subscriber, _}, Config) ->
+    Reply = config_listener:start(CallbackModule, {Subscriber, InitialState}),
+    {reply, Reply, Config}.
 
 handle_cast(stop, State) ->
     {stop, normal, State};
 handle_cast(_Msg, State) ->
     {noreply, State}.
 
+handle_info({gen_event_EXIT, {config_listener, Module}, shutdown}, State)  ->
+    couch_log:notice("config_listener(~p) stopped with reason: shutdown~n", [Module]),
+    {noreply, State};
+handle_info({gen_event_EXIT, {config_listener, Module}, normal}, State)  ->
+    couch_log:info("config_listener(~p) stopped with reason: shutdown~n", [Module]),
+    {noreply, State};
+handle_info({gen_event_EXIT, {config_listener, Module}, Reason}, State) ->
+    couch_log:error("config_listener(~p) stopped with reason: ~p~n", [Module, Reason]),
+    {noreply, State};
 handle_info(Info, State) ->
     couch_log:error("config:handle_info Info: ~p~n", [Info]),
     {noreply, State}.

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/e151ae43/src/config_listener.erl
----------------------------------------------------------------------
diff --git a/src/config_listener.erl b/src/config_listener.erl
index 79d3806..22ce366 100644
--- a/src/config_listener.erl
+++ b/src/config_listener.erl
@@ -13,7 +13,7 @@
 -module(config_listener).
 
 -behaviour(gen_event).
--vsn(1).
+-vsn(2).
 
 %% Public interface
 -export([start/2]).
@@ -22,11 +22,12 @@
 -export([behaviour_info/1]).
 
 %% Required gen_event interface
--export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, 
+-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2,
     code_change/3]).
 
 behaviour_info(callbacks) ->
-    [{handle_config_change,5}];
+    [{handle_config_change,5},
+     {handle_config_terminate, 3}];
 behaviour_info(_) ->
     undefined.
 
@@ -39,10 +40,10 @@ start(Module, Id, State) ->
 init({Module, State}) ->
     {ok, {Module, State}}.
 
-handle_event({config_change, Sec, Key, Value, Persist}, {Module, State}) ->
+handle_event({config_change, Sec, Key, Value, Persist}, {Module, {From, State}}) ->
     case Module:handle_config_change(Sec, Key, Value, Persist, State) of
         {ok, NewState} ->
-            {ok, {Module, NewState}};
+            {ok, {Module, {From, NewState}}};
         remove_handler ->
             remove_handler
     end.
@@ -53,8 +54,8 @@ handle_call(_Request, St) ->
 handle_info(_Info, St) ->
     {ok, St}.
 
-terminate(_Reason, _St) ->
-    ok.
+terminate(Reason, {Module, {Subscriber, State}}) ->
+    Module:handle_config_terminate(Subscriber, Reason, State).
 
 code_change(_OldVsn, St, _Extra) ->
     {ok, St}.

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/e151ae43/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index dcfb071..2d3968c 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -12,6 +12,10 @@
 
 -module(config_tests).
 
+-beahiour(config_listener).
+
+-export([handle_config_change/5, handle_config_terminate/3]).
+
 -include_lib("couch/include/couch_eunit.hrl").
 -include_lib("couch/include/couch_db.hrl").
 
@@ -50,6 +54,12 @@ setup(Chain) ->
 setup_empty() ->
     setup([]).
 
+setup_config_listener() ->
+    setup(),
+    {ok, Pid} = spawn_listener(),
+    config:listen_for_changes(?MODULE, {Pid, self(), []}),
+    {Pid, self()}.
+
 teardown({Pid, _}) ->
     stop_listener(Pid),
     [application:stop(App) || App <- ?DEPS];
@@ -59,6 +69,25 @@ teardown(_) ->
 teardown(_, _) ->
     [application:stop(App) || App <- ?DEPS].
 
+handle_config_change("remove_handler", _Key, _Value, _Persist, _State) ->
+    remove_handler;
+handle_config_change("update_state", Key, _Value, _Persist, {Listener, Subscriber, Items}) ->
+    NewState = {Listener, Subscriber, [Key|Items]},
+    ok = reply(NewState, NewState),
+    {ok, NewState};
+handle_config_change(Section, Key, Value, Persist, State) ->
+    ok = reply({{Section, Key, Value, Persist}, State}, State),
+    {ok, State}.
+handle_config_terminate(Self, Reason, State) ->
+    ok = reply({stop, Self, Reason, State}, State),
+    ok.
+
+reply(Reply, {Listener, _, _}) ->
+    call_sync(Listener, {set, Reply}).
+
+wait_reply(Listener) ->
+    call_sync(Listener, get).
+
 config_test_() ->
     {
         "CouchDB config tests",
@@ -68,7 +97,8 @@ config_test_() ->
             config_del_tests(),
             config_override_tests(),
             config_persistent_changes_tests(),
-            config_no_files_tests()
+            config_no_files_tests(),
+            config_listener_behaviour_tests()
         ]
     }.
 
@@ -170,6 +200,22 @@ config_no_files_tests() ->
         }
     }.
 
+config_listener_behaviour_tests() ->
+    {
+        "Test config_listener behaviour",
+        {
+            foreach,
+            fun setup_config_listener/0, fun teardown/1,
+            [
+                fun should_handle_value_change/1,
+                fun should_pass_correct_state_to_handle_config_change/1,
+                fun should_pass_correct_state_to_handle_config_terminate/1,
+                fun should_pass_subscriber_pid_to_handle_config_terminate/1,
+                fun should_not_call_handle_config_after_related_process_death/1,
+                fun should_remove_handler_when_requested/1
+            ]
+        }
+    }.
 
 should_load_all_configs() ->
     ?_assert(length(config:all()) > 0).
@@ -316,3 +362,92 @@ should_create_persistent_option() ->
             ok = config:set("httpd", "bind_address", "127.0.0.1"),
             config:get("httpd", "bind_address")
         end).
+
+should_handle_value_change({Pid, _}) ->
+    ?_test(begin
+        ok = config:set("httpd", "port", "80", false),
+        ?assertMatch({{"httpd", "port", "80", false}, _}, wait_reply(Pid))
+    end).
+should_pass_correct_state_to_handle_config_change({Pid, _}) ->
+    ?_test(begin
+        ok = config:set("httpd", "port", "80", false),
+        ?assertMatch({_, {Pid, _, []}}, wait_reply(Pid)),
+        ok = config:set("update_state", "foo", "any", false),
+        ?assertMatch({Pid, _, ["foo"]}, wait_reply(Pid))
+    end).
+should_pass_correct_state_to_handle_config_terminate({Pid, _}) ->
+    ?_test(begin
+        %% prepare some state
+        ok = config:set("httpd", "port", "80", false),
+        ?assertMatch({_, {Pid, _, []}}, wait_reply(Pid)),
+        ok = config:set("update_state", "foo", "any", false),
+        ?assertMatch({Pid, _, ["foo"]}, wait_reply(Pid)),
+
+        %% remove handler
+        ok = config:set("remove_handler", "any", "any", false),
+        Reply = wait_reply(Pid),
+        ?assertMatch({stop, _, remove_handler, _}, Reply),
+
+        {stop, Subscriber, _Reason, State} = Reply,
+        ?assert(is_pid(Subscriber)),
+        ?assertMatch({Pid, Subscriber, ["foo"]}, State)
+    end).
+should_pass_subscriber_pid_to_handle_config_terminate({Pid, SubscriberPid}) ->
+    ?_test(begin
+        ok = config:set("remove_handler", "any", "any", false),
+        Reply = wait_reply(Pid),
+        ?assertMatch({stop, _, remove_handler, _}, Reply),
+
+        {stop, Subscriber, _Reason, _State} = Reply,
+        ?assertMatch(SubscriberPid, Subscriber)
+    end).
+should_not_call_handle_config_after_related_process_death({Pid, _}) ->
+    ?_test(begin
+        ok = config:set("remove_handler", "any", "any", false),
+        Reply = wait_reply(Pid),
+        ?assertMatch({stop, _, remove_handler, _}, Reply),
+
+        ok = config:set("httpd", "port", "80", false),
+        ?assertMatch(undefined, wait_reply(Pid))
+    end).
+should_remove_handler_when_requested({Pid, _}) ->
+    ?_test(begin
+        ?assertEqual(1, n_handlers()),
+
+        ok = config:set("remove_handler", "any", "any", false),
+        Reply = wait_reply(Pid),
+        ?assertMatch({stop, _, remove_handler, _}, Reply),
+
+        ?assertEqual(0, n_handlers())
+    end).
+
+call_sync(Listener, Msg) ->
+    Ref = make_ref(),
+    Listener ! {Ref, self(), Msg},
+    receive
+        {ok, Ref, Reply} -> Reply
+    after ?TIMEOUT ->
+        throw({error, {timeout, call_sync}})
+    end.
+
+spawn_listener() ->
+    {ok, spawn(fun() -> loop(undefined) end)}.
+
+stop_listener(Listener) ->
+    call_sync(Listener, stop).
+
+loop(State) ->
+    receive
+        {Ref, From, stop} ->
+            From ! {ok, Ref, ok},
+            ok;
+        {Ref, From, {set, Value}} ->
+            From ! {ok, Ref, ok},
+            loop(Value);
+        {Ref, From, get} ->
+            From ! {ok, Ref, State},
+            loop(undefined)
+    end.
+
+n_handlers() ->
+    length(gen_event:which_handlers(config_event)).


[5/5] config commit: updated refs/heads/master to b281825

Posted by kx...@apache.org.
Merge remote-tracking branch 'iilyak/2561-make-config-API-consistent'

This closes #2

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/b2818252
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/b2818252
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/b2818252

Branch: refs/heads/master
Commit: b2818252eeb5de4a010a3797851b83e051e6c817
Parents: 8e8ac70 135af48
Author: Alexander Shorin <kx...@apache.org>
Authored: Wed Feb 4 18:43:14 2015 +0300
Committer: Alexander Shorin <kx...@apache.org>
Committed: Wed Feb 4 18:43:14 2015 +0300

----------------------------------------------------------------------
 src/config.erl          |  29 +++-
 src/config_listener.erl |  15 +-
 test/config_tests.erl   | 352 ++++++++++++++++++++-----------------------
 3 files changed, 199 insertions(+), 197 deletions(-)
----------------------------------------------------------------------



[4/5] config commit: updated refs/heads/master to b281825

Posted by kx...@apache.org.
Enforce type verification for config:set/get

Check the type of given default value to make sure it is supported.
Raise error(badarg) from set/get on type missmatch.
Add tests for the feature

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/135af48e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/135af48e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/135af48e

Branch: refs/heads/master
Commit: 135af48ed7cdcdf483b4f7e984e8f7ff278fff03
Parents: e151ae4
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Thu Jan 29 13:01:21 2015 -0800
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Wed Feb 4 06:00:10 2015 -0800

----------------------------------------------------------------------
 src/config.erl        | 11 +++++++++--
 test/config_tests.erl | 40 ++++++++++++++++++----------------------
 2 files changed, 27 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/135af48e/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index dcffe86..a24bb27 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -139,7 +139,12 @@ get(Section, Key, Default) when is_binary(Section) and is_binary(Key) ->
     ?MODULE:get(binary_to_list(Section), binary_to_list(Key), Default);
 get(Section, Key, Default) when is_list(Section), is_list(Key) ->
     case ets:lookup(?MODULE, {Section, Key}) of
-        [] -> Default;
+        [] when Default == undefined -> Default;
+        [] when is_boolean(Default) -> Default;
+        [] when is_float(Default) -> Default;
+        [] when is_integer(Default) -> Default;
+        [] when is_list(Default) -> Default;
+        [] -> error(badarg);
         [{_, Match}] -> Match
     end.
 
@@ -155,7 +160,9 @@ set(Sec, Key, Val, Persist, Reason) when is_binary(Sec) and is_binary(Key) ->
     ?MODULE:set(binary_to_list(Sec), binary_to_list(Key), Val, Persist, Reason);
 set(Section, Key, Value, Persist, Reason)
         when is_list(Section), is_list(Key), is_list(Value) ->
-    gen_server:call(?MODULE, {set, Section, Key, Value, Persist, Reason}).
+    gen_server:call(?MODULE, {set, Section, Key, Value, Persist, Reason});
+set(_Sec, _Key, _Val, _Persist, _Reason) ->
+    error(badarg).
 
 
 delete(Section, Key) when is_binary(Section) and is_binary(Key) ->

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/135af48e/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 2d3968c..c823d3e 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -115,8 +115,9 @@ config_get_tests() ->
                 should_return_undefined_atom_on_missed_section(),
                 should_return_undefined_atom_on_missed_option(),
                 should_return_custom_default_value_on_missed_option(),
-                should_only_return_default_on_missed_option()%,
-                %%should_get_binary_option()
+                should_only_return_default_on_missed_option(),
+                should_fail_to_get_binary_value(),
+                should_return_any_supported_default()
             ]
         }
     }.
@@ -129,8 +130,8 @@ config_set_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_update_option(),
-                should_create_new_section()%,
-                %%should_set_binary_option()
+                should_create_new_section(),
+                should_fail_to_set_binary_value()
             ]
         }
     }.
@@ -143,8 +144,7 @@ config_del_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_return_undefined_atom_after_option_deletion(),
-                should_be_ok_on_deleting_unknown_options()%,
-                %%should_delete_binary_option()
+                should_be_ok_on_deleting_unknown_options()
             ]
         }
     }.
@@ -243,10 +243,17 @@ should_only_return_default_on_missed_option() ->
     ?_assertEqual("0",
                   config:get("httpd", "port", "bar")).
 
-should_get_binary_option() ->
-    ?_assertEqual(<<"baz">>,
+should_fail_to_get_binary_value() ->
+    ?_assertException(error, badarg,
                   config:get(<<"foo">>, <<"bar">>, <<"baz">>)).
 
+should_return_any_supported_default() ->
+    Values = [undefined, "list", true, false, 0.1, 1],
+    Tests = [{lists:flatten(io_lib:format("for type(~p)", [V])), V}
+        || V <- Values],
+    [{T, ?_assertEqual(V, config:get(<<"foo">>, <<"bar">>, V))}
+        || {T, V} <- Tests].
+
 should_update_option() ->
     ?_assertEqual("severe",
         begin
@@ -262,12 +269,9 @@ should_create_new_section() ->
             config:get("new_section", "bizzle")
         end).
 
-should_set_binary_option() ->
-    ?_assertEqual(<<"baz">>,
-        begin
-            ok = config:set(<<"foo">>, <<"bar">>, <<"baz">>, false),
-            config:get(<<"foo">>, <<"bar">>)
-        end).
+should_fail_to_set_binary_value() ->
+    ?_assertException(error, badarg,
+        config:set(<<"foo">>, <<"bar">>, <<"baz">>, false)).
 
 should_return_undefined_atom_after_option_deletion() ->
     ?_assertEqual(undefined,
@@ -279,14 +283,6 @@ should_return_undefined_atom_after_option_deletion() ->
 should_be_ok_on_deleting_unknown_options() ->
     ?_assertEqual(ok, config:delete("zoo", "boo", false)).
 
-should_delete_binary_option() ->
-    ?_assertEqual(undefined,
-        begin
-            ok = config:set(<<"foo">>, <<"bar">>, <<"baz">>, false),
-            ok = config:delete(<<"foo">>, <<"bar">>, false),
-            config:get(<<"foo">>, <<"bar">>)
-        end).
-
 should_ensure_in_defaults(_, _) ->
     ?_test(begin
         ?assertEqual("500",