Debian Codegraph @

test/handlers/ws_shutdown_reason_commands_h.erl

raw
1 %% This module sends the process pid to the test pid
2 %% found in the x-test-pid header, then changes the
3 %% shutdown reason and closes the connection normally.
4
5 -module(ws_shutdown_reason_commands_h).
6 -behavior(cowboy_websocket).
7
8 -export([init/2]).
9 -export([websocket_init/1]).
10 -export([websocket_handle/2]).
11 -export([websocket_info/2]).
12
13 init(Req, Opts) ->
14 TestPid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
15 {cowboy_websocket, Req, {TestPid, maps:get(run_or_hibernate, Opts)}, Opts}.
16
17 websocket_init(State={TestPid, RunOrHibernate}) ->
18 TestPid ! {ws_pid, self()},
19 ShutdownReason = receive
20 {TestPid, SR} ->
21 SR
22 after 1000 ->
23 error(timeout)
24 end,
25 Commands = [
26 {shutdown_reason, ShutdownReason},
27 close
28 ],
29 case RunOrHibernate of
30 run -> {Commands, State};
31 hibernate -> {Commands, State, hibernate}
32 end.
33
34 websocket_handle(_, State) ->
35 {[], State}.
36
37 websocket_info(_, State) ->
38 {[], State}.