aboutsummaryrefslogtreecommitdiffstats
path: root/test/batch_echo_protocol.erl
diff options
context:
space:
mode:
authorjuhlig <[email protected]>2019-05-14 17:16:09 +0200
committerLoïc Hoguin <[email protected]>2019-05-18 22:02:05 +0200
commit0902d969562505ed80ad9b1e855d6fb442060a69 (patch)
treeeee732d38a5d525ee053acde8395d8afafddfaa8 /test/batch_echo_protocol.erl
parentb1e6406e4f0871c656f92fdd0755c8ef82be2818 (diff)
downloadranch-0902d969562505ed80ad9b1e855d6fb442060a69.tar.gz
ranch-0902d969562505ed80ad9b1e855d6fb442060a69.tar.bz2
ranch-0902d969562505ed80ad9b1e855d6fb442060a69.zip
Add tests for active N mode
Diffstat (limited to 'test/batch_echo_protocol.erl')
-rw-r--r--test/batch_echo_protocol.erl30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/batch_echo_protocol.erl b/test/batch_echo_protocol.erl
new file mode 100644
index 0000000..b48fcbe
--- /dev/null
+++ b/test/batch_echo_protocol.erl
@@ -0,0 +1,30 @@
+-module(batch_echo_protocol).
+-behaviour(ranch_protocol).
+
+-export([start_link/3]).
+-export([init/3]).
+
+start_link(Ref, Transport, [{batch_size, N}]) ->
+ Pid = spawn_link(?MODULE, init, [Ref, Transport, N]),
+ {ok, Pid}.
+
+init(Ref, Transport, N) ->
+ {ok, Socket} = ranch:handshake(Ref),
+ Transport:setopts(Socket, [{active, N}]),
+ loop(Socket, Transport, N, <<>>).
+
+loop(Socket, Transport, N, Acc) ->
+ {OK, Closed, Error, Passive} = Transport:messages(),
+ receive
+ {OK, Socket, Data} ->
+ Transport:send(Socket, <<"OK">>),
+ loop(Socket, Transport, N, <<Acc/binary, Data/binary>>);
+ {Passive, Socket} ->
+ Transport:send(Socket, Acc),
+ Transport:setopts(Socket, [{active, N}]),
+ loop(Socket, Transport, N, <<>>);
+ {Closed, Socket} ->
+ ok;
+ {Error, Socket, _} ->
+ ok = Transport:close(Socket)
+ end.