Permalink
Browse files

Added binding creation and listing test

  • Loading branch information...
1 parent 9f75fb8 commit 30840d3a435ea493b0250cf36eea7de2fbea2f8f Gianfranco Alongi committed Mar 11, 2012
Showing with 36 additions and 9 deletions.
  1. +1 −1 Two/Solution/src/port.erl
  2. +23 −8 Two/Solution/src/server.erl
  3. +12 −0 Two/Solution/test/server_tests.erl
@@ -12,7 +12,7 @@
{error,compile_error} |
{error,no_handle_function_exported}).
open(_,Port,echo) ->
- {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
+ {ok,Sock} = gen_tcp:listen(Port,[{active,false},{reuseaddr,true}]),
{ok,#port{type = echo,
socket = Sock,
number = Port}};
@@ -3,6 +3,9 @@
-export([start/1,
stop/1]).
+-record(state,{bindings = [] :: [{string(),string()}]
+ }).
+
-spec(start([{atom(),string() | integer()}]) -> ok).
start(Options) ->
FilesDir = proplists:get_value(files,Options),
@@ -25,23 +28,35 @@ start({To,Ref},FilesDir,CommandPort) ->
{ok,Sock} = gen_tcp:listen(CommandPort,[{active,false},{reuseaddr,true}]),
register(server,self()),
To ! {started,self(),Ref},
- loop(FilesDir,Sock).
+ loop(FilesDir,Sock,#state{}).
-loop(_FilesDir,Sock) ->
+loop(_FilesDir,Sock,ServerState) ->
{ok,Session} = gen_tcp:accept(Sock),
case gen_tcp:recv(Session,0) of
{error,closed} ->
gen_tcp:close(Session),
- loop(_FilesDir,Sock);
+ loop(_FilesDir,Sock,ServerState);
{ok,"stop"} ->
gen_tcp:send(Session,"stopping"),
gen_tcp:close(Session),
gen_tcp:close(Sock);
{ok,Command} ->
Res = command:parse(Command),
- case Res of
- #binding{type = list} ->
- gen_tcp:send(Session,"bindings: none"),
- loop(_FilesDir,Sock)
- end
+ {Reply,NewServerState} = perform_command(Res,ServerState),
+ gen_tcp:send(Session,Reply),
+ loop(_FilesDir,Sock,NewServerState)
end.
+
+perform_command(#binding{type = list},#state{bindings = []} = State) ->
+ {"bindings: none",State};
+perform_command(#binding{type = list},#state{bindings = Bindings} = State) ->
+ {lists:foldl(
+ fun({Type,File},Res) -> Res++"\n "++Type++", "++File
+ end,
+ "bindings:",
+ Bindings),State};
+perform_command(#binding{type = bind,arguments = Args},State) ->
+ Type = proplists:get_value(type,Args),
+ File = proplists:get_value(file,Args),
+ Bindings = State#state.bindings,
+ {"binding created",State#state{bindings = [{Type,File}|Bindings]}}.
@@ -18,6 +18,18 @@ server_list_bind_command_test() ->
server:start(Options),
?assertEqual("bindings: none",send_receive_command(CommandPort,"list bind")),
server:stop(CommandPort).
+
+server_bind_type_file_test() ->
+ CommandPort = 50001,
+ Options = [{files,"./test/test_files/"},
+ {command_port,CommandPort}],
+ server:start(Options),
+ BindRes = send_receive_command(CommandPort,"bind testtype compilation_clean_module"),
+ ?assertEqual("binding created",BindRes),
+ ListRes = send_receive_command(CommandPort,"list bind"),
+ ?assertEqual("bindings:\n"
+ " testtype, compilation_clean_module",ListRes),
+ server:stop(CommandPort).
%% --------------------------------------------------

0 comments on commit 30840d3

Please sign in to comment.