aboutsummaryrefslogtreecommitdiffstats
path: root/test/ws_SUITE_data
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-09-26 15:58:44 +0300
committerLoïc Hoguin <[email protected]>2014-09-26 15:58:44 +0300
commit5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 (patch)
tree7094d5f9d92c9c3bac1a60ca4b4922ba035b219d /test/ws_SUITE_data
parentfd37fad592fc96a384bcd060696194f5fe074f6f (diff)
downloadcowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.gz
cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.bz2
cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.zip
Unify the init and terminate callbacks
This set of changes is the first step to simplify the writing of handlers, by removing some extraneous callbacks and making others optional. init/3 is now init/2, its first argument being removed. rest_init/2 and rest_terminate/2 have been removed. websocket_init/3 and websocket_terminate/3 have been removed. terminate/3 is now optional. It is called regardless of the type of handler, including rest and websocket. The return value of init/2 changed. It now returns {Mod, Req, Opts} with Mod being either one of the four handler type or a custom module. It can also return extra timeout and hibernate options. The signature for sub protocols has changed, they now receive these extra timeout and hibernate options. Loop handlers are now implemented in cowboy_long_polling, and will be renamed throughout the project in a future commit.
Diffstat (limited to 'test/ws_SUITE_data')
-rw-r--r--test/ws_SUITE_data/ws_echo.erl17
-rw-r--r--test/ws_SUITE_data/ws_echo_timer.erl17
-rw-r--r--test/ws_SUITE_data/ws_init_shutdown.erl20
-rw-r--r--test/ws_SUITE_data/ws_send_many.erl16
-rw-r--r--test/ws_SUITE_data/ws_timeout_cancel.erl16
-rw-r--r--test/ws_SUITE_data/ws_timeout_hibernate.erl16
-rw-r--r--test/ws_SUITE_data/ws_upgrade_with_opts.erl28
7 files changed, 26 insertions, 104 deletions
diff --git a/test/ws_SUITE_data/ws_echo.erl b/test/ws_SUITE_data/ws_echo.erl
index d4a5f07..c4ab406 100644
--- a/test/ws_SUITE_data/ws_echo.erl
+++ b/test/ws_SUITE_data/ws_echo.erl
@@ -1,17 +1,13 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_echo).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3, websocket_handle/3,
- websocket_info/3, websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+-export([init/2]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
-websocket_init(_TransportName, Req, _Opts) ->
- Req2 = cowboy_req:compact(Req),
- {ok, Req2, undefined}.
+init(Req, _) ->
+ {ws, Req, undefined}.
websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State};
@@ -22,6 +18,3 @@ websocket_handle(_Frame, Req, State) ->
websocket_info(_Info, Req, State) ->
{ok, Req, State}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
diff --git a/test/ws_SUITE_data/ws_echo_timer.erl b/test/ws_SUITE_data/ws_echo_timer.erl
index 666a26d..199f02c 100644
--- a/test/ws_SUITE_data/ws_echo_timer.erl
+++ b/test/ws_SUITE_data/ws_echo_timer.erl
@@ -1,18 +1,14 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_echo_timer).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3, websocket_handle/3,
- websocket_info/3, websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+-export([init/2]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
-websocket_init(_TransportName, Req, _Opts) ->
+init(Req, _) ->
erlang:start_timer(1000, self(), <<"websocket_init">>),
- Req2 = cowboy_req:compact(Req),
- {ok, Req2, undefined}.
+ {ws, Req, undefined}.
websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State};
@@ -26,6 +22,3 @@ websocket_info({timeout, _Ref, Msg}, Req, State) ->
{reply, {text, Msg}, Req, State};
websocket_info(_Info, Req, State) ->
{ok, Req, State}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
diff --git a/test/ws_SUITE_data/ws_init_shutdown.erl b/test/ws_SUITE_data/ws_init_shutdown.erl
index 30bf66e..68f96f0 100644
--- a/test/ws_SUITE_data/ws_init_shutdown.erl
+++ b/test/ws_SUITE_data/ws_init_shutdown.erl
@@ -1,22 +1,8 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_init_shutdown).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3, websocket_handle/3,
- websocket_info/3, websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+-export([init/2]).
-websocket_init(_TransportName, Req, _Opts) ->
- {shutdown, cowboy_req:reply(403, Req)}.
-
-websocket_handle(_Frame, _Req, _State) ->
- exit(badarg).
-
-websocket_info(_Info, _Req, _State) ->
- exit(badarg).
-
-websocket_terminate(_Reason, _Req, _State) ->
- exit(badarg).
+init(Req, _) ->
+ {shutdown, cowboy_req:reply(403, Req), undefined}.
diff --git a/test/ws_SUITE_data/ws_send_many.erl b/test/ws_SUITE_data/ws_send_many.erl
index 2ed4772..2da82c3 100644
--- a/test/ws_SUITE_data/ws_send_many.erl
+++ b/test/ws_SUITE_data/ws_send_many.erl
@@ -1,27 +1,17 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_send_many).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3]).
+-export([init/2]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
--export([websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
-
-websocket_init(_TransportName, Req, Sequence) ->
- Req2 = cowboy_req:compact(Req),
+init(Req, Opts) ->
erlang:send_after(10, self(), send_many),
- {ok, Req2, Sequence}.
+ {ws, Req, Opts}.
websocket_handle(_Frame, Req, State) ->
{ok, Req, State}.
websocket_info(send_many, Req, State = [{sequence, Sequence}]) ->
{reply, Sequence, Req, State}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
diff --git a/test/ws_SUITE_data/ws_timeout_cancel.erl b/test/ws_SUITE_data/ws_timeout_cancel.erl
index 9c7b72b..6fcfc43 100644
--- a/test/ws_SUITE_data/ws_timeout_cancel.erl
+++ b/test/ws_SUITE_data/ws_timeout_cancel.erl
@@ -1,17 +1,14 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_timeout_cancel).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3, websocket_handle/3,
- websocket_info/3, websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+-export([init/2]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
-websocket_init(_TransportName, Req, _Opts) ->
+init(Req, _) ->
erlang:start_timer(500, self(), should_not_cancel_timer),
- {ok, Req, undefined, 1000}.
+ {ws, Req, undefined, 1000}.
websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State};
@@ -21,6 +18,3 @@ websocket_handle({binary, Data}, Req, State) ->
websocket_info(_Info, Req, State) ->
erlang:start_timer(500, self(), should_not_cancel_timer),
{ok, Req, State}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
diff --git a/test/ws_SUITE_data/ws_timeout_hibernate.erl b/test/ws_SUITE_data/ws_timeout_hibernate.erl
index cc91e26..da901d7 100644
--- a/test/ws_SUITE_data/ws_timeout_hibernate.erl
+++ b/test/ws_SUITE_data/ws_timeout_hibernate.erl
@@ -1,22 +1,16 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_timeout_hibernate).
--behaviour(cowboy_websocket_handler).
--export([init/3]).
--export([websocket_init/3, websocket_handle/3,
- websocket_info/3, websocket_terminate/3]).
-init(_Any, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+-export([init/2]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
-websocket_init(_TransportName, Req, _Opts) ->
- {ok, Req, undefined, 1000, hibernate}.
+init(Req, _) ->
+ {ws, Req, undefined, 1000, hibernate}.
websocket_handle(_Frame, Req, State) ->
{ok, Req, State, hibernate}.
websocket_info(_Info, Req, State) ->
{ok, Req, State, hibernate}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
diff --git a/test/ws_SUITE_data/ws_upgrade_with_opts.erl b/test/ws_SUITE_data/ws_upgrade_with_opts.erl
deleted file mode 100644
index b4f82fa..0000000
--- a/test/ws_SUITE_data/ws_upgrade_with_opts.erl
+++ /dev/null
@@ -1,28 +0,0 @@
-%% Feel free to use, reuse and abuse the code in this file.
-
--module(ws_upgrade_with_opts).
--behaviour(cowboy_websocket_handler).
-
--export([init/3]).
--export([websocket_init/3]).
--export([websocket_handle/3]).
--export([websocket_info/3]).
--export([websocket_terminate/3]).
-
-init(_Any, Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket, Req, <<"success">>}.
-
-websocket_init(_TransportName, Req, Response) ->
- Req2 = cowboy_req:compact(Req),
- erlang:send_after(10, self(), send_response),
- {ok, Req2, Response}.
-
-websocket_handle(_Frame, Req, State) ->
- {ok, Req, State}.
-
-websocket_info(send_response, Req, State = Response)
- when is_binary(Response) ->
- {reply, {text, Response}, Req, State}.
-
-websocket_terminate(_Reason, _Req, _State) ->
- ok.