Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: Gianfrancoalongi/TDD-Exercises
base: cbd188a9b6
...
head fork: Gianfrancoalongi/TDD-Exercises
compare: 5616a67ba2
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 13, 2012
Gianfranco Alongi Changed spawn to spawn_link and added send rcv test with echo 94d2b1b
Commits on Mar 14, 2012
Gianfranco Alongi Fixed race condition and added send-receive reverse test 5616a67
3  Two/Solution/src/port.erl
View
@@ -36,7 +36,8 @@ try_load(ModuleName,Module,ErlSource,Binary,Port) ->
false ->
{error,no_handle_function_exported};
true ->
- {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
+ {ok,Sock} = gen_tcp:listen(Port,[{reuseaddr,true},
+ {active,false}]),
{ok,#port{type = ModuleName,
socket = Sock,
number = Port}}
26 Two/Solution/src/server.erl
View
@@ -86,19 +86,25 @@ perform_command(#port_command{type = open,arguments = Args}, State) ->
[Port,Type] = pick([port,type],Args),
#state{bindings = Bindings, files_dir = Dir, ports = Ports} = State,
File = proplists:get_value(Type,Bindings),
- Pid = spawn(fun() ->
- {ok,PortRecord} = port:open(Dir,Port,File),
- port_loop(PortRecord)
- end),
+ Ref = make_ref(),
+ Self = self(),
+ Pid = spawn_link(fun() ->
+ {ok,PortRecord} = port:open(Dir,Port,File),
+ Self ! {opened,Ref,self()},
+ port_loop(PortRecord)
+ end),
+ receive
+ {opened,Ref,Pid} -> ok
+ end,
{"port opened",State#state{ports = [{Type,Port,Pid}|Ports]}}.
-
+
port_loop(#port{socket = Sock} = Port) ->
{ok,Active} = gen_tcp:accept(Sock),
- spawn(fun() ->
- {ok,Got} = gen_tcp:recv(Active,0),
- gen_tcp:send(Active,port:handle(Port,Got)),
- gen_tcp:close(Active)
- end),
+ spawn_link(fun() ->
+ {ok,Got} = gen_tcp:recv(Active,0),
+ gen_tcp:send(Active,port:handle(Port,Got)),
+ gen_tcp:close(Active)
+ end),
port_loop(Port).
28 Two/Solution/test/server_tests.erl
View
@@ -15,11 +15,14 @@ command_test_() ->
{foreach,
fun start/0,
fun stop/1,
- [fun list_bind/0,
+ [
+ fun list_bind/0,
fun bind_type/0,
fun unbind_type/0,
fun list_port/0,
- fun open_port/0
+ fun open_port/0,
+ fun open_echo_port_send/0,
+ fun open_reverse_port_send/0
]}.
list_bind() ->
@@ -49,8 +52,18 @@ open_port() ->
" ab, 50002\n"
" command-port, 50001",
send_receive_command("list port")).
+
+open_echo_port_send() ->
+ send_receive_command("bind ab echo"),
+ send_receive_command("open 50003 ab"),
+ ?assertEqual("this is echoed",send_receive_on_port(50003,"this is echoed")).
-
+open_reverse_port_send() ->
+ send_receive_command("bind rev reverse_module"),
+ send_receive_command("open 50010 rev"),
+ ?assertEqual("deohce si siht",send_receive_on_port(50010,"this is echoed")).
+
+
%% --------------------------------------------------
start() ->
Options = [{files,"./test/test_files/"},
@@ -69,7 +82,12 @@ assert_port_closed(Port) ->
{error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
send_receive_command(Command) ->
- {ok,Sock} = gen_tcp:connect("localhost",?COMMAND_PORT,[{active,false}]),
- gen_tcp:send(Sock,Command),
+ send_receive_on_port(?COMMAND_PORT,Command).
+
+send_receive_on_port(Port,Send) ->
+ {ok,Sock} = gen_tcp:connect("localhost",Port,[{active,false}]),
+ gen_tcp:send(Sock,Send),
{ok,Socket} = gen_tcp:recv(Sock,0),
+ gen_tcp:close(Sock),
Socket.
+
4 Two/Solution/test/test_files/reverse_module.erl
View
@@ -0,0 +1,4 @@
+-module(reverse_module).
+-export([handle/1]).
+handle(S) ->
+ lists:reverse(S).

No commit comments for this range

Something went wrong with that request. Please try again.