From 1949357f0cbcc1e6c94add457c7cad90d35340c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 16 Nov 2018 12:54:34 +0100 Subject: Allow overriding cowboy_http's idle_timeout per request This allows requests that expect to run longer to do so without impacting the configuration of other requests. --- test/http_SUITE.erl | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) (limited to 'test/http_SUITE.erl') diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index b66b641..f330d58 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -32,7 +32,8 @@ groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}]. init_routes(_) -> [ {"localhost", [ {"/", hello_h, []}, - {"/echo/:key", echo_h, []} + {"/echo/:key", echo_h, []}, + {"/set_options/:key", set_options_h, []} ]} ]. @@ -65,11 +66,13 @@ idle_timeout_infinity(Config) -> doc("Ensure the idle_timeout option accepts the infinity value."), {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{ env => #{dispatch => cowboy_router:compile(init_routes(Config))}, - request_timeout => infinity + request_timeout => 500, + idle_timeout => infinity }), Port = ranch:get_port(name()), ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), - _ = gun:post(ConnPid, "/echo/read_body", [], <<"TEST">>), + _ = gun:post(ConnPid, "/echo/read_body", + [{<<"content-type">>, <<"text/plain">>}]), #{socket := Socket} = gun:info(ConnPid), Pid = get_remote_pid_tcp(Socket), Ref = erlang:monitor(process, Pid), @@ -98,6 +101,62 @@ request_timeout_infinity(Config) -> ok end. +set_options_idle_timeout(Config) -> + doc("Confirm that the idle_timeout option can be dynamically " + "set to change how long Cowboy will wait before it closes the connection."), + %% We start with a long timeout and then cut it short. + {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{ + env => #{dispatch => cowboy_router:compile(init_routes(Config))}, + idle_timeout => 60000 + }), + Port = ranch:get_port(name()), + ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), + _ = gun:post(ConnPid, "/set_options/idle_timeout_short", + [{<<"content-type">>, <<"text/plain">>}]), + #{socket := Socket} = gun:info(ConnPid), + Pid = get_remote_pid_tcp(Socket), + Ref = erlang:monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, _} -> + ok + after 2000 -> + error(timeout) + end. + +set_options_idle_timeout_only_applies_to_current_request(Config) -> + doc("Confirm that changes to the idle_timeout option only apply to the current stream."), + %% We start with a long timeout and then cut it short. + {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{ + env => #{dispatch => cowboy_router:compile(init_routes(Config))}, + idle_timeout => 500 + }), + Port = ranch:get_port(name()), + ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), + StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long", + [{<<"content-type">>, <<"text/plain">>}]), + #{socket := Socket} = gun:info(ConnPid), + Pid = get_remote_pid_tcp(Socket), + Ref = erlang:monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, Reason} -> + error(Reason) + after 2000 -> + ok + end, + %% Finish the first request and start a second one to confirm + %% the idle_timeout option is back to normal. + gun:data(ConnPid, StreamRef, fin, <<"Hello!">>), + {response, nofin, 200, _} = gun:await(ConnPid, StreamRef), + {ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef), + _ = gun:post(ConnPid, "/echo/read_body", + [{<<"content-type">>, <<"text/plain">>}]), + receive + {'DOWN', Ref, process, Pid, _} -> + ok + after 2000 -> + error(timeout) + end. + switch_protocol_flush(Config) -> doc("Confirm that switch_protocol does not flush unrelated messages."), ProtoOpts = #{ -- cgit v1.2.3