http2_SUITE.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. %% Copyright (c) 2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(http2_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [get_remote_pid_tcp/1]).
  20. -import(cowboy_test, [gun_open/1]).
  21. all() -> [{group, clear}].
  22. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  23. init_dispatch(_) ->
  24. cowboy_router:compile([{"localhost", [
  25. {"/", hello_h, []},
  26. {"/echo/:key", echo_h, []},
  27. {"/resp_iolist_body", resp_iolist_body_h, []}
  28. ]}]).
  29. %% Do a prior knowledge handshake (function originally copied from rfc7540_SUITE).
  30. do_handshake(Config) ->
  31. do_handshake(#{}, Config).
  32. do_handshake(Settings, Config) ->
  33. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  34. [binary, {active, false}|proplists:get_value(tcp_opts, Config, [])]),
  35. %% Send a valid preface.
  36. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(Settings)]),
  37. %% Receive the server preface.
  38. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  39. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  40. %% Send the SETTINGS ack.
  41. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  42. %% Receive the SETTINGS ack.
  43. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  44. {ok, Socket}.
  45. idle_timeout(Config) ->
  46. doc("Terminate when the idle timeout is reached."),
  47. ProtoOpts = #{
  48. env => #{dispatch => init_dispatch(Config)},
  49. idle_timeout => 1000
  50. },
  51. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  52. Port = ranch:get_port(?FUNCTION_NAME),
  53. try
  54. {ok, Socket} = do_handshake([{port, Port}|Config]),
  55. timer:sleep(1000),
  56. %% Receive a GOAWAY frame back with NO_ERROR.
  57. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  58. gen_tcp:close(Socket)
  59. after
  60. cowboy:stop_listener(?FUNCTION_NAME)
  61. end.
  62. idle_timeout_infinity(Config) ->
  63. doc("Ensure the idle_timeout option accepts the infinity value."),
  64. ProtoOpts = #{
  65. env => #{dispatch => init_dispatch(Config)},
  66. idle_timeout => infinity
  67. },
  68. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  69. Port = ranch:get_port(?FUNCTION_NAME),
  70. try
  71. {ok, Socket} = do_handshake([{port, Port}|Config]),
  72. timer:sleep(1000),
  73. %% Don't receive a GOAWAY frame.
  74. {error, timeout} = gen_tcp:recv(Socket, 17, 1000),
  75. gen_tcp:close(Socket)
  76. after
  77. cowboy:stop_listener(?FUNCTION_NAME)
  78. end.
  79. idle_timeout_reset_on_data(Config) ->
  80. doc("Terminate when the idle timeout is reached."),
  81. ProtoOpts = #{
  82. env => #{dispatch => init_dispatch(Config)},
  83. idle_timeout => 1000
  84. },
  85. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  86. Port = ranch:get_port(?FUNCTION_NAME),
  87. try
  88. {ok, Socket} = do_handshake([{port, Port}|Config]),
  89. %% We wait a little, send a PING, receive a PING ack.
  90. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  91. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  92. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  93. %% Again.
  94. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  95. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  96. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  97. %% And one more time.
  98. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  99. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  100. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  101. %% The connection goes away soon after we stop sending data.
  102. timer:sleep(1000),
  103. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  104. gen_tcp:close(Socket)
  105. after
  106. cowboy:stop_listener(?FUNCTION_NAME)
  107. end.
  108. inactivity_timeout(Config) ->
  109. doc("Terminate when the inactivity timeout is reached."),
  110. ProtoOpts = #{
  111. env => #{dispatch => init_dispatch(Config)},
  112. inactivity_timeout => 1000
  113. },
  114. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  115. Port = ranch:get_port(?FUNCTION_NAME),
  116. try
  117. {ok, Socket} = do_handshake([{port, Port}|Config]),
  118. receive after 1000 -> ok end,
  119. %% Receive a GOAWAY frame back with an INTERNAL_ERROR.
  120. {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  121. gen_tcp:close(Socket)
  122. after
  123. cowboy:stop_listener(?FUNCTION_NAME)
  124. end.
  125. initial_connection_window_size(Config) ->
  126. doc("Confirm a WINDOW_UPDATE frame is sent when the configured "
  127. "connection window is larger than the default."),
  128. ConfiguredSize = 100000,
  129. ProtoOpts = #{
  130. env => #{dispatch => init_dispatch(Config)},
  131. initial_connection_window_size => ConfiguredSize
  132. },
  133. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  134. Port = ranch:get_port(?FUNCTION_NAME),
  135. try
  136. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
  137. %% Send a valid preface.
  138. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  139. %% Receive the server preface.
  140. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  141. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  142. %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000.
  143. {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000),
  144. ConfiguredSize = Size + 65535,
  145. gen_tcp:close(Socket)
  146. after
  147. cowboy:stop_listener(?FUNCTION_NAME)
  148. end.
  149. max_frame_size_sent(Config) ->
  150. doc("Confirm that frames sent by Cowboy are limited in size "
  151. "by the max_frame_size_sent configuration value."),
  152. MaxFrameSize = 20000,
  153. ProtoOpts = #{
  154. env => #{dispatch => init_dispatch(Config)},
  155. max_frame_size_sent => MaxFrameSize
  156. },
  157. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  158. Port = ranch:get_port(?FUNCTION_NAME),
  159. try
  160. {ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000},
  161. [{port, Port}|Config]),
  162. %% Send a request with a 30000 bytes body.
  163. {HeadersBlock, _} = cow_hpack:encode([
  164. {<<":method">>, <<"POST">>},
  165. {<<":scheme">>, <<"http">>},
  166. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  167. {<<":path">>, <<"/echo/read_body">>}
  168. ]),
  169. ok = gen_tcp:send(Socket, [
  170. cow_http2:headers(1, nofin, HeadersBlock),
  171. cow_http2:data(1, nofin, <<0:16384/unit:8>>),
  172. cow_http2:data(1, fin, <<0:13616/unit:8>>)
  173. ]),
  174. %% Receive a HEADERS frame as a response.
  175. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of
  176. %% We received a WINDOW_UPDATE first. Skip it and the next.
  177. {ok, <<4:24, 8:8, 0:40>>} ->
  178. {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000),
  179. gen_tcp:recv(Socket, 9, 1000);
  180. Res ->
  181. Res
  182. end,
  183. {ok, _} = gen_tcp:recv(Socket, SkipLen, 6000),
  184. %% The DATA frames following must have lengths of 20000
  185. %% and then 10000 due to the limit.
  186. {ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000),
  187. {ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000),
  188. gen_tcp:close(Socket)
  189. after
  190. cowboy:stop_listener(?FUNCTION_NAME)
  191. end.
  192. persistent_term_router(Config) ->
  193. doc("The router can retrieve the routes from persistent_term storage."),
  194. case erlang:function_exported(persistent_term, get, 1) of
  195. true -> do_persistent_term_router(Config);
  196. false -> {skip, "This test uses the persistent_term functionality added in Erlang/OTP 21.2."}
  197. end.
  198. do_persistent_term_router(Config) ->
  199. persistent_term:put(?FUNCTION_NAME, init_dispatch(Config)),
  200. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  201. env => #{dispatch => {persistent_term, ?FUNCTION_NAME}}
  202. }),
  203. Port = ranch:get_port(?FUNCTION_NAME),
  204. try
  205. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  206. {ok, http2} = gun:await_up(ConnPid),
  207. StreamRef = gun:get(ConnPid, "/"),
  208. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
  209. gun:close(ConnPid)
  210. after
  211. cowboy:stop_listener(?FUNCTION_NAME)
  212. end.
  213. preface_timeout_infinity(Config) ->
  214. doc("Ensure infinity for preface_timeout is accepted."),
  215. ProtoOpts = #{
  216. env => #{dispatch => init_dispatch(Config)},
  217. preface_timeout => infinity
  218. },
  219. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  220. Port = ranch:get_port(?FUNCTION_NAME),
  221. try
  222. {ok, Socket} = do_handshake([{port, Port}|Config]),
  223. Pid = get_remote_pid_tcp(Socket),
  224. Ref = erlang:monitor(process, Pid),
  225. receive
  226. {'DOWN', Ref, process, Pid, Reason} ->
  227. error(Reason)
  228. after 1000 ->
  229. gen_tcp:close(Socket)
  230. end
  231. after
  232. cowboy:stop_listener(?FUNCTION_NAME)
  233. end.
  234. resp_iolist_body(Config) ->
  235. doc("Regression test when response bodies are iolists that "
  236. "include improper lists, empty lists and empty binaries. "
  237. "The original issue failed to split the body into frames properly."),
  238. ProtoOpts = #{
  239. env => #{dispatch => init_dispatch(Config)}
  240. },
  241. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  242. Port = ranch:get_port(?FUNCTION_NAME),
  243. try
  244. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  245. Ref = gun:get(ConnPid, "/resp_iolist_body"),
  246. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  247. {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  248. Len = binary_to_integer(BinLen),
  249. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  250. Len = iolist_size(RespBody),
  251. gun:close(ConnPid)
  252. after
  253. cowboy:stop_listener(?FUNCTION_NAME)
  254. end.
  255. settings_timeout_infinity(Config) ->
  256. doc("Ensure infinity for settings_timeout is accepted."),
  257. ProtoOpts = #{
  258. env => #{dispatch => init_dispatch(Config)},
  259. settings_timeout => infinity
  260. },
  261. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  262. Port = ranch:get_port(?FUNCTION_NAME),
  263. try
  264. {ok, Socket} = do_handshake([{port, Port}|Config]),
  265. Pid = get_remote_pid_tcp(Socket),
  266. Ref = erlang:monitor(process, Pid),
  267. receive
  268. {'DOWN', Ref, process, Pid, Reason} ->
  269. error(Reason)
  270. after 1000 ->
  271. gen_tcp:close(Socket)
  272. end
  273. after
  274. cowboy:stop_listener(?FUNCTION_NAME)
  275. end.
  276. graceful_shutdown_connection(Config) ->
  277. doc("Check that ongoing requests are handled before gracefully shutting down a connection."),
  278. Dispatch = cowboy_router:compile([{"localhost", [
  279. {"/delay_hello", delay_hello_h,
  280. #{delay => 500, notify_received => self()}}
  281. ]}]),
  282. ProtoOpts = #{
  283. env => #{dispatch => Dispatch}
  284. },
  285. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  286. Port = ranch:get_port(?FUNCTION_NAME),
  287. try
  288. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  289. Ref = gun:get(ConnPid, "/delay_hello"),
  290. %% Make sure the request is received.
  291. receive {request_received, <<"/delay_hello">>} -> ok end,
  292. %% Tell the connection to shutdown while the handler is working.
  293. [CowboyConnPid] = ranch:procs(?FUNCTION_NAME, connections),
  294. monitor(process, CowboyConnPid),
  295. ok = sys:terminate(CowboyConnPid, goaway),
  296. %% Check that the response is sent to the client before the
  297. %% connection goes down.
  298. {response, nofin, 200, _RespHeaders} = gun:await(ConnPid, Ref),
  299. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  300. <<"Hello world!">> = iolist_to_binary(RespBody),
  301. %% Check that the connection is gone soon afterwards. (The exit
  302. %% reason is supposed to be 'goaway' as passed to
  303. %% sys:terminate/2, but it is {shutdown, closed}.)
  304. receive
  305. {'DOWN', _, process, CowboyConnPid, _Reason} ->
  306. ok
  307. end,
  308. [] = ranch:procs(?FUNCTION_NAME, connections),
  309. gun:close(ConnPid)
  310. after
  311. cowboy:stop_listener(?FUNCTION_NAME)
  312. end.
  313. graceful_shutdown_timeout(Config) ->
  314. doc("Check that a connection is closed when gracefully shutting down times out."),
  315. Dispatch = cowboy_router:compile([{"localhost", [
  316. {"/long_delay_hello", delay_hello_h,
  317. #{delay => 10000, notify_received => self()}}
  318. ]}]),
  319. ProtoOpts = #{
  320. env => #{dispatch => Dispatch},
  321. goaway_initial_timeout => 200,
  322. goaway_complete_timeout => 500
  323. },
  324. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  325. Port = ranch:get_port(?FUNCTION_NAME),
  326. try
  327. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  328. Ref = gun:get(ConnPid, "/long_delay_hello"),
  329. %% Make sure the request is received.
  330. receive {request_received, <<"/long_delay_hello">>} -> ok end,
  331. %% Tell the connection to shutdown while the handler is working.
  332. [CowboyConnPid] = ranch:procs(?FUNCTION_NAME, connections),
  333. monitor(process, CowboyConnPid),
  334. ok = sys:terminate(CowboyConnPid, goaway),
  335. %% Check that connection didn't wait for the slow handler.
  336. {error, {stream_error, closed}} = gun:await(ConnPid, Ref),
  337. %% Check that the connection is gone. (The exit reason is
  338. %% supposed to be 'goaway' as passed to sys:terminate/2, but it
  339. %% is {shutdown, {stop, {exit, goaway}, 'Graceful shutdown timed
  340. %% out.'}}.)
  341. receive
  342. {'DOWN', _, process, CowboyConnPid, _Reason} ->
  343. ok
  344. after 100 ->
  345. error(still_alive)
  346. end,
  347. [] = ranch:procs(?FUNCTION_NAME, connections),
  348. gun:close(ConnPid)
  349. after
  350. cowboy:stop_listener(?FUNCTION_NAME)
  351. end.
  352. graceful_shutdown_listener(Config) ->
  353. doc("Check that connections are shut down gracefully when stopping a listener."),
  354. TransOpts = #{
  355. socket_opts => [{port, 0}],
  356. shutdown => 1000 %% Shorter timeout to make the test case faster.
  357. },
  358. Dispatch = cowboy_router:compile([{"localhost", [
  359. {"/delay_hello", delay_hello_h,
  360. #{delay => 500, notify_received => self()}}
  361. ]}]),
  362. ProtoOpts = #{
  363. env => #{dispatch => Dispatch}
  364. },
  365. {ok, Listener} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts),
  366. Port = ranch:get_port(?FUNCTION_NAME),
  367. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  368. Ref = gun:get(ConnPid, "/delay_hello"),
  369. %% Shutdown listener while the handlers are working.
  370. receive {request_received, <<"/delay_hello">>} -> ok end,
  371. ListenerMonitorRef = monitor(process, Listener),
  372. %% Note: This call does not complete quickly and will
  373. %% prevent other cowboy:stop_listener/1 calls to complete.
  374. ok = cowboy:stop_listener(?FUNCTION_NAME),
  375. receive
  376. {'DOWN', ListenerMonitorRef, process, Listener, _Reason} ->
  377. ok
  378. end,
  379. %% Check that the request is handled before shutting down.
  380. {response, nofin, 200, _RespHeaders} = gun:await(ConnPid, Ref),
  381. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  382. <<"Hello world!">> = iolist_to_binary(RespBody),
  383. gun:close(ConnPid).
  384. graceful_shutdown_listener_timeout(Config) ->
  385. doc("Check that connections are shut down when gracefully stopping a listener times out."),
  386. TransOpts = #{
  387. socket_opts => [{port, 0}],
  388. shutdown => 1000 %% Shorter timeout to make the test case faster.
  389. },
  390. Dispatch = cowboy_router:compile([{"localhost", [
  391. {"/long_delay_hello", delay_hello_h,
  392. #{delay => 10000, notify_received => self()}}
  393. ]}]),
  394. ProtoOpts = #{
  395. env => #{dispatch => Dispatch},
  396. goaway_initial_timeout => 200,
  397. goaway_complete_timeout => 500
  398. },
  399. {ok, Listener} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts),
  400. Port = ranch:get_port(?FUNCTION_NAME),
  401. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  402. Ref = gun:get(ConnPid, "/long_delay_hello"),
  403. %% Shutdown listener while the handlers are working.
  404. receive {request_received, <<"/long_delay_hello">>} -> ok end,
  405. ListenerMonitorRef = monitor(process, Listener),
  406. %% Note: This call does not complete quickly and will
  407. %% prevent other cowboy:stop_listener/1 calls to complete.
  408. ok = cowboy:stop_listener(?FUNCTION_NAME),
  409. receive
  410. {'DOWN', ListenerMonitorRef, process, Listener, _Reason} ->
  411. ok
  412. end,
  413. %% Check that the slow request is aborted.
  414. {error, {stream_error, closed}} = gun:await(ConnPid, Ref),
  415. gun:close(ConnPid).
  416. send_timeout_close(Config) ->
  417. doc("Check that connections are closed on send timeout."),
  418. TransOpts = #{
  419. port => 0,
  420. socket_opts => [
  421. {send_timeout, 100},
  422. {send_timeout_close, true},
  423. {sndbuf, 10}
  424. ]
  425. },
  426. Dispatch = cowboy_router:compile([{"localhost", [
  427. {"/endless", loop_handler_endless_h, #{delay => 100}}
  428. ]}]),
  429. ProtoOpts = #{
  430. env => #{dispatch => Dispatch},
  431. idle_timeout => infinity
  432. },
  433. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts),
  434. Port = ranch:get_port(?FUNCTION_NAME),
  435. try
  436. %% Connect a client that sends a request and waits indefinitely.
  437. {ok, ClientSocket} = do_handshake([{port, Port},
  438. {tcp_opts, [{recbuf, 10}, {buffer, 10}, {active, false}]}|Config]),
  439. {HeadersBlock, _} = cow_hpack:encode([
  440. {<<":method">>, <<"GET">>},
  441. {<<":scheme">>, <<"http">>},
  442. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  443. {<<":path">>, <<"/endless">>},
  444. {<<"x-test-pid">>, pid_to_list(self())}
  445. ]),
  446. ok = gen_tcp:send(ClientSocket, [
  447. cow_http2:headers(1, fin, HeadersBlock),
  448. %% Greatly increase the window to make sure we don't run
  449. %% out of space before we get send timeouts.
  450. cow_http2:window_update(10000000),
  451. cow_http2:window_update(1, 10000000)
  452. ]),
  453. %% Wait for the handler to start then get its pid,
  454. %% the remote connection's pid and socket.
  455. StreamPid = receive
  456. {Self, StreamPid0, init} when Self =:= self() ->
  457. StreamPid0
  458. after 1000 ->
  459. error(timeout)
  460. end,
  461. ServerPid = ct_helper:get_remote_pid_tcp(ClientSocket),
  462. {links, ServerLinks} = process_info(ServerPid, links),
  463. [ServerSocket] = [PidOrPort || PidOrPort <- ServerLinks, is_port(PidOrPort)],
  464. %% Poll the socket repeatedly until it is closed by the server.
  465. WaitClosedFun =
  466. fun F(T) when T =< 0 ->
  467. error({status, prim_inet:getstatus(ServerSocket)});
  468. F(T) ->
  469. Snooze = 100,
  470. case inet:sockname(ServerSocket) of
  471. {error, _} ->
  472. timer:sleep(Snooze);
  473. {ok, _} ->
  474. timer:sleep(Snooze),
  475. F(T - Snooze)
  476. end
  477. end,
  478. ok = WaitClosedFun(2000),
  479. false = erlang:is_process_alive(StreamPid),
  480. false = erlang:is_process_alive(ServerPid),
  481. gen_tcp:close(ClientSocket)
  482. after
  483. cowboy:stop_listener(?FUNCTION_NAME)
  484. end.