diff options
author | Loïc Hoguin <[email protected]> | 2019-10-10 11:33:35 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-10-10 11:33:35 +0200 |
commit | d52e84bdd97b93d7d9cea827de57bd4a0edea9a8 (patch) | |
tree | 48958da1fc3bd2b2e3666fc9ee2b545099116d47 /test/handlers/ws_shutdown_reason_commands_h.erl | |
parent | cc54c207e35f3ab7a2dfc105eef39fe7d3bf1633 (diff) | |
download | cowboy-d52e84bdd97b93d7d9cea827de57bd4a0edea9a8.tar.gz cowboy-d52e84bdd97b93d7d9cea827de57bd4a0edea9a8.tar.bz2 cowboy-d52e84bdd97b93d7d9cea827de57bd4a0edea9a8.zip |
Add shutdown_reason Websocket command
This allows changing the normal exit reason of Websocket
processes, providing a way to signal other processes of
why the exit occurred.
Diffstat (limited to 'test/handlers/ws_shutdown_reason_commands_h.erl')
-rw-r--r-- | test/handlers/ws_shutdown_reason_commands_h.erl | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/handlers/ws_shutdown_reason_commands_h.erl b/test/handlers/ws_shutdown_reason_commands_h.erl new file mode 100644 index 0000000..90b435c --- /dev/null +++ b/test/handlers/ws_shutdown_reason_commands_h.erl @@ -0,0 +1,38 @@ +%% This module sends the process pid to the test pid +%% found in the x-test-pid header, then changes the +%% shutdown reason and closes the connection normally. + +-module(ws_shutdown_reason_commands_h). +-behavior(cowboy_websocket). + +-export([init/2]). +-export([websocket_init/1]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, RunOrHibernate) -> + TestPid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))), + {cowboy_websocket, Req, {TestPid, RunOrHibernate}}. + +websocket_init(State={TestPid, RunOrHibernate}) -> + TestPid ! {ws_pid, self()}, + ShutdownReason = receive + {TestPid, SR} -> + SR + after 1000 -> + error(timeout) + end, + Commands = [ + {shutdown_reason, ShutdownReason}, + close + ], + case RunOrHibernate of + run -> {Commands, State}; + hibernate -> {Commands, State, hibernate} + end. + +websocket_handle(_, State) -> + {[], State}. + +websocket_info(_, State) -> + {[], State}. |