Skip to content

Instantly share code, notes, and snippets.

@dalizard
Created April 13, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalizard/4636c9d35ee5ffe2a098909b896708fe to your computer and use it in GitHub Desktop.
Save dalizard/4636c9d35ee5ffe2a098909b896708fe to your computer and use it in GitHub Desktop.
-module(frequency_tests).
-include_lib("eunit/include/eunit.hrl").
-define(setup(F), {setup, fun start/0, fun stop/1, F}).
%%%
%%% TESTS DESCRIPTIONS
%%%
boot_up_test_() ->
{"Can be started with a registered name",
?setup(fun boot_up/1)}.
shutdown_test_() ->
{"Can be shutdown and unregistered",
?setup(fun shutdown/1)}.
allocation_test_() ->
[{"Can allocate new frequencies",
?setup(fun allocation/1)},
{"Throws an error when no frequencies are left",
?setup(fun no_frequencies_left/1)}].
deallocation_test_() ->
{"Can deallocate frequencies",
?setup(fun deallocation/1)}.
%%%
%%% SETUP FUNCTIONS
%%%
start() ->
frequency:start().
stop(Pid) ->
Pid ! {request, self(), stop},
timer:sleep(5).
%%%
%%% ACTUAL TESTS
%%%
boot_up(Pid) ->
[
?_assert(erlang:is_process_alive(Pid)),
?_assertEqual(Pid, whereis(frequency))
].
shutdown(Pid) ->
Pid ! {request, self(), stop},
[
?_assertNot(erlang:is_process_alive(Pid)),
?_assertEqual(undefined, whereis(frequency))
].
allocation(Pid) ->
Response = allocate(Pid),
?_assertEqual(10, Response).
deallocation(Pid) ->
allocate(Pid),
Response = deallocate(Pid, 10),
timer:sleep(5),
?_assertEqual(ok, Response).
no_frequencies_left(Pid) ->
[allocate(Pid) || _ <- lists:seq(0,5)],
Response = no_frequency(Pid),
timer:sleep(5),
?_assertEqual(ok, Response).
%%%
%%% HELPER FUNCTIONS
%%%
allocate(Pid) ->
Pid ! {request, self(), allocate},
receive
{reply, {ok, Frequency}} -> Frequency
end.
deallocate(Pid, Frequency) ->
Pid ! {request, self(), {deallocate, Frequency}},
receive
{reply, ok} -> ok
end.
no_frequency(Pid) ->
Pid ! {request, self(), allocate},
receive
{reply, {error, no_frequency}} -> ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment