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 8168F11DFA for ; Mon, 16 Jun 2014 22:52:49 +0000 (UTC) Received: (qmail 96958 invoked by uid 500); 16 Jun 2014 22:52:43 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 96633 invoked by uid 500); 16 Jun 2014 22:52:43 -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 96150 invoked by uid 99); 16 Jun 2014 22:52:43 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Jun 2014 22:52:43 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id ADF9A941755; Mon, 16 Jun 2014 22:52:42 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kxepal@apache.org To: commits@couchdb.apache.org Date: Mon, 16 Jun 2014 22:52:53 -0000 Message-Id: <586d2dccb39d4eafaadf996f42166cf8@git.apache.org> In-Reply-To: <86731430c5194546a2188404fdce3eb5@git.apache.org> References: <86731430c5194546a2188404fdce3eb5@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [12/50] couchdb commit: updated refs/heads/1963-eunit to bfb7eb9 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/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/806f13bb Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/806f13bb Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/806f13bb Branch: refs/heads/1963-eunit Commit: 806f13bbd8d8985eae044b4650ca630ecb5fd13e Parents: 09a0e6d Author: Alexander Shorin Authored: Mon May 26 09:26:22 2014 +0400 Committer: Alexander Shorin Committed: Mon Jun 16 00:51:18 2014 +0400 ---------------------------------------------------------------------- test/couchdb/couch_config_tests.erl | 147 ++++++++++++++++++++++++++++++- test/etap/082-config-register.t | 94 -------------------- test/etap/Makefile.am | 1 - 3 files changed, 146 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/806f13bb/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). http://git-wip-us.apache.org/repos/asf/couchdb/blob/806f13bb/test/etap/082-config-register.t ---------------------------------------------------------------------- diff --git a/test/etap/082-config-register.t b/test/etap/082-config-register.t deleted file mode 100755 index 191ba8f..0000000 --- a/test/etap/082-config-register.t +++ /dev/null @@ -1,94 +0,0 @@ -#!/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() -> - test_util:build_file("etc/couchdb/default_dev.ini"). - -main(_) -> - test_util:init_code_path(), - etap:plan(5), - 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() -> - couch_config:start_link([default_config()]), - - etap:is( - couch_config:get("httpd", "port"), - "5984", - "{httpd, port} is 5984 by default." - ), - - ok = couch_config:set("httpd", "port", "4895", false), - - etap:is( - couch_config:get("httpd", "port"), - "4895", - "{httpd, port} changed to 4895" - ), - - SentinelFunc = fun() -> - % Ping/Pong to make sure we wait for this - % process to die - receive {ping, From} -> From ! pong end - end, - SentinelPid = spawn(SentinelFunc), - - couch_config:register( - fun("httpd", "port", Value) -> - etap:is(Value, "8080", "Registered function got notification.") - end, - SentinelPid - ), - - ok = couch_config:set("httpd", "port", "8080", false), - - % Implicitly checking that we *don't* call the function - etap:is( - couch_config:get("httpd", "bind_address"), - "127.0.0.1", - "{httpd, bind_address} is not '0.0.0.0'" - ), - ok = couch_config:set("httpd", "bind_address", "0.0.0.0", false), - - % Ping-Pong kill process - SentinelPid ! {ping, self()}, - receive - _Any -> ok - after 1000 -> - throw({timeout_error, registered_pid}) - end, - - ok = couch_config:set("httpd", "port", "80", false), - etap:is( - couch_config:get("httpd", "port"), - "80", - "Implicitly test that the function got de-registered" - ), - - % test passing of Persist flag - couch_config:register( - fun("httpd", _, _, Persist) -> - etap:is(Persist, false) - end), - ok = couch_config:set("httpd", "port", "80", false), - - ok. http://git-wip-us.apache.org/repos/asf/couchdb/blob/806f13bb/test/etap/Makefile.am ---------------------------------------------------------------------- diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am index 07583ac..25889f4 100644 --- a/test/etap/Makefile.am +++ b/test/etap/Makefile.am @@ -36,7 +36,6 @@ fixture_files = \ fixtures/test.couch tap_files = \ - 082-config-register.t \ 083-config-no-files.t \ 090-task-status.t \ 100-ref-counter.t \