Skip to content
This repository has been archived by the owner on Apr 23, 2018. It is now read-only.

Commit

Permalink
reorganising code and adding test for get_messages_from_spy
Browse files Browse the repository at this point in the history
  • Loading branch information
nruth committed Dec 6, 2010
1 parent 1676d7a commit a9897f8
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions nspy.erl
Expand Up @@ -3,16 +3,46 @@
-define (NODEBUG, true). -define (NODEBUG, true).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").


% API
% ===

% a test double / mock process to use as a non-replying endpoint (null object) for messages % a test double / mock process to use as a non-replying endpoint (null object) for messages
mock() -> new([]). mock() -> new([]).



% a test spy to wrap another process, monitoring the messages sent to that process and relaying them to the intended receiver % 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. % doesn't consider node failures or message synchrony, but you probably shouldn't be relying on that anyway.
wrapper(Target) -> new([Target]). 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) -> new(RelayMessagesTo) ->
spawn(nspy, spy, [[], 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) -> spy (Messages, RelayMessagesTo) ->
receive receive
{nspy_list_messages, ReplyTo} -> {nspy_list_messages, ReplyTo} ->
Expand All @@ -26,35 +56,32 @@ spy (Messages, RelayMessagesTo) ->
spy([Message | Messages], RelayMessagesTo) spy([Message | Messages], RelayMessagesTo)
end. end.






% ASSERTS
% =======

assert_message_received(Spy, Expected) -> assert_message_received(Spy, Expected) ->
Messages = get_messages_from_spy(Spy), Messages = get_messages_from_spy(Spy),
io:format("~n[SPY] expected ~p received ~p~n", [Expected, Messages]), io:format("~n[SPY] expected ~p received ~p~n", [Expected, Messages]),
MessageFound = lists:any(fun(Elem) -> Elem =:= Expected end, Messages), MessageFound = lists:any(fun(Elem) -> Elem =:= Expected end, Messages),
?assert(MessageFound). ?assert(MessageFound).


get_messages_from_spy(Spy) -> assert_message_received_success_test() ->
Spy ! {nspy_list_messages, self()}, Spy = nspy:mock(),
receive {nspy_messages, Messages} -> Messages end. 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.