Permalink
Browse files

Fixing test suite issue with accepting/connecting.

Because of a weird TCP stack behaviour it is possible that
connect returns with a socket before it is properly accepted on the
server side. On top of this if the listening socket is closed between
the connect returning and the accept really happening the connection
disappears on the server side. Like it never happened in the first place.
  • Loading branch information...
1 parent 42044e2 commit c71bd5d943fe179336bba6b6f0916313dadb8538 Tamas Nagy committed Nov 23, 2009
Showing with 12 additions and 7 deletions.
  1. +4 −4 test/lhttpc_manager_tests.erl
  2. +8 −3 test/socket_server.erl
@@ -74,10 +74,10 @@ one_socket() ->
many_sockets() ->
{LS, Socket1} = socket_server:open(),
{ok, Port} = inet:port(LS),
- socket_server:accept(LS),
- socket_server:accept(LS),
- Socket2 = socket_server:connect(Port),
- Socket3 = socket_server:connect(Port),
+ Pid2 = socket_server:accept(LS),
+ Pid3 = socket_server:accept(LS),
+ Socket2 = socket_server:connect(Pid2, Port),
+ Socket3 = socket_server:connect(Pid3, Port),
gen_tcp:close(LS),
give_away(Socket1, ?HOST, ?PORT, ?SSL),
give_away(Socket2, ?HOST, ?PORT, ?SSL),
View
@@ -27,18 +27,22 @@
%%% @author Oscar Hellström <oscar@erlang-consulting.com>
-module(socket_server).
--export([open/0, connect/1, listen/0, accept/1]).
+-export([open/0, connect/2, listen/0, accept/1]).
-export([do_accept/2]).
open() ->
{LS, Port} = listen(),
- accept(LS),
+ Pid = accept(LS),
{ok, Port} = inet:port(LS),
+ Pid ! {connecting, self()},
{ok, Socket} = gen_tcp:connect({127,0,0,1}, Port, [{active, false}]),
+ receive accepted -> ok end,
{LS, Socket}.
-connect(Port) ->
+connect(Pid, Port) ->
+ Pid ! {connecting, self()},
{ok, Socket} = gen_tcp:connect({127,0,0,1}, Port, [{active, false}]),
+ receive accepted -> ok end,
Socket.
listen() ->
@@ -54,4 +58,5 @@ accept(LS) ->
do_accept(LS, Parent) ->
erlang:send_after(50, Parent, in_accept),
{ok, S} = gen_tcp:accept(LS),
+ receive {connecting, Pid} -> Pid ! accepted end,
{error, closed} = gen_tcp:recv(S, 0).

0 comments on commit c71bd5d

Please sign in to comment.