Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BDE271116E for ; Fri, 5 Sep 2014 21:11:11 +0000 (UTC) Received: (qmail 15370 invoked by uid 500); 5 Sep 2014 21:11:11 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 15150 invoked by uid 500); 5 Sep 2014 21:11:11 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 15059 invoked by uid 99); 5 Sep 2014 21:11:11 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Sep 2014 21:11:11 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 49AA8A0B3F2; Fri, 5 Sep 2014 21:11:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: rnewson@apache.org To: commits@couchdb.apache.org Date: Fri, 05 Sep 2014 21:11:13 -0000 Message-Id: In-Reply-To: <9c092c0caa124507956374a1aae69721@git.apache.org> References: <9c092c0caa124507956374a1aae69721@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [03/12] config commit: updated refs/heads/master to 1eb910f Port 082-config-register.t etap test suite to eunit Merged into couch_config_tests suite. Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/25f08978 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/25f08978 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/25f08978 Branch: refs/heads/master Commit: 25f0897818a201d3ed048d8417103be829627228 Parents: 6c115d7 Author: Alexander Shorin Authored: Mon May 26 09:26:22 2014 +0400 Committer: Russell Branca Committed: Thu Aug 28 13:57:09 2014 -0700 ---------------------------------------------------------------------- test/couchdb/couch_config_tests.erl | 147 ++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/25f08978/test/couchdb/couch_config_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/couch_config_tests.erl b/test/couchdb/couch_config_tests.erl index 4bdc333..fdd2479 100644 --- a/test/couchdb/couch_config_tests.erl +++ b/test/couchdb/couch_config_tests.erl @@ -15,6 +15,7 @@ -include("couch_eunit.hrl"). -include_lib("couchdb/couch_db.hrl"). +-define(SHORT_TIMEOUT, 100). -define(TIMEOUT, 1000). -define(CONFIG_DEFAULT, @@ -43,6 +44,32 @@ setup(Chain) -> {ok, Pid} = couch_config:start_link(Chain), Pid. +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) -> couch_config:stop(), erlang:monitor(process, Pid), @@ -64,7 +91,8 @@ couch_config_test_() -> couch_config_set_tests(), couch_config_del_tests(), config_override_tests(), - config_persistent_changes_tests() + config_persistent_changes_tests(), + config_register_tests() ] }. @@ -152,6 +180,21 @@ 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 + ] + } + }. + should_load_all_configs() -> ?_assert(length(couch_config:all()) > 0). @@ -281,3 +324,105 @@ should_ensure_that_written_to_last_config_in_chain(_, _) -> ?assertEqual(undefined, couch_config:get("httpd", "bind_address")) end). + +should_handle_port_changes({_, SentinelPid}) -> + ?_assert(begin + MainProc = self(), + Port = "8080", + + couch_config:register( + fun("httpd", "port", Value) -> + % couch_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 = couch_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(), + + couch_config:register( + fun("httpd", "port", _, Persist) -> + % couch_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 = couch_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(), + + couch_config:register( + fun("httpd", "port", _) -> + MainProc ! ok + end, + SentinelPid + ), + ok = couch_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(), + + couch_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 = couch_config:set("httpd", "port", "12345", false), + + receive + ok -> + false + after ?SHORT_TIMEOUT -> + true + end + end).