Now we store the tables when they are created

This commit is contained in:
Jonatan Pålsson 2011-02-17 12:12:12 +01:00
parent 391af1a96a
commit 818205e19b
3 changed files with 37 additions and 22 deletions

View file

@ -8,6 +8,11 @@
code_change/3]). code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-record(co_state,
{players = [], % List of all player processes
player_table_map = [], % Players <-> Table map
table_state_map = []}). % Table <-> Table state map
%% @doc This module act as "the man in the middle". %% @doc This module act as "the man in the middle".
%% Creates the starting connection between table and players. %% Creates the starting connection between table and players.
@ -48,13 +53,14 @@ remove_player(_From, _Player) ->
%% gen_server callbacks %% gen_server callbacks
init([]) -> init([]) ->
{ok, ok}. {ok, #co_state{}}.
handle_call({join_table, Table}, From, State) -> handle_call({join_table, Table}, From, State) ->
{reply, {error, no_such_table}, State}; {reply, {error, no_such_table}, State};
handle_call({create_table, {force, TID}}, From, State) -> handle_call({create_table, {force, TID}}, From, State) ->
{reply, {ok, TID}, State}; TIDs = State#co_state.player_table_map,
{reply, {ok, TID}, State#co_state{player_table_map = [TID | TIDs]}};
handle_call(_Message, _From, State) -> handle_call(_Message, _From, State) ->
{noreply, State}. {noreply, State}.

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa erlv8/ebin -pa ebin -pa src -eval 'ggs_protocol_test:test_parse().' erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa ebin -pa src -eval 'ggs_coordinator_test:test().'

View file

@ -1,33 +1,42 @@
-module(ggs_coordinator_test). -module(ggs_coordinator_test).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
start_link_test() -> coordinator_test_() ->
{ok, Coord} = ggs_coordinator:start_link(), % Start {foreach,
PInfo = erlang:process_info(Coord), % Check process info fun() ->
ggs_coordinator:stop(""), % Clean up {ok, _Coord} = ggs_coordinator:start_link(),
timer:sleep(100), % Wait for cleaning.. timer:sleep(100)
?assert((PInfo /= undefined)). % Did the server start? end,
fun(_X) ->
ggs_coordinator:stop("End of test"),
timer:sleep(100)
end,
[
fun test_start_link/0,
fun test_stop/0,
fun test_join_bad_table/0,
fun test_create_table/0
]
}.
stop_test() -> test_start_link() ->
% Check process info
PInfo = whereis(ggs_coordinator),
?assert((PInfo /= undefined)). % Did the server start?
test_stop() ->
ok = ggs_coordinator:stop(""), % Extra cleaning ok = ggs_coordinator:stop(""), % Extra cleaning
{ok, Coord} = ggs_coordinator:start_link(), % Start server timer:sleep(100),
ggs_coordinator:stop("Terminate now"), % Send stop message % Did it stop?
timer:sleep(100), % Wait for cleaning.. ?assert((whereis(ggs_coordinator)) == undefined).
?assert((erlang:process_info(Coord) == undefined)). % Did it stop?
join_bad_table_test() -> test_join_bad_table() ->
{ok, _Coord} = ggs_coordinator:start_link(),
Response = ggs_coordinator:join_table("Nonexistant table"), Response = ggs_coordinator:join_table("Nonexistant table"),
ggs_coordinator:stop(""),
timer:sleep(100),
?assert(Response == {error, no_such_table}). ?assert(Response == {error, no_such_table}).
create_table_test() -> test_create_table() ->
{ok, _Coord} = ggs_coordinator:start_link(),
% Forcibly create a table. This functionality should be disabled % Forcibly create a table. This functionality should be disabled
% in the production system, but is pretty nice for testing. % in the production system, but is pretty nice for testing.
Response = ggs_coordinator:create_table({force, 1337}), Response = ggs_coordinator:create_table({force, 1337}),
ggs_coordinator:stop(""),
timer:sleep(100),
?assert(Response == {ok, 1337}). ?assert(Response == {ok, 1337}).