1. Fred T-H
  2. chut

Commits

Fred T-H  committed cfbc759

Added a HandlerId to messages returned from the listen handler

Adding the HandlerId to messages returned is necessary if we want to
be able to hold more than one client for a single user in a single
process. While there might be little practical uses for this at the
moment, it will be necessary in order to be able to listen to
listen handler exit messages on a per-user basis or in the case of
a thread pool, where the absence of a HandlerId could mean exit messages
targetting an older client could me misread as belonging to a newer one.

  • Participants
  • Parent commits 17e7f52
  • Branches default

Comments (0)

Files changed (3)

File src/client.erl

View file
  • Ignore whitespace
 -module(client).
--export([connect/1, disconnect/2, message/3, listen/1]).
+-export([connect/1, disconnect/2, message/3, listen/2]).
 
 -define(SESSION_LIFE, 15000).
 -define(LISTEN_TIMEOUT, 40000).
     usr:message(From, To, Message).
 
 %% picks messages belonging to Id
-listen(Id) ->
+listen(Id, HandlerId) ->
     receive
-        {Id,Msg} -> [Msg | listen1(Id)]
+        {{Id, HandlerId}, Msg} -> [Msg | listen1(Id, HandlerId)]
     after ?LISTEN_TIMEOUT ->
         []
     end.
 
 %% used to catch all messages currently lined up
-listen1(Id) ->
+listen1(Id, HandlerId) ->
     receive
-        {Id,Msg} -> [Msg | listen1(Id)]
+        {{Id, HandlerId}, Msg} -> [Msg | listen1(Id, HandlerId)]
     after 0 ->
         []
     end.

File src/usr_SUITE.erl

View file
  • Ignore whitespace
 %%--------------------------------------------------------------------
 all() -> 
     [usr_timeout, usr_subscribe, usr_message, usr_relay, usr_conv,
-     client_conv].
+     client_conv, two_clients_one_usr].
 %%--------------------------------------------------------------------
 %% Function: TestCase(Config0) ->
 %%               ok | exit() | {skip,Reason} | {comment,Comment} |
     ok.
 
 usr_subscribe(_Config) ->
+    process_flag(trap_exit, true), % only needed for the test to succeed?
     [Id, Sleep, Expected] = [id, 500, [{monitor, id}, id, {manager,id}]],
     IsProc = fun(Name) -> lists:member(Name, global:registered_names()) end,
     usr:start(Id, Sleep),
     {ok, Handler} = usr:subscribe(send_id),
     usr:message(send_id, invalid_id, hello),
     X = receive
-            {send_id, {sent, invalid_id, hello}} -> true
+            {{send_id, Handler}, {sent, invalid_id, hello}} -> true
         after 1000 -> throw("Message not sent")
         end,
     usr:unsubscribe(send_id, Handler),
     {ok, HandlerR} = usr:subscribe(rec),
     usr:message(sender, rec, hello),
     X = receive
-            {rec, {received, sender, hello}} -> true
+            {{rec,HandlerR}, {received, sender, hello}} -> true
         after 1000 -> throw("Message not received")
         end,
     usr:unsubscribe(sender, HandlerS),
     {ok, HandlerB} = usr:subscribe(B),
     usr:message(A,B,"Hello, B"),
     usr:message(B,A,"Oh, Hello, A!"),
-    receive {B, {received, A, "Hello, B"}} -> ok
+    receive {{B,HandlerB}, {received, A, "Hello, B"}} -> ok
     after 500 -> throw("Message not received")
     end,
-    receive {A, {sent, B, "Hello, B"}} -> ok
+    receive {{A,HandlerA}, {sent, B, "Hello, B"}} -> ok
     after 500 -> throw("Message not sent")
     end,
-    receive {A, {received, B, "Oh, Hello, A!"}} -> ok
+    receive {{A,HandlerA}, {received, B, "Oh, Hello, A!"}} -> ok
     after 500 -> throw("Message not received")
     end,
-    receive {B, {sent, A, "Oh, Hello, A!"}} -> ok
+    receive {{B,HandlerB}, {sent, A, "Oh, Hello, A!"}} -> ok
     after 500 -> throw ("Message not sent")
     end,
     usr:message(A,B, "Well, goodbye!"),
     usr:message(B,A, "Have a nice day..."),
-    receive {B, {received, A, "Well, goodbye!"}} -> ok
+    receive {{B,HandlerB}, {received, A, "Well, goodbye!"}} -> ok
     after 500 -> throw("Message not received")
     end,
-    receive {A, {sent, B, "Well, goodbye!"}} -> ok
+    receive {{A,HandlerA}, {sent, B, "Well, goodbye!"}} -> ok
     after 500 -> throw("Message not sent")
     end,
-    receive {A, {received, B, "Have a nice day..."}} -> ok
+    receive {{A,HandlerA}, {received, B, "Have a nice day..."}} -> ok
     after 500 -> throw("Message not received")
     end,
-    receive {B, {sent, A, "Have a nice day..."}} -> ok
+    receive {{B,HandlerB}, {sent, A, "Have a nice day..."}} -> ok
     after 500 -> throw ("Message not sent")
     end,
     usr:unsubscribe(A, HandlerA),
     {ok, ConnA} = client:connect(A),
     {ok, ConnB} = client:connect(B),
     client:message(A,B,"hi"),
-    [{sent, B, "hi"}] = client:listen(A),
+    [{sent, B, "hi"}] = client:listen(A,ConnA),
     client:message(A,B, "you there?"),
     timer:sleep(50), %% simulate delays to group messages
-    [{received, A, "hi"},{received, A, "you there?"}] = client:listen(B),
+    [{received, A, "hi"},{received, A, "you there?"}] = client:listen(B,ConnB),
     client:message(B,A, "stop harassing me!"),
     timer:sleep(50), %% simulate delays to group messages
     [{sent, B, "you there?"},
-     {received,B, "stop harassing me!"}] = client:listen(A),
-    [{sent, A, "stop harassing me!"}] = client:listen(B),
+     {received,B, "stop harassing me!"}] = client:listen(A,ConnA),
+    [{sent, A, "stop harassing me!"}] = client:listen(B,ConnB),
     client:disconnect(A,ConnA),
     client:disconnect(B,ConnB).
+
+%% tests two clients to the same user sharing a single process
+two_clients_one_usr(_Config) ->
+    A = make_ref(),
+    {ok, ConnA} = client:connect(A),
+    {ok, ConnB} = client:connect(A),
+    client:message(A,A,"hi"),
+    [{sent, A, "hi"},{received, A, "hi"}] = client:listen(A,ConnA),
+    [{sent, A, "hi"},{received, A, "hi"}] = client:listen(A,ConnB),
+    client:disconnect(A,ConnA),
+    client:disconnect(A,ConnB).

File src/usr_listen_handler.erl

View file
  • Ignore whitespace
 %% each installed event handler to handle the event.
 %%--------------------------------------------------------------------
 handle_event({received, From, Msg}, {UserId, SubscriberPid, HandlerId}) ->
-    SubscriberPid ! {UserId, {received, From, Msg}},
+    SubscriberPid ! {{UserId, HandlerId}, {received, From, Msg}},
     {ok, {UserId, SubscriberPid, HandlerId}};
 handle_event({send, To, Msg}, {UserId, SubscriberPid, HandlerId}) ->
-    SubscriberPid ! {UserId, {sent, To, Msg}},
+    SubscriberPid ! {{UserId, HandlerId}, {sent, To, Msg}},
     {ok, {UserId, SubscriberPid, HandlerId}};
 handle_event(_Event, State) ->
     {ok, State}.