aboutsummaryrefslogtreecommitdiffstats
path: root/test/shutdown_SUITE.erl
blob: 97963d553009639073997c699d8ef64aed640554 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%% Copyright (c) 2013, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(shutdown_SUITE).
-compile(export_all).

-import(ct_helper, [doc/1]).

%% ct.

all() ->
	ct_helper:all(?MODULE).

%% Tests.

brutal_kill(_) ->
	Name = brutal_kill,
	{ok, ListenerSup} = ranch:start_listener(Name, 1,
		ranch_tcp, [{port, 0}, {shutdown, brutal_kill}],
		echo_protocol, []),
	Port = ranch:get_port(Name),
	{ok, _} = gen_tcp:connect("localhost", Port, []),
	receive after 100 -> ok end,
	ListenerSupChildren = supervisor:which_children(ListenerSup),
	{_, ConnsSup, _, _}
		= lists:keyfind(ranch_conns_sup, 1, ListenerSupChildren),
	[{_, Pid, _, _}] = supervisor:which_children(ConnsSup),
	true = is_process_alive(Pid),
	ok = ranch:stop_listener(Name),
	receive after 100 -> ok end,
	false = is_process_alive(Pid),
	false = is_process_alive(ListenerSup),
	{error, _} = gen_tcp:connect("localhost", Port, []),
	ok.

infinity(_) ->
	Name = infinity,
	{ok, ListenerSup} = ranch:start_listener(Name, 1,
		ranch_tcp, [{port, 0}, {shutdown, infinity}],
		echo_protocol, []),
	Port = ranch:get_port(Name),
	{ok, _} = gen_tcp:connect("localhost", Port, []),
	receive after 100 -> ok end,
	ListenerSupChildren = supervisor:which_children(ListenerSup),
	{_, ConnsSup, _, _}
		= lists:keyfind(ranch_conns_sup, 1, ListenerSupChildren),
	[{_, Pid, _, _}] = supervisor:which_children(ConnsSup),
	true = is_process_alive(Pid),
	ok = ranch:stop_listener(Name),
	receive after 100 -> ok end,
	false = is_process_alive(Pid),
	false = is_process_alive(ListenerSup),
	{error, _} = gen_tcp:connect("localhost", Port, []),
	ok.

infinity_trap_exit(_) ->
	Name = infinity_trap_exit,
	{ok, ListenerSup} = ranch:start_listener(Name, 1,
		ranch_tcp, [{port, 0}, {shutdown, infinity}],
		trap_exit_protocol, []),
	Port = ranch:get_port(Name),
	{ok, _} = gen_tcp:connect("localhost", Port, []),
	receive after 100 -> ok end,
	ListenerSupChildren = supervisor:which_children(ListenerSup),
	{_, ConnsSup, _, _}
		= lists:keyfind(ranch_conns_sup, 1, ListenerSupChildren),
	[{_, Pid, _, _}] = supervisor:which_children(ConnsSup),
	true = is_process_alive(Pid),
	%% This call will block infinitely.
	SpawnPid = spawn(fun() -> ok = ranch:stop_listener(Name) end),
	receive after 100 -> ok end,
	%% The protocol traps exit signals, and ignore them, so it won't die.
	true = is_process_alive(Pid),
	%% The listener will stay up forever too.
	true = is_process_alive(ListenerSup),
	%% We can't connect, though.
	{error, _} = gen_tcp:connect("localhost", Port, []),
	%% Killing the process unblocks everything.
	exit(Pid, kill),
	receive after 100 -> ok end,
	false = is_process_alive(ListenerSup),
	false = is_process_alive(SpawnPid),
	ok.

%% Same as infinity because the protocol doesn't trap exits.
timeout(_) ->
	Name = timeout,
	{ok, ListenerSup} = ranch:start_listener(Name, 1,
		ranch_tcp, [{port, 0}, {shutdown, 500}],
		echo_protocol, []),
	Port = ranch:get_port(Name),
	{ok, _} = gen_tcp:connect("localhost", Port, []),
	receive after 100 -> ok end,
	ListenerSupChildren = supervisor:which_children(ListenerSup),
	{_, ConnsSup, _, _}
		= lists:keyfind(ranch_conns_sup, 1, ListenerSupChildren),
	[{_, Pid, _, _}] = supervisor:which_children(ConnsSup),
	true = is_process_alive(Pid),
	ok = ranch:stop_listener(Name),
	receive after 100 -> ok end,
	false = is_process_alive(Pid),
	false = is_process_alive(ListenerSup),
	{error, _} = gen_tcp:connect("localhost", Port, []),
	ok.

timeout_trap_exit(_) ->
	Name = timeout_trap_exit,
	{ok, ListenerSup} = ranch:start_listener(Name, 1,
		ranch_tcp, [{port, 0}, {shutdown, 500}],
		trap_exit_protocol, []),
	Port = ranch:get_port(Name),
	{ok, _} = gen_tcp:connect("localhost", Port, []),
	receive after 100 -> ok end,
	ListenerSupChildren = supervisor:which_children(ListenerSup),
	{_, ConnsSup, _, _}
		= lists:keyfind(ranch_conns_sup, 1, ListenerSupChildren),
	[{_, Pid, _, _}] = supervisor:which_children(ConnsSup),
	true = is_process_alive(Pid),
	%% This call will block for the duration of the shutdown.
	SpawnPid = spawn(fun() -> ok = ranch:stop_listener(Name) end),
	receive after 100 -> ok end,
	%% The protocol traps exit signals, and ignore them, so it won't die.
	true = is_process_alive(Pid),
	%% The listener will stay up for now too.
	true = is_process_alive(ListenerSup),
	%% We can't connect, though.
	{error, _} = gen_tcp:connect("localhost", Port, []),
	%% Wait for the timeout to finish and see that everything is killed.
	receive after 500 -> ok end,
	false = is_process_alive(Pid),
	false = is_process_alive(ListenerSup),
	false = is_process_alive(SpawnPid),
	ok.