stream_handler_SUITE.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. shutdown_on_stream_stop(Config) ->
  211. doc("Confirm supervised processes are shutdown when stopping the stream."),
  212. Self = self(),
  213. ConnPid = gun_open(Config),
  214. Ref = gun:get(ConnPid, "/long_polling", [
  215. {<<"accept-encoding">>, <<"gzip">>},
  216. {<<"x-test-case">>, <<"shutdown_on_stream_stop">>},
  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. %% Receive the pid of the newly started process and monitor it.
  222. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  223. MRef = monitor(process, Spawn),
  224. Spawn ! {Self, ready},
  225. %% Confirm terminate/3 is called, indicating the stream ended.
  226. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  227. %% We should receive a DOWN message soon after (or before) because the stream
  228. %% handler is stopping the stream immediately after the process started.
  229. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  230. %% The response is still sent.
  231. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  232. {ok, _} = gun:await_body(ConnPid, Ref),
  233. ok.
  234. shutdown_on_socket_close(Config) ->
  235. doc("Confirm supervised processes are shutdown when the socket closes."),
  236. Self = self(),
  237. ConnPid = gun_open(Config),
  238. _ = gun:get(ConnPid, "/long_polling", [
  239. {<<"accept-encoding">>, <<"gzip">>},
  240. {<<"x-test-case">>, <<"shutdown_on_socket_close">>},
  241. {<<"x-test-pid">>, pid_to_list(Self)}
  242. ]),
  243. %% Confirm init/3 is called.
  244. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  245. %% Receive the pid of the newly started process and monitor it.
  246. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  247. MRef = monitor(process, Spawn),
  248. Spawn ! {Self, ready},
  249. %% Close the socket.
  250. ok = gun:close(ConnPid),
  251. %% Confirm terminate/3 is called, indicating the stream ended.
  252. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  253. %% Confirm we receive a DOWN message for the child process.
  254. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  255. ok.
  256. shutdown_timeout_on_stream_stop(Config) ->
  257. doc("Confirm supervised processes are killed "
  258. "when the shutdown timeout triggers after stopping the stream."),
  259. Self = self(),
  260. ConnPid = gun_open(Config),
  261. Ref = gun:get(ConnPid, "/long_polling", [
  262. {<<"accept-encoding">>, <<"gzip">>},
  263. {<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>},
  264. {<<"x-test-pid">>, pid_to_list(Self)}
  265. ]),
  266. %% Confirm init/3 is called.
  267. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  268. %% Receive the pid of the newly started process and monitor it.
  269. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  270. MRef = monitor(process, Spawn),
  271. Spawn ! {Self, ready},
  272. %% Confirm terminate/3 is called, indicating the stream ended.
  273. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  274. %% We should NOT receive a DOWN message immediately.
  275. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  276. %% We should received it now.
  277. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  278. %% The response is still sent.
  279. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  280. {ok, _} = gun:await_body(ConnPid, Ref),
  281. ok.
  282. shutdown_timeout_on_socket_close(Config) ->
  283. doc("Confirm supervised processes are killed "
  284. "when the shutdown timeout triggers after the socket has closed."),
  285. Self = self(),
  286. ConnPid = gun_open(Config),
  287. _ = gun:get(ConnPid, "/long_polling", [
  288. {<<"accept-encoding">>, <<"gzip">>},
  289. {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>},
  290. {<<"x-test-pid">>, pid_to_list(Self)}
  291. ]),
  292. %% Confirm init/3 is called.
  293. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  294. %% Receive the pid of the newly started process and monitor it.
  295. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  296. MRef = monitor(process, Spawn),
  297. Spawn ! {Self, ready},
  298. %% Close the socket.
  299. ok = gun:close(ConnPid),
  300. %% Confirm terminate/3 is called, indicating the stream ended.
  301. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  302. %% We should NOT receive a DOWN message immediately.
  303. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  304. %% We should received it now.
  305. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  306. ok.
  307. terminate_on_socket_close(Config) ->
  308. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  309. Self = self(),
  310. ConnPid = gun_open(Config),
  311. Ref = gun:get(ConnPid, "/long_polling", [
  312. {<<"accept-encoding">>, <<"gzip">>},
  313. {<<"x-test-case">>, <<"terminate_on_socket_close">>},
  314. {<<"x-test-pid">>, pid_to_list(Self)}
  315. ]),
  316. %% Confirm init/3 is called and receive the beginning of the response.
  317. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  318. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  319. %% Close the socket.
  320. ok = gun:close(ConnPid),
  321. %% Confirm terminate/3 is called.
  322. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  323. ok.
  324. terminate_on_stop(Config) ->
  325. doc("Confirm terminate/3 is called after stop is returned."),
  326. Self = self(),
  327. ConnPid = gun_open(Config),
  328. Ref = gun:get(ConnPid, "/long_polling", [
  329. {<<"accept-encoding">>, <<"gzip">>},
  330. {<<"x-test-case">>, <<"terminate_on_stop">>},
  331. {<<"x-test-pid">>, pid_to_list(Self)}
  332. ]),
  333. %% Confirm init/3 is called and receive the response.
  334. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  335. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  336. %% Confirm the stream is still alive even though we
  337. %% received the response fully, and tell it to stop.
  338. Pid ! {{Pid, 1}, please_stop},
  339. receive {Self, Pid, info, _, please_stop, _} -> ok after 1000 -> error(timeout) end,
  340. %% Confirm terminate/3 is called.
  341. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  342. ok.
  343. terminate_on_switch_protocol(Config) ->
  344. case config(protocol, Config) of
  345. http -> do_terminate_on_switch_protocol(Config);
  346. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  347. end.
  348. do_terminate_on_switch_protocol(Config) ->
  349. doc("Confirm terminate/3 is called after switch_protocol is returned."),
  350. Self = self(),
  351. ConnPid = gun_open(Config),
  352. Ref = gun:get(ConnPid, "/long_polling", [
  353. {<<"accept-encoding">>, <<"gzip">>},
  354. {<<"x-test-case">>, <<"terminate_on_switch_protocol">>},
  355. {<<"x-test-pid">>, pid_to_list(Self)}
  356. ]),
  357. %% Confirm init/3 is called and receive the response.
  358. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  359. {inform, 101, _} = gun:await(ConnPid, Ref),
  360. %% Confirm terminate/3 is called.
  361. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  362. %% Confirm takeover/7 is called.
  363. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  364. ok.