aboutsummaryrefslogtreecommitdiff
path: root/learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
commit5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch)
tree982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl
parent05ae56e5e89abf2993f84e6d52b250131f247c35 (diff)
downloaderlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip
wip
Diffstat (limited to 'learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl')
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl55
1 files changed, 55 insertions, 0 deletions
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl
new file mode 100644
index 0000000..3a327b7
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.1.0/test/pq_events_tests.erl
@@ -0,0 +1,55 @@
+-module(pq_events_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+-define(setup(Name, T), {setup, fun() -> start(Name) end, fun stop/1, fun T/1}).
+-define(setup(T), ?setup(make_ref(), T)).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% TESTS DESCRIPTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+events_start_stop_reg_test_() ->
+ {"The event handler can be reached, started and stopped by using the "
+ "player's name",
+ ?setup(can_contact)}.
+
+%%%%%%%%%%%%%%%%%%%%%%%
+%%% SETUP FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%
+start(Name) ->
+ application:start(regis),
+ {ok, Pid} = pq_events:start_link(Name),
+ unlink(Pid),
+ Name.
+
+stop(Name) ->
+ pq_events:stop(Name).
+
+%%%%%%%%%%%%%%%%%%%%
+%%% ACTUAL TESTS %%%
+%%%%%%%%%%%%%%%%%%%%
+can_contact(Name) ->
+ ok = pq_events:add_handler(Name, pq_events_handler, self()),
+ pq_events:notify(Name, hello),
+ L1 = flush(),
+ pq_events:delete_handler(Name, pq_events_handler, []),
+ pq_events:notify(Name, hello),
+ L2 = flush(),
+ [?_assertEqual([hello], L1),
+ ?_assertEqual([], L2)].
+
+%%%%%%%%%%%%%%%%%%%%%%%%
+%%% HELPER FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%
+flush() ->
+ receive
+ X -> [X | flush1()]
+ after 300 ->
+ []
+ end.
+
+flush1() ->
+ receive
+ X -> [X | flush1()]
+ after 0 ->
+ []
+ end.