Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Fixing test suite issue with accepting/connecting.
Browse files Browse the repository at this point in the history
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
Tamas Nagy committed Nov 23, 2009
1 parent 42044e2 commit c71bd5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions test/lhttpc_manager_tests.erl
Expand Up @@ -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),
Expand Down
11 changes: 8 additions & 3 deletions test/socket_server.erl
Expand Up @@ -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() ->
Expand All @@ -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.