switch_protocol_flush_h.erl 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. takeover(_, _, _, _, _, _, Pid) ->
  25. Msgs = receive_all([]),
  26. Pid ! {Pid, Msgs},
  27. exit(normal).
  28. receive_all(Acc) ->
  29. receive
  30. Msg ->
  31. receive_all([Msg|Acc])
  32. after 0 ->
  33. Acc
  34. end.
  35. validate(Msgs) ->
  36. [
  37. {ranch_tcp, socket, <<"123">>},
  38. {'$gen_call', a, b},
  39. {system, a, b},
  40. good
  41. ] = Msgs,
  42. ok.