switch_protocol_flush_h.erl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. %% This module is used to test the flushing of messages when
  2. %% switch_protocol is executed by cowboy_http.
  3. -module(switch_protocol_flush_h).
  4. -export([init/3]).
  5. -export([info/3]).
  6. -export([terminate/3]).
  7. -export([takeover/7]).
  8. -export([validate/1]).
  9. init(StreamID, Req, _) ->
  10. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
  11. %% Send ourselves a few messages that may or may not be flushed.
  12. self() ! good,
  13. self() ! {'EXIT', Pid, normal},
  14. self() ! {system, a, b},
  15. self() ! {{self(), StreamID}, hello},
  16. self() ! {'$gen_call', a, b},
  17. self() ! {timeout, make_ref(), ?MODULE},
  18. self() ! {ranch_tcp, socket, <<"123">>},
  19. {[{switch_protocol, #{}, ?MODULE, Pid}], undefined}.
  20. info(_, _, State) ->
  21. {[], State}.
  22. terminate(_, _, _) ->
  23. ok.
  24. %% @todo It would be good if we could allow this function to return normally.
  25. -spec takeover(_, _, _, _, _, _, _) -> no_return().
  26. takeover(_, _, _, _, _, _, Pid) ->
  27. Msgs = receive_all([]),
  28. Pid ! {Pid, Msgs},
  29. exit(normal).
  30. receive_all(Acc) ->
  31. receive
  32. Msg ->
  33. receive_all([Msg|Acc])
  34. after 0 ->
  35. Acc
  36. end.
  37. validate(Msgs) ->
  38. [
  39. {ranch_tcp, socket, <<"123">>},
  40. {'$gen_call', a, b},
  41. {system, a, b},
  42. good
  43. ] = Msgs,
  44. ok.