diff options
author | Sergei Shuvatov <[email protected]> | 2022-11-22 10:57:36 +0300 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2023-12-12 15:05:01 +0100 |
commit | 3f5f326b732e3dbd1c335b854e78f5927f2f48fa (patch) | |
tree | c450fdd2aff2c75609ab9073a34a65441589745c /test/http_SUITE.erl | |
parent | 0ce9696e5ee301d0e6791dd514d14c44e23c725a (diff) | |
download | cowboy-3f5f326b732e3dbd1c335b854e78f5927f2f48fa.tar.gz cowboy-3f5f326b732e3dbd1c335b854e78f5927f2f48fa.tar.bz2 cowboy-3f5f326b732e3dbd1c335b854e78f5927f2f48fa.zip |
Add test for send_timeout_close
LH: I reworked the test a little and added the same test
for HTTP/2 so that both HTTP/1.1 and HTTP/2 get the issue
fixed.
Diffstat (limited to 'test/http_SUITE.erl')
-rw-r--r-- | test/http_SUITE.erl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index d0c92e4..28118a6 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -514,3 +514,63 @@ graceful_shutdown_listener(Config) -> %% Check that the 2nd (very slow) request is not handled. {error, {stream_error, closed}} = gun:await(ConnPid2, Ref2), gun:close(ConnPid2). + +send_timeout_close(_Config) -> + doc("Check that connections are closed on send timeout."), + TransOpts = #{ + port => 0, + socket_opts => [ + {send_timeout, 100}, + {send_timeout_close, true}, + {sndbuf, 10} + ] + }, + Dispatch = cowboy_router:compile([{"localhost", [ + {"/endless", loop_handler_endless_h, #{delay => 100}} + ]}]), + ProtoOpts = #{ + env => #{dispatch => Dispatch}, + idle_timeout => infinity + }, + {ok, _} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), + Port = ranch:get_port(?FUNCTION_NAME), + try + %% Connect a client that sends a request and waits indefinitely. + {ok, ClientSocket} = gen_tcp:connect("localhost", Port, + [{recbuf, 10}, {buffer, 10}, {active, false}, {packet, 0}]), + ok = gen_tcp:send(ClientSocket, [ + "GET /endless HTTP/1.1\r\n", + "Host: localhost:", integer_to_list(Port), "\r\n", + "x-test-pid: ", pid_to_list(self()), "\r\n\r\n" + ]), + %% Wait for the handler to start then get its pid, + %% the remote connection's pid and socket. + StreamPid = receive + {Self, StreamPid0, init} when Self =:= self() -> + StreamPid0 + after 1000 -> + error(timeout) + end, + ServerPid = ct_helper:get_remote_pid_tcp(ClientSocket), + {links, ServerLinks} = process_info(ServerPid, links), + [ServerSocket] = [PidOrPort || PidOrPort <- ServerLinks, is_port(PidOrPort)], + %% Poll the socket repeatedly until it is closed by the server. + WaitClosedFun = + fun F(T, Status) when T =< 0 -> + error({status, Status}); + F(T, _) -> + case prim_inet:getstatus(ServerSocket) of + {error, _} -> + ok; + {ok, Status} -> + Snooze = 100, + timer:sleep(Snooze), + F(T - Snooze, Status) + end + end, + ok = WaitClosedFun(2000, undefined), + false = erlang:is_process_alive(StreamPid), + false = erlang:is_process_alive(ServerPid) + after + cowboy:stop_listener(?FUNCTION_NAME) + end. |