Permalink
Browse files

Added 'list bind' command test handling

  • Loading branch information...
1 parent 0e84335 commit 9f75fb8b862e16d2a713dcbd7294cbe4d6ad7807 Gianfranco Alongi committed Mar 11, 2012
Showing with 28 additions and 5 deletions.
  1. +12 −4 Two/Solution/src/server.erl
  2. +16 −1 Two/Solution/test/server_tests.erl
@@ -1,4 +1,5 @@
-module(server).
+-include("command.hrl").
-export([start/1,
stop/1]).
@@ -21,19 +22,26 @@ stop(CommandPort) ->
ok.
start({To,Ref},FilesDir,CommandPort) ->
- {ok,Sock} = gen_tcp:listen(CommandPort,[{active,false}]),
+ {ok,Sock} = gen_tcp:listen(CommandPort,[{active,false},{reuseaddr,true}]),
register(server,self()),
To ! {started,self(),Ref},
loop(FilesDir,Sock).
loop(_FilesDir,Sock) ->
{ok,Session} = gen_tcp:accept(Sock),
case gen_tcp:recv(Session,0) of
+ {error,closed} ->
+ gen_tcp:close(Session),
+ loop(_FilesDir,Sock);
{ok,"stop"} ->
gen_tcp:send(Session,"stopping"),
gen_tcp:close(Session),
gen_tcp:close(Sock);
- {error,closed} ->
- gen_tcp:close(Session),
- loop(_FilesDir,Sock)
+ {ok,Command} ->
+ Res = command:parse(Command),
+ case Res of
+ #binding{type = list} ->
+ gen_tcp:send(Session,"bindings: none"),
+ loop(_FilesDir,Sock)
+ end
end.
@@ -10,11 +10,26 @@ server_start_test() ->
assert_port_open(CommandPort),
?assertEqual(ok,server:stop(CommandPort)),
assert_port_closed(CommandPort).
+
+server_list_bind_command_test() ->
+ CommandPort = 50001,
+ Options = [{files,"./test/test_files/"},
+ {command_port,CommandPort}],
+ server:start(Options),
+ ?assertEqual("bindings: none",send_receive_command(CommandPort,"list bind")),
+ server:stop(CommandPort).
+
%% --------------------------------------------------
assert_port_open(Port) ->
{ok,Sock} = gen_tcp:connect("localhost",Port,[]),
gen_tcp:close(Sock).
assert_port_closed(Port) ->
- {error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
+ {error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
+
+send_receive_command(CommandPort,Command) ->
+ {ok,Sock} = gen_tcp:connect("localhost",CommandPort,[{active,false}]),
+ gen_tcp:send(Sock,Command),
+ {ok,Socket} = gen_tcp:recv(Sock,0),
+ Socket.

0 comments on commit 9f75fb8

Please sign in to comment.