Merge remote branch 'cloudant:3102-fix-config_subscription'

This closes #15

Signed-off-by: ILYA Khlopotov <iilyak@ca.ibm.com>
diff --git a/src/couch_log_config_listener.erl b/src/couch_log_config_listener.erl
deleted file mode 100644
index bb51e63..0000000
--- a/src/couch_log_config_listener.erl
+++ /dev/null
@@ -1,110 +0,0 @@
-% 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.
-
--module(couch_log_config_listener).
--behaviour(gen_server).
--behaviour(config_listener).
-
-
--export([
-    start_link/0
-]).
-
--export([
-    init/1,
-    terminate/2,
-    handle_call/3,
-    handle_cast/2,
-    handle_info/2,
-    code_change/3
-]).
-
--export([
-    handle_config_change/5,
-    handle_config_terminate/3
-]).
-
-
--ifdef(TEST).
--define(RELISTEN_DELAY, 500).
--else.
--define(RELISTEN_DELAY, 5000).
--endif.
-
-
-start_link() ->
-    gen_server:start_link({local, ?MODULE}, ?MODULE, nil, []).
-
-
-init(_) ->
-    ok = config:listen_for_changes(?MODULE, nil),
-    {ok, nil}.
-
-
-terminate(_, _) ->
-    ok.
-
-
-handle_call(_, _, _) ->
-    {reply, ignored, nil}.
-
-
-handle_cast(_, _) ->
-    {noreply, nil}.
-
-
-handle_info(restart_listener, _) ->
-    ok = config:listen_for_changes(?MODULE, nil),
-    {noreply, nil};
-
-handle_info(_, _) ->
-    {noreply, nil}.
-
-
-code_change(_, _, _) ->
-    {ok, nil}.
-
-
-handle_config_change("log", Key, _, _, _) ->
-    case Key of
-        "level" ->
-            couch_log_config:reconfigure();
-        "max_message_size" ->
-            couch_log_config:reconfigure();
-        _ ->
-            % Someone may have changed the config for
-            % the writer so we need to re-initialize.
-            couch_log_server:reconfigure()
-    end,
-    notify_listeners(),
-    {ok, nil};
-
-handle_config_change(_, _, _, _, Settings) ->
-    {ok, Settings}.
-
-
-handle_config_terminate(_, stop, _) ->
-    ok;
-handle_config_terminate(_, _, _) ->
-    erlang:send_after(?RELISTEN_DELAY, whereis(?MODULE), restart_listener).
-
-
--ifdef(TEST).
-notify_listeners() ->
-    Listeners = application:get_env(couch_log, config_listeners, []),
-    lists:foreach(fun(L) ->
-        L ! couch_log_config_change_finished
-    end, Listeners).
--else.
-notify_listeners() ->
-    ok.
--endif.
diff --git a/src/couch_log_sup.erl b/src/couch_log_sup.erl
index fc84a8a..083f5fc 100644
--- a/src/couch_log_sup.erl
+++ b/src/couch_log_sup.erl
@@ -13,10 +13,12 @@
 -module(couch_log_sup).
 
 -behaviour(supervisor).
+-vsn(1).
+-behaviour(config_listener).
 
 -export([init/1]).
 -export([start_link/0]).
-
+-export([handle_config_change/5, handle_config_terminate/3]).
 
 start_link() ->
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
@@ -46,11 +48,42 @@
             [couch_log_monitor]
         },
         {
-            couch_log_config_listener,
-            {couch_log_config_listener, start_link, []},
+            config_listener_mon,
+            {config_listener_mon, start_link, [?MODULE, nil]},
             permanent,
             5000,
             worker,
-            [couch_log_config_listener]
+            [config_listener_mon]
         }
     ].
+
+handle_config_change("log", Key, _, _, S) ->
+    case Key of
+        "level" ->
+            couch_log_config:reconfigure();
+        "max_message_size" ->
+            couch_log_config:reconfigure();
+        _ ->
+            % Someone may have changed the config for
+            % the writer so we need to re-initialize.
+            couch_log_server:reconfigure()
+    end,
+    notify_listeners(),
+    {ok, S};
+
+handle_config_change(_, _, _, _, S) ->
+    {ok, S}.
+
+handle_config_terminate(_Server, _Reason, _State) ->
+    ok.
+
+-ifdef(TEST).
+notify_listeners() ->
+    Listeners = application:get_env(couch_log, config_listeners, []),
+    lists:foreach(fun(L) ->
+        L ! couch_log_config_change_finished
+    end, Listeners).
+-else.
+notify_listeners() ->
+    ok.
+-endif.
diff --git a/test/couch_log_config_listener_test.erl b/test/couch_log_config_listener_test.erl
index e607164..e3680b8 100644
--- a/test/couch_log_config_listener_test.erl
+++ b/test/couch_log_config_listener_test.erl
@@ -29,12 +29,19 @@
 
 
 check_restart_listener() ->
-    ?assertNotEqual(not_found, get_handler()),
-    gen_event:delete_handler(config_event, get_handler(), testing),
-    ?assertEqual(not_found, get_handler()),
-    timer:sleep(1000),
-    ?assertNotEqual(not_found, get_handler()).
+    Listener1 = get_listener(),
+    ?assert(is_process_alive(Listener1)),
 
+    Handler1 = get_handler(),
+    ?assertNotEqual(not_found, Handler1),
+    ok = gen_event:delete_handler(config_event, get_handler(), testing),
+    ?assertEqual(not_found, get_handler()),
+
+    timer:sleep(100),
+    ?assertNot(is_process_alive(Listener1)),
+
+    ?assert(is_process_alive(get_listener())),
+    ok.
 
 check_ignore_non_log() ->
     Run = fun() ->
@@ -48,9 +55,13 @@
 
 get_handler() ->
     FoldFun = fun
-        ({config_listener, {couch_log_config_listener, _}} = H, not_found) ->
+        ({config_listener, {couch_log_sup, _}} = H, not_found) ->
             H;
         (_, Acc) ->
             Acc
     end,
     lists:foldl(FoldFun, not_found, gen_event:which_handlers(config_event)).
+
+get_listener() ->
+    Children = supervisor:which_children(couch_log_sup),
+    hd([Pid || {config_listener_mon, Pid, _, _} <- Children]).