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