Browse Source

Fix intermittent failures in sys_SUITE

Loïc Hoguin 5 years ago
parent
commit
e28a339ccd
1 changed files with 19 additions and 68 deletions
  1. 19 68
      test/sys_SUITE.erl

+ 19 - 68
test/sys_SUITE.erl

@@ -409,12 +409,7 @@ trap_exit_other_exit_h2(Config) ->
 		"'EXIT' messages from unknown processes."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	Pid ! {'EXIT', self(), {shutdown, ?MODULE}},
 	%% Send a HEADERS frame as a request.
@@ -532,17 +527,8 @@ sys_change_code_h2(Config) ->
 	doc("h2: The sys:change_code/4 function works as expected."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
-	%% Send a valid preface.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	%% Receive the server preface.
-	{ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
-	{ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
-	%% Send the SETTINGS ack.
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	%% Receive the SETTINGS ack.
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
 	%% Suspend the process and try to get a request in. The
 	%% response will not come back until we resume the process.
 	ok = sys:suspend(Pid),
@@ -818,17 +804,8 @@ sys_suspend_and_resume_h2(Config) ->
 	doc("h2: The sys:suspend/1 and sys:resume/1 functions work as expected."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
-	%% Send a valid preface.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	%% Receive the server preface.
-	{ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
-	{ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
-	%% Send the SETTINGS ack.
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	%% Receive the SETTINGS ack.
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
 	%% Suspend the process and try to get a request in. The
 	%% response will not come back until we resume the process.
 	ok = sys:suspend(Pid),
@@ -1009,12 +986,7 @@ supervisor_count_children_h2(Config) ->
 	doc("h2: The function supervisor:count_children/1 must work."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% No request was sent so there's no children.
 	Counts1 = supervisor:count_children(Pid),
@@ -1086,12 +1058,7 @@ supervisor_delete_child_not_found_h2(Config) ->
 	doc("h2: The function supervisor:delete_child/2 must return {error, not_found}."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% When no children exist.
 	{error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
@@ -1150,12 +1117,7 @@ supervisor_get_childspec_not_found_h2(Config) ->
 	doc("h2: The function supervisor:get_childspec/2 must return {error, not_found}."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% When no children exist.
 	{error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
@@ -1214,12 +1176,7 @@ supervisor_restart_child_not_found_h2(Config) ->
 	doc("h2: The function supervisor:restart_child/2 must return {error, not_found}."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% When no children exist.
 	{error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
@@ -1273,12 +1230,7 @@ supervisor_start_child_not_found_h2(Config) ->
 	doc("h2: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	{error, start_child_disabled} = supervisor:start_child(Pid, #{
 		id => error,
@@ -1332,12 +1284,7 @@ supervisor_terminate_child_not_found_h2(Config) ->
 	doc("h2: The function supervisor:terminate_child/2 must return {error, not_found}."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% When no children exist.
 	{error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
@@ -1400,12 +1347,7 @@ supervisor_which_children_h2(Config) ->
 	doc("h2: The function supervisor:which_children/1 must work."),
 	{ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
 		[{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-	%% Do the handshake.
-	ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
-	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
-	ok = ssl:send(Socket, cow_http2:settings_ack()),
-	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
-	timer:sleep(100),
+	do_http2_handshake(Socket),
 	Pid = get_remote_pid_tls(Socket),
 	%% No request was sent so there's no children.
 	[] = supervisor:which_children(Pid),
@@ -1442,3 +1384,12 @@ supervisor_which_children_ws(Config) ->
 	Pid = get_remote_pid_tcp(Socket),
 	[] = supervisor:which_children(Pid),
 	ok.
+
+%% Internal.
+
+do_http2_handshake(Socket) ->
+	ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"),
+	{ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
+	ok = ssl:send(Socket, [cow_http2:settings(#{}), cow_http2:settings_ack()]),
+	{ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
+	ok.