Permalink
Browse files

Added deregister from pipe test

  • Loading branch information...
1 parent 68ed72d commit 17633e7061686f19f08ce7c4f66c20b39cdd4b37 Gianfranco Alongi committed Mar 21, 2012
Showing with 24 additions and 3 deletions.
  1. +14 −2 Three/Solution/src/pubsub_pipes.erl
  2. +10 −1 Three/Solution/test/pubsub_pipes_tests.erl
@@ -9,7 +9,8 @@
-export([new_pipe/1,
get_pipes/0,
subscribe_to_pipe/1,
- get_subscribers_to_pipe/1
+ get_subscribers_to_pipe/1,
+ unsubscribe_from_pipe/1
]).
-export([start_link/0,
stop/0
@@ -45,6 +46,10 @@ subscribe_to_pipe(PipeName) ->
get_subscribers_to_pipe(PipeName) ->
gen_server:call(?MODULE,{get_subscribers_to_pipe,PipeName}).
+-spec(unsubscribe_from_pipe(string()) -> ok).
+unsubscribe_from_pipe(PipeName) ->
+ gen_server:call(?MODULE,{unsubscribe_from_pipe,PipeName,self()}).
+
%%%===================================================================
init([]) ->
{ok, #state{pipes = ets:new(pipes,[set])}}.
@@ -69,7 +74,14 @@ handle_call({subscribe_to_pipe,PipeName,Pid},_From,State) ->
handle_call({get_subscribers_to_pipe,PipeName},_From,State) ->
[{PipeName,Subscribers}] = ets:lookup(State#state.pipes,PipeName),
- {reply,Subscribers,State}.
+ {reply,Subscribers,State};
+
+handle_call({unsubscribe_from_pipe,PipeName,Pid},_From,State) ->
+ [{PipeName,Subscribers}] = ets:lookup(State#state.pipes,PipeName),
+ Removed = [ X || X <- Subscribers, X =/= Pid],
+ ets:insert(State#state.pipes,[{PipeName,Removed}]),
+ {reply,ok,State}.
+
handle_cast(_Msg, State) ->
{noreply, State}.
@@ -7,7 +7,8 @@ pipe_test_() ->
fun cleanup/1,
[
fun create_pipe/0,
- fun register_on_pipe/0
+ fun register_on_pipe/0,
+ fun deregister_from_pipe/0
]}.
setup() ->
@@ -28,5 +29,13 @@ register_on_pipe() ->
ok = pubsub_pipes:new_pipe(PipeName),
ok = pubsub_pipes:subscribe_to_pipe(PipeName),
?assertEqual([Self],pubsub_pipes:get_subscribers_to_pipe(PipeName)).
+
+deregister_from_pipe() ->
+ PipeName = "A",
+ ok = pubsub_pipes:new_pipe(PipeName),
+ ok = pubsub_pipes:subscribe_to_pipe(PipeName),
+ ok = pubsub_pipes:unsubscribe_from_pipe(PipeName),
+ ?assertEqual([],pubsub_pipes:get_subscribers_to_pipe(PipeName)).
+

0 comments on commit 17633e7

Please sign in to comment.