couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iil...@apache.org
Subject [5/6] config commit: updated refs/heads/master to 47e5f70
Date Tue, 23 Aug 2016 22:04:26 GMT
Make sure we don't subscribe multiple times

Check there is no subscription from given Pid in subscribe_for_changes

COUCHDB-3102


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

Branch: refs/heads/master
Commit: f56890f3ac808c4b362647fe891ff1c2e2b71686
Parents: f09a2eb
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Fri Aug 19 15:44:55 2016 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Fri Aug 19 15:44:55 2016 -0700

----------------------------------------------------------------------
 src/config_notifier.erl | 13 +++++++++++--
 test/config_tests.erl   | 17 ++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/f56890f3/src/config_notifier.erl
----------------------------------------------------------------------
diff --git a/src/config_notifier.erl b/src/config_notifier.erl
index c1844e7..52fc4c0 100644
--- a/src/config_notifier.erl
+++ b/src/config_notifier.erl
@@ -33,8 +33,13 @@ subscribe(Subscription) ->
     subscribe(self(), Subscription).
 
 subscribe(Subscriber, Subscription) ->
-    gen_event:add_sup_handler(
-        config_event, {?MODULE, Subscriber}, {Subscriber, Subscription}).
+    case lists:member(Subscriber, handlers()) of
+        true ->
+            ok;
+        false ->
+            gen_event:add_sup_handler(
+                config_event, {?MODULE, Subscriber}, {Subscriber, Subscription})
+    end.
 
 init({Subscriber, Subscription}) ->
     {ok, {Subscriber, Subscription}}.
@@ -66,3 +71,7 @@ maybe_notify({config_change, Sec, Key, _, _} = Event, Subscriber, Subscription)
 
 should_notify(Sec, Key, Subscription) ->
     lists:any(fun(S) -> S =:= Sec orelse S =:= {Sec, Key} end, Subscription).
+
+handlers() ->
+    AllHandlers = gen_event:which_handlers(config_event),
+    [Id || {?MODULE, Id} <- AllHandlers].

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/f56890f3/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 854e20e..d3464e1 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -271,7 +271,8 @@ config_notifier_behaviour_test_() ->
                 {[{"section_foo", "key_bar"}], fun should_notify/2},
                 {["section_foo"], fun should_not_notify/2},
                 {[{"section_foo", "key_bar"}], fun should_not_notify/2},
-                {all, fun should_unsubscribe_when_subscriber_gone/2}
+                {all, fun should_unsubscribe_when_subscriber_gone/2},
+                {all, fun should_not_add_duplicate/2}
             ]
         }
     }.
@@ -585,6 +586,20 @@ should_unsubscribe_when_subscriber_gone(_Subscription, Pid) ->
         ok
     end).
 
+should_not_add_duplicate(_, _) ->
+    ?_test(begin
+        ?assertEqual(1, n_notifiers()), %% spawned from setup
+
+        ?assertMatch(ok, config:subscribe_for_changes(all)),
+
+        ?assertEqual(2, n_notifiers()),
+
+        ?assertMatch(ok, config:subscribe_for_changes(all)),
+
+        ?assertEqual(2, n_notifiers()),
+        ok
+    end).
+
 
 spawn_config_listener() ->
     Self = self(),


Mime
View raw message