stream_handler_SUITE.erl 16 KB

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