From a2facaf2da2c95df32ffdc529bd3bdd9f91c080c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 14 Aug 2017 17:17:44 +0200 Subject: Add tests for the streams shutdown mechanism --- test/handlers/stream_handler_h.erl | 45 +++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'test/handlers') diff --git a/test/handlers/stream_handler_h.erl b/test/handlers/stream_handler_h.erl index bb4a6da..3ee4932 100644 --- a/test/handlers/stream_handler_h.erl +++ b/test/handlers/stream_handler_h.erl @@ -9,21 +9,56 @@ -export([terminate/3]). -export([early_error/5]). +-record(state, { + pid, + test +}). + init(StreamID, Req, Opts) -> - %% @todo Vary behavior depending on x-test-case. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))), + Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1), + State = #state{pid=Pid, test=Test}, Pid ! {Pid, self(), init, StreamID, Req, Opts}, - {[{headers, 200, #{}}], Pid}. + {init_commands(StreamID, Req, State), State}. + +init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) -> + Spawn = init_process(false, State), + [{headers, 200, #{}}, {spawn, Spawn, 5000}, stop]; +init_commands(_, _, State=#state{test=shutdown_on_socket_close}) -> + Spawn = init_process(false, State), + [{headers, 200, #{}}, {spawn, Spawn, 5000}]; +init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) -> + Spawn = init_process(true, State), + [{headers, 200, #{}}, {spawn, Spawn, 2000}, stop]; +init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) -> + Spawn = init_process(true, State), + [{headers, 200, #{}}, {spawn, Spawn, 2000}]; +init_commands(_, _, _) -> + [{headers, 200, #{}}]. + +init_process(TrapExit, #state{pid=Pid}) -> + Self = self(), + Spawn = spawn_link(fun() -> + process_flag(trap_exit, TrapExit), + Pid ! {Pid, Self, spawned, self()}, + receive {Pid, ready} -> ok after 1000 -> error(timeout) end, + Self ! {self(), ready}, + receive after 5000 -> + Pid ! {Pid, Self, still_alive, self()} + end + end), + receive {Spawn, ready} -> ok after 1000 -> error(timeout) end, + Spawn. -data(StreamID, IsFin, Data, State=Pid) -> +data(StreamID, IsFin, Data, State=#state{pid=Pid}) -> Pid ! {Pid, self(), data, StreamID, IsFin, Data, State}, {[], State}. -info(StreamID, Info, State=Pid) -> +info(StreamID, Info, State=#state{pid=Pid}) -> Pid ! {Pid, self(), info, StreamID, Info, State}, {[], State}. -terminate(StreamID, Reason, State=Pid) -> +terminate(StreamID, Reason, State=#state{pid=Pid}) -> Pid ! {Pid, self(), terminate, StreamID, Reason, State}, ok. -- cgit v1.2.3