http2_SUITE.erl 17 KB

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