From d2f367fba3a44101ccc333d83ab0c889cca2317b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 19 Nov 2018 09:29:21 +0100 Subject: Use try..after in tests that start their own listeners --- test/http2_SUITE.erl | 216 +++++++++++++++++++++++++++++---------------------- 1 file changed, 123 insertions(+), 93 deletions(-) (limited to 'test/http2_SUITE.erl') diff --git a/test/http2_SUITE.erl b/test/http2_SUITE.erl index ffed530..54a5c98 100644 --- a/test/http2_SUITE.erl +++ b/test/http2_SUITE.erl @@ -58,11 +58,14 @@ idle_timeout(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - timer:sleep(1000), - %% Receive a GOAWAY frame back with NO_ERROR. - {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000), - ok. + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + timer:sleep(1000), + %% Receive a GOAWAY frame back with NO_ERROR. + {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. idle_timeout_infinity(Config) -> doc("Ensure the idle_timeout option accepts the infinity value."), @@ -72,11 +75,14 @@ idle_timeout_infinity(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - timer:sleep(1000), - %% Don't receive a GOAWAY frame. - {error, timeout} = gen_tcp:recv(Socket, 17, 1000), - ok. + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + timer:sleep(1000), + %% Don't receive a GOAWAY frame. + {error, timeout} = gen_tcp:recv(Socket, 17, 1000) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. idle_timeout_reset_on_data(Config) -> doc("Terminate when the idle timeout is reached."), @@ -86,23 +92,26 @@ idle_timeout_reset_on_data(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - %% We wait a little, send a PING, receive a PING ack. - {error, timeout} = gen_tcp:recv(Socket, 17, 500), - ok = gen_tcp:send(Socket, cow_http2:ping(0)), - {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), - %% Again. - {error, timeout} = gen_tcp:recv(Socket, 17, 500), - ok = gen_tcp:send(Socket, cow_http2:ping(0)), - {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), - %% And one more time. - {error, timeout} = gen_tcp:recv(Socket, 17, 500), - ok = gen_tcp:send(Socket, cow_http2:ping(0)), - {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), - %% The connection goes away soon after we stop sending data. - timer:sleep(1000), - {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000), - ok. + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + %% We wait a little, send a PING, receive a PING ack. + {error, timeout} = gen_tcp:recv(Socket, 17, 500), + ok = gen_tcp:send(Socket, cow_http2:ping(0)), + {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), + %% Again. + {error, timeout} = gen_tcp:recv(Socket, 17, 500), + ok = gen_tcp:send(Socket, cow_http2:ping(0)), + {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), + %% And one more time. + {error, timeout} = gen_tcp:recv(Socket, 17, 500), + ok = gen_tcp:send(Socket, cow_http2:ping(0)), + {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), + %% The connection goes away soon after we stop sending data. + timer:sleep(1000), + {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. inactivity_timeout(Config) -> doc("Terminate when the inactivity timeout is reached."), @@ -112,11 +121,14 @@ inactivity_timeout(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - receive after 1000 -> ok end, - %% Receive a GOAWAY frame back with an INTERNAL_ERROR. - {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000), - ok. + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + receive after 1000 -> ok end, + %% Receive a GOAWAY frame back with an INTERNAL_ERROR. + {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. initial_connection_window_size(Config) -> doc("Confirm a WINDOW_UPDATE frame is sent when the configured " @@ -128,16 +140,19 @@ initial_connection_window_size(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]), - %% Send a valid preface. - ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]), - %% Receive the server preface. - {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000), - {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000), - %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000. - {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000), - ConfiguredSize = Size + 65535, - ok. + try + {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]), + %% Send a valid preface. + ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]), + %% Receive the server preface. + {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000), + {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000), + %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000. + {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000), + ConfiguredSize = Size + 65535 + after + cowboy:stop_listener(?FUNCTION_NAME) + end. max_frame_size_sent(Config) -> doc("Confirm that frames sent by Cowboy are limited in size " @@ -149,35 +164,38 @@ max_frame_size_sent(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000}, [{port, Port}|Config]), - %% Send a request with a 30000 bytes body. - {HeadersBlock, _} = cow_hpack:encode([ - {<<":method">>, <<"POST">>}, - {<<":scheme">>, <<"http">>}, - {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. - {<<":path">>, <<"/echo/read_body">>} - ]), - ok = gen_tcp:send(Socket, [ - cow_http2:headers(1, nofin, HeadersBlock), - cow_http2:data(1, nofin, <<0:16384/unit:8>>), - cow_http2:data(1, fin, <<0:13616/unit:8>>) - ]), - %% Receive a HEADERS frame as a response. - {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of - %% We received a WINDOW_UPDATE first. Skip it and the next. - {ok, <<4:24, 8:8, 0:40>>} -> - {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000), - gen_tcp:recv(Socket, 9, 1000); - Res -> - Res - end, - {ok, _} = gen_tcp:recv(Socket, SkipLen, 6000), - %% The DATA frames following must have lengths of 20000 - %% and then 10000 due to the limit. - {ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000), - {ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000), - %% Stop the listener. - cowboy:stop_listener(?FUNCTION_NAME). + try + {ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000}, + [{port, Port}|Config]), + %% Send a request with a 30000 bytes body. + {HeadersBlock, _} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/echo/read_body">>} + ]), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:16384/unit:8>>), + cow_http2:data(1, fin, <<0:13616/unit:8>>) + ]), + %% Receive a HEADERS frame as a response. + {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of + %% We received a WINDOW_UPDATE first. Skip it and the next. + {ok, <<4:24, 8:8, 0:40>>} -> + {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000), + gen_tcp:recv(Socket, 9, 1000); + Res -> + Res + end, + {ok, _} = gen_tcp:recv(Socket, SkipLen, 6000), + %% The DATA frames following must have lengths of 20000 + %% and then 10000 due to the limit. + {ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000), + {ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. preface_timeout_infinity(Config) -> doc("Ensure infinity for preface_timeout is accepted."), @@ -187,13 +205,17 @@ preface_timeout_infinity(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - Pid = get_remote_pid_tcp(Socket), - Ref = erlang:monitor(process, Pid), - receive - {'DOWN', Ref, process, Pid, Reason} -> - error(Reason) - after 1000 -> + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + Pid = get_remote_pid_tcp(Socket), + Ref = erlang:monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, Reason} -> + error(Reason) + after 1000 -> + ok + end + after cowboy:stop_listener(?FUNCTION_NAME) end. @@ -206,14 +228,18 @@ resp_iolist_body(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), - Ref = gun:get(ConnPid, "/resp_iolist_body"), - {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref), - {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders), - Len = binary_to_integer(BinLen), - {ok, RespBody} = gun:await_body(ConnPid, Ref), - Len = iolist_size(RespBody), - gun:close(ConnPid). + try + ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), + Ref = gun:get(ConnPid, "/resp_iolist_body"), + {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref), + {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders), + Len = binary_to_integer(BinLen), + {ok, RespBody} = gun:await_body(ConnPid, Ref), + Len = iolist_size(RespBody), + gun:close(ConnPid) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. settings_timeout_infinity(Config) -> doc("Ensure infinity for settings_timeout is accepted."), @@ -223,12 +249,16 @@ settings_timeout_infinity(Config) -> }, {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), Port = ranch:get_port(?FUNCTION_NAME), - {ok, Socket} = do_handshake([{port, Port}|Config]), - Pid = get_remote_pid_tcp(Socket), - Ref = erlang:monitor(process, Pid), - receive - {'DOWN', Ref, process, Pid, Reason} -> - error(Reason) - after 1000 -> + try + {ok, Socket} = do_handshake([{port, Port}|Config]), + Pid = get_remote_pid_tcp(Socket), + Ref = erlang:monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, Reason} -> + error(Reason) + after 1000 -> + ok + end + after cowboy:stop_listener(?FUNCTION_NAME) end. -- cgit v1.2.3