stream_handler_h.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %% This module behaves differently depending on a specific header.
  2. -module(stream_handler_h).
  3. -behavior(cowboy_stream).
  4. -export([init/3]).
  5. -export([data/4]).
  6. -export([info/3]).
  7. -export([terminate/3]).
  8. -export([early_error/5]).
  9. -record(state, {
  10. pid,
  11. test
  12. }).
  13. init(StreamID, Req, Opts) ->
  14. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
  15. Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1),
  16. State = #state{pid=Pid, test=Test},
  17. Pid ! {Pid, self(), init, StreamID, Req, Opts},
  18. {init_commands(StreamID, Req, State), State}.
  19. init_commands(_, _, #state{test=crash_in_init}) ->
  20. error(crash);
  21. init_commands(_, _, #state{test=crash_in_data}) ->
  22. [];
  23. init_commands(_, _, #state{test=crash_in_info}) ->
  24. [];
  25. init_commands(_, _, #state{test=crash_in_terminate}) ->
  26. [{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop];
  27. init_commands(_, _, #state{test=crash_in_early_error}) ->
  28. error(crash);
  29. init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) ->
  30. Spawn = init_process(false, State),
  31. [{headers, 200, #{}}, {spawn, Spawn, 5000}, stop];
  32. init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
  33. Spawn = init_process(false, State),
  34. [{headers, 200, #{}}, {spawn, Spawn, 5000}];
  35. init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
  36. Spawn = init_process(true, State),
  37. [{headers, 200, #{}}, {spawn, Spawn, 2000}, stop];
  38. init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
  39. Spawn = init_process(true, State),
  40. [{headers, 200, #{}}, {spawn, Spawn, 2000}];
  41. init_commands(_, _, State=#state{test=terminate_on_stop}) ->
  42. [{response, 204, #{}, <<>>}];
  43. init_commands(_, _, _) ->
  44. [{headers, 200, #{}}].
  45. init_process(TrapExit, #state{pid=Pid}) ->
  46. Self = self(),
  47. Spawn = spawn_link(fun() ->
  48. process_flag(trap_exit, TrapExit),
  49. Pid ! {Pid, Self, spawned, self()},
  50. receive {Pid, ready} -> ok after 1000 -> error(timeout) end,
  51. Self ! {self(), ready},
  52. receive after 5000 ->
  53. Pid ! {Pid, Self, still_alive, self()}
  54. end
  55. end),
  56. receive {Spawn, ready} -> ok after 1000 -> error(timeout) end,
  57. Spawn.
  58. data(_, _, _, #state{test=crash_in_data}) ->
  59. error(crash);
  60. data(StreamID, IsFin, Data, State=#state{pid=Pid}) ->
  61. Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
  62. {[], State}.
  63. info(_, Resp={response, _, _, _}, State) ->
  64. {[Resp], State};
  65. info(_, crash, #state{test=crash_in_info}) ->
  66. error(crash);
  67. info(StreamID, Info, State=#state{pid=Pid}) ->
  68. Pid ! {Pid, self(), info, StreamID, Info, State},
  69. case Info of
  70. please_stop -> {[stop], State};
  71. _ -> {[], State}
  72. end.
  73. terminate(StreamID, Reason, State=#state{pid=Pid, test=crash_in_terminate}) ->
  74. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  75. error(crash);
  76. terminate(StreamID, Reason, State=#state{pid=Pid}) ->
  77. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  78. ok.
  79. %% This clause can only test for early errors that reached the required headers.
  80. early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
  81. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
  82. Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
  83. case cowboy_req:header(<<"x-test-case">>, PartialReq) of
  84. <<"crash_in_early_error",_/bits>> -> error(crash);
  85. _ -> Resp
  86. end.