Created
April 13, 2017 14:56
-
-
Save dalizard/4636c9d35ee5ffe2a098909b896708fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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