diff --git a/src/ggs_coordinator.erl b/src/ggs_coordinator.erl index 2a5d509..c6e01d6 100644 --- a/src/ggs_coordinator.erl +++ b/src/ggs_coordinator.erl @@ -8,6 +8,11 @@ code_change/3]). -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". %% Creates the starting connection between table and players. @@ -48,13 +53,14 @@ remove_player(_From, _Player) -> %% gen_server callbacks init([]) -> - {ok, ok}. + {ok, #co_state{}}. handle_call({join_table, Table}, From, State) -> {reply, {error, no_such_table}, 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) -> {noreply, State}. diff --git a/start_test b/start_test index 5920151..76050c1 100755 --- a/start_test +++ b/start_test @@ -1,3 +1,3 @@ #!/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().' diff --git a/tests/ggs_coordinator_test.erl b/tests/ggs_coordinator_test.erl index ba05c95..ccc9256 100644 --- a/tests/ggs_coordinator_test.erl +++ b/tests/ggs_coordinator_test.erl @@ -1,33 +1,42 @@ -module(ggs_coordinator_test). -include_lib("eunit/include/eunit.hrl"). -start_link_test() -> - {ok, Coord} = ggs_coordinator:start_link(), % Start - PInfo = erlang:process_info(Coord), % Check process info - ggs_coordinator:stop(""), % Clean up - timer:sleep(100), % Wait for cleaning.. - ?assert((PInfo /= undefined)). % Did the server start? +coordinator_test_() -> + {foreach, + fun() -> + {ok, _Coord} = ggs_coordinator:start_link(), + timer:sleep(100) + 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, Coord} = ggs_coordinator:start_link(), % Start server - ggs_coordinator:stop("Terminate now"), % Send stop message - timer:sleep(100), % Wait for cleaning.. - ?assert((erlang:process_info(Coord) == undefined)). % Did it stop? + timer:sleep(100), + % Did it stop? + ?assert((whereis(ggs_coordinator)) == undefined). -join_bad_table_test() -> - {ok, _Coord} = ggs_coordinator:start_link(), +test_join_bad_table() -> Response = ggs_coordinator:join_table("Nonexistant table"), - ggs_coordinator:stop(""), - timer:sleep(100), ?assert(Response == {error, no_such_table}). -create_table_test() -> - {ok, _Coord} = ggs_coordinator:start_link(), +test_create_table() -> % Forcibly create a table. This functionality should be disabled % in the production system, but is pretty nice for testing. Response = ggs_coordinator:create_table({force, 1337}), - ggs_coordinator:stop(""), - timer:sleep(100), ?assert(Response == {ok, 1337}).