-
Notifications
You must be signed in to change notification settings - Fork 474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
graceful shutdown #178
base: main
Are you sure you want to change the base?
graceful shutdown #178
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,9 @@ client_fun(Socket, [{send_pid, To} | Cmds]) -> | |
To ! {client, self()}, | ||
client_fun(Socket, Cmds); | ||
client_fun(Socket, [{send, Data, Tester} | Cmds]) -> | ||
client_fun(Socket, [{send, Data, Tester, 0} | Cmds]); | ||
client_fun(Socket, [{send, Data, Tester, Delay} | Cmds]) -> | ||
timer:sleep(Delay), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not good to have tests that sleep, they can cause the test runs to take much longer than they should and they can intermittently pass or fail depending on external factors (e.g. the specs or load on the system running the tests). It's best to have code that doesn't require the wall clock to work, or at least use mocking when testing it to avoid any sleeping. |
||
case gen_tcp:send(Socket, Data) of | ||
ok -> ok; | ||
{error, E} -> Tester ! {client_send_error, self(), E} | ||
|
@@ -140,10 +143,72 @@ normal_acceptor_test_fun() -> | |
?assertEqual(Expected, Result) | ||
end || {Max, PoolSize, NumClients, Expected} <- Tests]. | ||
|
||
graceful_shutdown_test_fun() -> | ||
Tester = self(), | ||
NumClients = 2, | ||
ClientSendDelay = 10, | ||
BufferTime = 5, | ||
ShutDownDelay = (NumClients * ClientSendDelay) + BufferTime, | ||
ServerOpts = [{max, NumClients}, {acceptor_pool_size, NumClients}, {shutdown_delay, ShutDownDelay}], | ||
ServerLoop = | ||
fun (Socket, _Opts) -> | ||
Tester ! {server_accepted, self()}, | ||
mochiweb_socket:setopts(Socket, [{packet, 1}]), | ||
echo_loop(Socket) | ||
end, | ||
{Server, Port} = socket_server(ServerOpts, ServerLoop), | ||
Data = <<"data">>, | ||
ClientCmds = [{send_pid, Tester}, {wait_msg, go}, | ||
{send, Data, Tester, ClientSendDelay}, | ||
{close_sock}, {send_msg, done, Tester}], | ||
start_client_conns(Port, NumClients, fun client_fun/2, ClientCmds, Tester), | ||
|
||
ConnectLoop = | ||
fun (Loop, Connected, Accepted, Errors) -> | ||
case (length(Accepted) + Errors >= NumClients | ||
andalso length(Connected) + Errors >= NumClients) of | ||
true -> {Connected, Accepted}; | ||
false -> | ||
receive | ||
{server_accepted, ServerPid} -> | ||
Loop(Loop, Connected, [ServerPid | Accepted], Errors); | ||
{client, ClientPid} -> | ||
Loop(Loop, Connected ++ [ClientPid], Accepted, Errors); | ||
{client_conn_error, _E} -> | ||
Loop(Loop, Connected, Accepted, Errors + 1) | ||
end | ||
end | ||
end, | ||
{Connected, _} = ConnectLoop(ConnectLoop, [], [], 0), | ||
|
||
spawn(mochiweb_socket_server, stop, [Server]), | ||
|
||
WaitLoop = | ||
fun (_Loop, Done, Error, []) -> | ||
{Done, Error}; | ||
(Loop, Done, Error, [NextClient | Rest]) -> | ||
NextClient ! go, | ||
receive | ||
{done, From} -> | ||
Loop(Loop, [From | Done], Error, Rest); | ||
E -> | ||
Loop(Loop, Done, [E | Error], Rest) | ||
end | ||
end, | ||
|
||
{Done, Error} = WaitLoop(WaitLoop, [], [], Connected), | ||
?assertEqual(NumClients, length(Done)), | ||
?assertEqual([], Error). | ||
|
||
|
||
-define(LARGE_TIMEOUT, 40). | ||
|
||
normal_acceptor_test_() -> | ||
Tests = normal_acceptor_test_fun(), | ||
{timeout, ?LARGE_TIMEOUT, Tests}. | ||
|
||
|
||
graceful_shutdown_test_() -> | ||
{timeout, ?LARGE_TIMEOUT, [fun() -> graceful_shutdown_test_fun() end]}. | ||
|
||
-endif. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a more active solution that doesn't require polling.