Skip to content

Commit cc52a6f

Browse files
committed
Make the test fixture restart race-free
The eunit generators tear the fixture down and restart it back to back. stop/0 fired exit(kill) without waiting, so the next start/0 could race the dying process for the registered name and crash init/1 on register/2, surfacing as an opaque setup timeout on CI. stop/0 now blocks on the DOWN signal, and init/1 reports its crash reason to the caller instead of timing out silently.
1 parent 18d6f65 commit cc52a6f

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

test/buoy_http_server.erl

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ start() ->
2525
Pid = spawn(fun () -> init(Self) end),
2626
receive
2727
{Pid, started} ->
28-
{ok, Pid}
28+
{ok, Pid};
29+
{Pid, {error, Reason}} ->
30+
{error, Reason}
2931
after 5000 ->
3032
{error, timeout}
3133
end.
@@ -35,25 +37,35 @@ stop() ->
3537
undefined ->
3638
ok;
3739
Pid ->
40+
Ref = monitor(process, Pid),
3841
exit(Pid, kill),
39-
ok
42+
receive
43+
{'DOWN', Ref, process, Pid, _} ->
44+
ok
45+
end
4046
end.
4147

4248
%% private
4349
init(Parent) ->
44-
register(?MODULE, self()),
45-
persistent_term:put({?MODULE, connections}, counters:new(1, [])),
46-
{ok, _} = application:ensure_all_started(ssl),
47-
{ok, LSocket} = gen_tcp:listen(?PORT, ?LISTEN_OPTIONS),
48-
%% the default pkix_test_data key (secp112r2, sha1) is not
49-
%% negotiable by a modern TLS client
50-
KeyOpts = [{key, {namedCurve, secp256r1}}, {digest, sha256}],
51-
SslOptions = public_key:pkix_test_data(#{root => KeyOpts,
52-
peer => KeyOpts}),
53-
{ok, LSocketSsl} = ssl:listen(?PORT_SSL, ?LISTEN_OPTIONS ++ SslOptions),
54-
spawn_link(fun () -> accept_ssl(LSocketSsl) end),
55-
Parent ! {self(), started},
56-
accept(LSocket).
50+
try
51+
register(?MODULE, self()),
52+
persistent_term:put({?MODULE, connections}, counters:new(1, [])),
53+
{ok, _} = application:ensure_all_started(ssl),
54+
{ok, LSocket} = gen_tcp:listen(?PORT, ?LISTEN_OPTIONS),
55+
%% the default pkix_test_data key (secp112r2, sha1) is not
56+
%% negotiable by a modern TLS client
57+
KeyOpts = [{key, {namedCurve, secp256r1}}, {digest, sha256}],
58+
SslOptions = public_key:pkix_test_data(#{root => KeyOpts,
59+
peer => KeyOpts}),
60+
{ok, LSocketSsl} = ssl:listen(?PORT_SSL,
61+
?LISTEN_OPTIONS ++ SslOptions),
62+
spawn_link(fun () -> accept_ssl(LSocketSsl) end),
63+
Parent ! {self(), started},
64+
accept(LSocket)
65+
catch
66+
Class:Error:Stacktrace ->
67+
Parent ! {self(), {error, {Class, Error, Stacktrace}}}
68+
end.
5769

5870
accept(LSocket) ->
5971
{ok, Socket} = gen_tcp:accept(LSocket),

0 commit comments

Comments
 (0)