stream_handler_SUITE.erl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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(stream_handler_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [gun_down/1]).
  21. %% ct.
  22. all() ->
  23. cowboy_test:common_all().
  24. groups() ->
  25. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  26. init_per_group(Name = http, Config) ->
  27. cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config);
  28. init_per_group(Name = https, Config) ->
  29. cowboy_test:init_https(Name, #{stream_handlers => [stream_handler_h]}, Config);
  30. init_per_group(Name = h2, Config) ->
  31. cowboy_test:init_http2(Name, #{stream_handlers => [stream_handler_h]}, Config);
  32. init_per_group(Name = h2c, Config) ->
  33. Config1 = cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config),
  34. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  35. init_per_group(Name = http_compress, Config) ->
  36. cowboy_test:init_http(Name, #{
  37. stream_handlers => [cowboy_compress_h, stream_handler_h]
  38. }, Config);
  39. init_per_group(Name = https_compress, Config) ->
  40. cowboy_test:init_https(Name, #{
  41. stream_handlers => [cowboy_compress_h, stream_handler_h]
  42. }, Config);
  43. init_per_group(Name = h2_compress, Config) ->
  44. cowboy_test:init_http2(Name, #{
  45. stream_handlers => [cowboy_compress_h, stream_handler_h]
  46. }, Config);
  47. init_per_group(Name = h2c_compress, Config) ->
  48. Config1 = cowboy_test:init_http(Name, #{
  49. stream_handlers => [cowboy_compress_h, stream_handler_h]
  50. }, Config),
  51. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  52. end_per_group(Name, _) ->
  53. cowboy:stop_listener(Name).
  54. %% Tests.
  55. crash_in_init(Config) ->
  56. doc("Confirm an error is sent when a stream handler crashes in init/3."),
  57. Self = self(),
  58. ConnPid = gun_open(Config),
  59. Ref = gun:get(ConnPid, "/long_polling", [
  60. {<<"accept-encoding">>, <<"gzip">>},
  61. {<<"x-test-case">>, <<"crash_in_init">>},
  62. {<<"x-test-pid">>, pid_to_list(Self)}
  63. ]),
  64. %% Confirm init/3 is called.
  65. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  66. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  67. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  68. %% Receive a 500 error response.
  69. case gun:await(ConnPid, Ref) of
  70. {response, fin, 500, _} -> ok;
  71. {error, {stream_error, internal_error, _}} -> ok
  72. end.
  73. crash_in_data(Config) ->
  74. doc("Confirm an error is sent when a stream handler crashes in data/4."),
  75. Self = self(),
  76. ConnPid = gun_open(Config),
  77. Ref = gun:post(ConnPid, "/long_polling", [
  78. {<<"accept-encoding">>, <<"gzip">>},
  79. {<<"content-length">>, <<"6">>},
  80. {<<"x-test-case">>, <<"crash_in_data">>},
  81. {<<"x-test-pid">>, pid_to_list(Self)}
  82. ]),
  83. %% Confirm init/3 is called.
  84. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  85. %% Send data to make the stream handler crash.
  86. gun:data(ConnPid, Ref, fin, <<"Hello!">>),
  87. %% Confirm terminate/3 is called, indicating the stream ended.
  88. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  89. %% Receive a 500 error response.
  90. case gun:await(ConnPid, Ref) of
  91. {response, fin, 500, _} -> ok;
  92. {error, {stream_error, internal_error, _}} -> ok
  93. end.
  94. crash_in_info(Config) ->
  95. doc("Confirm an error is sent when a stream handler crashes in info/3."),
  96. Self = self(),
  97. ConnPid = gun_open(Config),
  98. Ref = gun:get(ConnPid, "/long_polling", [
  99. {<<"accept-encoding">>, <<"gzip">>},
  100. {<<"x-test-case">>, <<"crash_in_info">>},
  101. {<<"x-test-pid">>, pid_to_list(Self)}
  102. ]),
  103. %% Confirm init/3 is called.
  104. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  105. %% Send a message to make the stream handler crash.
  106. Pid ! {{Pid, 1}, crash},
  107. %% Confirm terminate/3 is called, indicating the stream ended.
  108. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  109. %% Receive a 500 error response.
  110. case gun:await(ConnPid, Ref) of
  111. {response, fin, 500, _} -> ok;
  112. {error, {stream_error, internal_error, _}} -> ok
  113. end.
  114. crash_in_terminate(Config) ->
  115. doc("Confirm the state is correct when a stream handler crashes in terminate/3."),
  116. Self = self(),
  117. ConnPid = gun_open(Config),
  118. %% Do a first request.
  119. Ref1 = gun:get(ConnPid, "/hello_world", [
  120. {<<"accept-encoding">>, <<"gzip">>},
  121. {<<"x-test-case">>, <<"crash_in_terminate">>},
  122. {<<"x-test-pid">>, pid_to_list(Self)}
  123. ]),
  124. %% Confirm init/3 is called.
  125. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  126. %% Confirm terminate/3 is called.
  127. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  128. %% Receive the response.
  129. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  130. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
  131. %% Do a second request to make sure the connection state is still good.
  132. Ref2 = gun:get(ConnPid, "/hello_world", [
  133. {<<"accept-encoding">>, <<"gzip">>},
  134. {<<"x-test-case">>, <<"crash_in_terminate">>},
  135. {<<"x-test-pid">>, pid_to_list(Self)}
  136. ]),
  137. %% Confirm init/3 is called. The pid shouldn't change.
  138. receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end,
  139. %% Confirm terminate/3 is called.
  140. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  141. %% Receive the second response.
  142. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  143. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref2),
  144. ok.
  145. crash_in_early_error(Config) ->
  146. case config(protocol, Config) of
  147. http -> do_crash_in_early_error(Config);
  148. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  149. end.
  150. do_crash_in_early_error(Config) ->
  151. doc("Confirm an error is sent when a stream handler crashes in early_error/5."
  152. "The connection is kept open by Cowboy."),
  153. Self = self(),
  154. ConnPid = gun_open(Config),
  155. Ref1 = gun:get(ConnPid, "/long_polling", [
  156. {<<"accept-encoding">>, <<"gzip">>},
  157. {<<"x-test-case">>, <<"crash_in_early_error">>},
  158. {<<"x-test-pid">>, pid_to_list(Self)}
  159. ]),
  160. %% Confirm init/3 is called.
  161. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  162. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  163. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  164. %% Confirm early_error/5 is called.
  165. receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  166. %% Receive a 500 error response.
  167. {response, fin, 500, _} = gun:await(ConnPid, Ref1),
  168. %% This error is not fatal. We should be able to repeat it on the same connection.
  169. Ref2 = gun:get(ConnPid, "/long_polling", [
  170. {<<"accept-encoding">>, <<"gzip">>},
  171. {<<"x-test-case">>, <<"crash_in_early_error">>},
  172. {<<"x-test-pid">>, pid_to_list(Self)}
  173. ]),
  174. %% Confirm init/3 is called.
  175. receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end,
  176. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  177. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  178. %% Confirm early_error/5 is called.
  179. receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  180. %% Receive a 500 error response.
  181. {response, fin, 500, _} = gun:await(ConnPid, Ref2),
  182. ok.
  183. crash_in_early_error_fatal(Config) ->
  184. case config(protocol, Config) of
  185. http -> do_crash_in_early_error_fatal(Config);
  186. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  187. end.
  188. do_crash_in_early_error_fatal(Config) ->
  189. doc("Confirm an error is sent when a stream handler crashes in early_error/5."
  190. "The error was fatal and the connection is closed by Cowboy."),
  191. Self = self(),
  192. ConnPid = gun_open(Config),
  193. Ref = gun:get(ConnPid, "/long_polling", [
  194. {<<"accept-encoding">>, <<"gzip">>},
  195. {<<"host">>, <<"host:port">>},
  196. {<<"x-test-case">>, <<"crash_in_early_error_fatal">>},
  197. {<<"x-test-pid">>, pid_to_list(Self)}
  198. ]),
  199. %% Confirm init/3 is NOT called. The error occurs before we reach this step.
  200. receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end,
  201. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  202. receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  203. %% Confirm early_error/5 is called.
  204. receive {Self, _, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  205. %% Receive a 400 error response. We do not send a 500 when
  206. %% early_error/5 crashes, we send the original error.
  207. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  208. %% Confirm the connection gets closed.
  209. gun_down(ConnPid).
  210. set_options_ignore_unknown(Config) ->
  211. doc("Confirm that unknown options are ignored when using the set_options commands."),
  212. Self = self(),
  213. ConnPid = gun_open(Config),
  214. Ref = gun:get(ConnPid, "/long_polling", [
  215. {<<"accept-encoding">>, <<"gzip">>},
  216. {<<"x-test-case">>, <<"set_options_ignore_unknown">>},
  217. {<<"x-test-pid">>, pid_to_list(Self)}
  218. ]),
  219. %% Confirm init/3 is called.
  220. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  221. %% Confirm terminate/3 is called, indicating the stream ended.
  222. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  223. %% Confirm the response is sent.
  224. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  225. {ok, _} = gun:await_body(ConnPid, Ref),
  226. ok.
  227. shutdown_on_stream_stop(Config) ->
  228. doc("Confirm supervised processes are shutdown when stopping the stream."),
  229. Self = self(),
  230. ConnPid = gun_open(Config),
  231. Ref = gun:get(ConnPid, "/long_polling", [
  232. {<<"accept-encoding">>, <<"gzip">>},
  233. {<<"x-test-case">>, <<"shutdown_on_stream_stop">>},
  234. {<<"x-test-pid">>, pid_to_list(Self)}
  235. ]),
  236. %% Confirm init/3 is called.
  237. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  238. %% Receive the pid of the newly started process and monitor it.
  239. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  240. MRef = monitor(process, Spawn),
  241. Spawn ! {Self, ready},
  242. %% Confirm terminate/3 is called, indicating the stream ended.
  243. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  244. %% We should receive a DOWN message soon after (or before) because the stream
  245. %% handler is stopping the stream immediately after the process started.
  246. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  247. %% The response is still sent.
  248. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  249. {ok, _} = gun:await_body(ConnPid, Ref),
  250. ok.
  251. shutdown_on_socket_close(Config) ->
  252. doc("Confirm supervised processes are shutdown when the socket closes."),
  253. Self = self(),
  254. ConnPid = gun_open(Config),
  255. _ = gun:get(ConnPid, "/long_polling", [
  256. {<<"accept-encoding">>, <<"gzip">>},
  257. {<<"x-test-case">>, <<"shutdown_on_socket_close">>},
  258. {<<"x-test-pid">>, pid_to_list(Self)}
  259. ]),
  260. %% Confirm init/3 is called.
  261. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  262. %% Receive the pid of the newly started process and monitor it.
  263. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  264. MRef = monitor(process, Spawn),
  265. Spawn ! {Self, ready},
  266. %% Close the socket.
  267. ok = gun:close(ConnPid),
  268. %% Confirm terminate/3 is called, indicating the stream ended.
  269. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  270. %% Confirm we receive a DOWN message for the child process.
  271. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  272. ok.
  273. shutdown_timeout_on_stream_stop(Config) ->
  274. doc("Confirm supervised processes are killed "
  275. "when the shutdown timeout triggers after stopping the stream."),
  276. Self = self(),
  277. ConnPid = gun_open(Config),
  278. Ref = gun:get(ConnPid, "/long_polling", [
  279. {<<"accept-encoding">>, <<"gzip">>},
  280. {<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>},
  281. {<<"x-test-pid">>, pid_to_list(Self)}
  282. ]),
  283. %% Confirm init/3 is called.
  284. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  285. %% Receive the pid of the newly started process and monitor it.
  286. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  287. MRef = monitor(process, Spawn),
  288. Spawn ! {Self, ready},
  289. %% Confirm terminate/3 is called, indicating the stream ended.
  290. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  291. %% We should NOT receive a DOWN message immediately.
  292. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  293. %% We should received it now.
  294. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  295. %% The response is still sent.
  296. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  297. {ok, _} = gun:await_body(ConnPid, Ref),
  298. ok.
  299. shutdown_timeout_on_socket_close(Config) ->
  300. doc("Confirm supervised processes are killed "
  301. "when the shutdown timeout triggers after the socket has closed."),
  302. Self = self(),
  303. ConnPid = gun_open(Config),
  304. _ = gun:get(ConnPid, "/long_polling", [
  305. {<<"accept-encoding">>, <<"gzip">>},
  306. {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>},
  307. {<<"x-test-pid">>, pid_to_list(Self)}
  308. ]),
  309. %% Confirm init/3 is called.
  310. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  311. %% Receive the pid of the newly started process and monitor it.
  312. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  313. MRef = monitor(process, Spawn),
  314. Spawn ! {Self, ready},
  315. %% Close the socket.
  316. ok = gun:close(ConnPid),
  317. %% Confirm terminate/3 is called, indicating the stream ended.
  318. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  319. %% We should NOT receive a DOWN message immediately.
  320. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  321. %% We should received it now.
  322. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  323. ok.
  324. switch_protocol_after_headers(Config) ->
  325. case config(protocol, Config) of
  326. http -> do_switch_protocol_after_response(
  327. <<"switch_protocol_after_headers">>, Config);
  328. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  329. end.
  330. switch_protocol_after_headers_data(Config) ->
  331. case config(protocol, Config) of
  332. http -> do_switch_protocol_after_response(
  333. <<"switch_protocol_after_headers_data">>, Config);
  334. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  335. end.
  336. switch_protocol_after_response(Config) ->
  337. case config(protocol, Config) of
  338. http -> do_switch_protocol_after_response(
  339. <<"switch_protocol_after_response">>, Config);
  340. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  341. end.
  342. do_switch_protocol_after_response(TestCase, Config) ->
  343. doc("The 101 informational response must not be sent when a response "
  344. "has already been sent before the switch_protocol is returned."),
  345. Self = self(),
  346. ConnPid = gun_open(Config),
  347. Ref = gun:get(ConnPid, "/long_polling", [
  348. {<<"accept-encoding">>, <<"gzip">>},
  349. {<<"x-test-case">>, TestCase},
  350. {<<"x-test-pid">>, pid_to_list(Self)}
  351. ]),
  352. %% Confirm init/3 is called and receive the response.
  353. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  354. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  355. Gzipped =
  356. lists:keyfind(<<"content-encoding">>, 1, Headers)
  357. =:= {<<"content-encoding">>, <<"gzip">>},
  358. case TestCase of
  359. <<"switch_protocol_after_headers">> ->
  360. ok;
  361. _ ->
  362. <<"{}">> = case gun:await_body(ConnPid, Ref) of
  363. {ok, Body} when Gzipped ->
  364. zlib:gunzip(Body);
  365. {ok, Body} ->
  366. Body
  367. end,
  368. ok
  369. end,
  370. {error, _} = gun:await(ConnPid, Ref),
  371. %% Confirm terminate/3 is called.
  372. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  373. %% Confirm takeover/7 is called.
  374. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  375. ok.
  376. terminate_on_socket_close(Config) ->
  377. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  378. Self = self(),
  379. ConnPid = gun_open(Config),
  380. Ref = gun:get(ConnPid, "/long_polling", [
  381. {<<"accept-encoding">>, <<"gzip">>},
  382. {<<"x-test-case">>, <<"terminate_on_socket_close">>},
  383. {<<"x-test-pid">>, pid_to_list(Self)}
  384. ]),
  385. %% Confirm init/3 is called and receive the beginning of the response.
  386. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  387. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  388. %% Close the socket.
  389. ok = gun:close(ConnPid),
  390. %% Confirm terminate/3 is called.
  391. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  392. ok.
  393. terminate_on_stop(Config) ->
  394. doc("Confirm terminate/3 is called after stop is returned."),
  395. Self = self(),
  396. ConnPid = gun_open(Config),
  397. Ref = gun:get(ConnPid, "/long_polling", [
  398. {<<"accept-encoding">>, <<"gzip">>},
  399. {<<"x-test-case">>, <<"terminate_on_stop">>},
  400. {<<"x-test-pid">>, pid_to_list(Self)}
  401. ]),
  402. %% Confirm init/3 is called and receive the response.
  403. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  404. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  405. %% Confirm the stream is still alive even though we
  406. %% received the response fully, and tell it to stop.
  407. Pid ! {{Pid, 1}, please_stop},
  408. receive {Self, Pid, info, _, please_stop, _} -> ok after 1000 -> error(timeout) end,
  409. %% Confirm terminate/3 is called.
  410. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  411. ok.
  412. terminate_on_switch_protocol(Config) ->
  413. case config(protocol, Config) of
  414. http -> do_terminate_on_switch_protocol(Config);
  415. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  416. end.
  417. do_terminate_on_switch_protocol(Config) ->
  418. doc("Confirm terminate/3 is called after switch_protocol is returned."),
  419. Self = self(),
  420. ConnPid = gun_open(Config),
  421. Ref = gun:get(ConnPid, "/long_polling", [
  422. {<<"accept-encoding">>, <<"gzip">>},
  423. {<<"x-test-case">>, <<"terminate_on_switch_protocol">>},
  424. {<<"x-test-pid">>, pid_to_list(Self)}
  425. ]),
  426. %% Confirm init/3 is called and receive the response.
  427. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  428. {inform, 101, _} = gun:await(ConnPid, Ref),
  429. %% Confirm terminate/3 is called.
  430. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  431. %% Confirm takeover/7 is called.
  432. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  433. ok.