Browse Source

Fix timing issues in shutdown test suite

juhlig 6 years ago
parent
commit
55d56a1108
1 changed files with 11 additions and 10 deletions
  1. 11 10
      test/shutdown_SUITE.erl

+ 11 - 10
test/shutdown_SUITE.erl

@@ -33,8 +33,7 @@ brutal_kill(_) ->
 		ranch_tcp, #{shutdown => brutal_kill},
 		echo_protocol, []),
 	Port = ranch:get_port(Name),
-	{ok, _} = gen_tcp:connect("localhost", Port, []),
-	receive after 100 -> ok end,
+	ok = do_connect_and_ping(Port),
 	ListenerSupChildren = supervisor:which_children(ListenerSup),
 	{_, ConnsSupSup, _, _} = lists:keyfind(ranch_conns_sup_sup, 1, ListenerSupChildren),
 	[Pid] = do_get_conn_pids(ConnsSupSup),
@@ -53,8 +52,7 @@ infinity(_) ->
 		ranch_tcp, #{shutdown => infinity},
 		echo_protocol, []),
 	Port = ranch:get_port(Name),
-	{ok, _} = gen_tcp:connect("localhost", Port, []),
-	receive after 100 -> ok end,
+	ok = do_connect_and_ping(Port),
 	ListenerSupChildren = supervisor:which_children(ListenerSup),
 	{_, ConnsSupSup, _, _} = lists:keyfind(ranch_conns_sup_sup, 1, ListenerSupChildren),
 	[Pid] = do_get_conn_pids(ConnsSupSup),
@@ -75,8 +73,7 @@ infinity_trap_exit(_) ->
 		ranch_tcp, #{shutdown => infinity},
 		trap_exit_protocol, []),
 	Port = ranch:get_port(Name),
-	{ok, _} = gen_tcp:connect("localhost", Port, []),
-	receive after 100 -> ok end,
+	ok = do_connect_and_ping(Port),
 	ListenerSupChildren = supervisor:which_children(ListenerSup),
 	{_, ConnsSupSup, _, _} = lists:keyfind(ranch_conns_sup_sup, 1, ListenerSupChildren),
 	[Pid] = do_get_conn_pids(ConnsSupSup),
@@ -104,8 +101,7 @@ timeout(_) ->
 		ranch_tcp, #{shutdown => 500},
 		echo_protocol, []),
 	Port = ranch:get_port(Name),
-	{ok, _} = gen_tcp:connect("localhost", Port, []),
-	receive after 100 -> ok end,
+	ok = do_connect_and_ping(Port),
 	ListenerSupChildren = supervisor:which_children(ListenerSup),
 	{_, ConnsSupSup, _, _} = lists:keyfind(ranch_conns_sup_sup, 1, ListenerSupChildren),
 	[Pid] = do_get_conn_pids(ConnsSupSup),
@@ -126,8 +122,7 @@ timeout_trap_exit(_) ->
 		ranch_tcp, #{shutdown => 500},
 		trap_exit_protocol, []),
 	Port = ranch:get_port(Name),
-	{ok, _} = gen_tcp:connect("localhost", Port, []),
-	receive after 100 -> ok end,
+	ok = do_connect_and_ping(Port),
 	ListenerSupChildren = supervisor:which_children(ListenerSup),
 	{_, ConnsSupSup, _, _} = lists:keyfind(ranch_conns_sup_sup, 1, ListenerSupChildren),
 	[Pid] = do_get_conn_pids(ConnsSupSup),
@@ -154,3 +149,9 @@ do_get_conn_pids(ConnsSupSup) ->
 	ConnChildren = lists:flatten(
 		[supervisor:which_children(ConnsSup) || ConnsSup <- ConnsSups]),
 	[ConnPid || {_, ConnPid, _, _} <- ConnChildren].
+
+do_connect_and_ping(Port) ->
+	{ok, Conn} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
+	ok = gen_tcp:send(Conn, <<"PING">>),
+	{ok, <<"PING">>} = gen_tcp:recv(Conn, 4, 1000),
+	ok.