Commits

Anonymous committed e2697e9

Try to avoid race condition in socket server.

Comments (0)

Files changed (1)

test/socket_server.erl

 -module(socket_server).
 
 -export([open/0, connect/1, listen/0, accept/1]).
--export([do_accept/1]).
+-export([do_accept/2]).
 
 open() ->
     {LS, Port} = listen(),
     {LS, Port}.
 
 accept(LS) ->
-    spawn_link(?MODULE, do_accept, [LS]).
+    Pid = spawn_link(?MODULE, do_accept, [LS, self()]),
+    receive in_accept -> ok end,
+    Pid.
 
-do_accept(LS) ->
+do_accept(LS, Parent) ->
+    erlang:send_after(50, Parent, in_accept),
     {ok, S} = gen_tcp:accept(LS),
     {error, closed} = gen_tcp:recv(S, 0).