Permalink
Browse files

reorganising code and adding test for get_messages_from_spy

  • Loading branch information...
1 parent 1676d7a commit a9897f8ba29f3184a0d14f871285a66dbe95a081 @nruth committed Dec 6, 2010
Showing with 50 additions and 23 deletions.
  1. +50 −23 nspy.erl
View
73 nspy.erl
@@ -3,16 +3,46 @@
-define (NODEBUG, true).
-include_lib("eunit/include/eunit.hrl").
+% API
+% ===
+
% a test double / mock process to use as a non-replying endpoint (null object) for messages
mock() -> new([]).
+
% a test spy to wrap another process, monitoring the messages sent to that process and relaying them to the intended receiver
% doesn't consider node failures or message synchrony, but you probably shouldn't be relying on that anyway.
wrapper(Target) -> new([Target]).
+ passthrough_wrapper_spy_test() ->
+ StuntDouble = nspy:mock(),
+ PassthroughSpy = nspy:wrapper(StuntDouble),
+ PassthroughSpy ! hi,
+ assert_message_received(PassthroughSpy, hi),
+ assert_message_received(StuntDouble, hi).
+
+
+
+%spawn a new spy process with no messages and a list of processes to forward messages to
new(RelayMessagesTo) ->
spawn(nspy, spy, [[], RelayMessagesTo]).
+
+
+% asks the spy to return its current messages received list
+% n.b. assume asynchronous messaging and use appropriate sleep periods to allow message delivery
+get_messages_from_spy(Spy) ->
+ Spy ! {nspy_list_messages, self()},
+ receive {nspy_messages, Messages} -> Messages end.
+
+ get_messages_from_spy_test() ->
+ Spy = spawn(nspy, spy, [[a_message], []]),
+ ?assertEqual(get_messages_from_spy(Spy), [a_message]). %or timeout failure
+
+
+% CORE
+% ====
+
spy (Messages, RelayMessagesTo) ->
receive
{nspy_list_messages, ReplyTo} ->
@@ -26,35 +56,32 @@ spy (Messages, RelayMessagesTo) ->
spy([Message | Messages], RelayMessagesTo)
end.
+
+
+
+
+% ASSERTS
+% =======
+
assert_message_received(Spy, Expected) ->
Messages = get_messages_from_spy(Spy),
io:format("~n[SPY] expected ~p received ~p~n", [Expected, Messages]),
MessageFound = lists:any(fun(Elem) -> Elem =:= Expected end, Messages),
?assert(MessageFound).
-get_messages_from_spy(Spy) ->
- Spy ! {nspy_list_messages, self()},
- receive {nspy_messages, Messages} -> Messages end.
+ assert_message_received_success_test() ->
+ Spy = nspy:mock(),
+ Spy ! hi,
+ Spy ! ho,
+ timer:sleep(200),
+ assert_message_received(Spy, hi),
+ assert_message_received(Spy, ho).
+
+ assert_message_received_failure_test() ->
+ Spy = nspy:mock(),
+ Spy ! hi,
+ timer:sleep(200),
+ ?assertError({assertion_failed, _}, assert_message_received(Spy, ho)).
-assert_message_received_success_test() ->
- Spy = nspy:mock(),
- Spy ! hi,
- Spy ! ho,
- timer:sleep(200),
- assert_message_received(Spy, hi),
- assert_message_received(Spy, ho).
-
-assert_message_received_failure_test() ->
- Spy = nspy:mock(),
- Spy ! hi,
- timer:sleep(200),
- ?assertError({assertion_failed, _}, assert_message_received(Spy, ho)).
-
-passthrough_spy_test() ->
- StuntDouble = nspy:mock(),
- PassthroughSpy = nspy:wrapper(StuntDouble),
- PassthroughSpy ! hi,
- assert_message_received(PassthroughSpy, hi),
- assert_message_received(StuntDouble, hi).

0 comments on commit a9897f8

Please sign in to comment.