From 4af873ce004ba4a82e22d04308d7a105916bbca2 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Tue, 19 Jul 2011 15:41:27 -0400 Subject: [PATCH] Fix broken tests + remove outdated tests --- src/lhttpc.erl | 26 ++++---- test/lhttpc_manager_tests.erl | 119 ---------------------------------- test/lhttpc_tests.erl | 35 ++-------- 3 files changed, 19 insertions(+), 161 deletions(-) delete mode 100644 test/lhttpc_manager_tests.erl diff --git a/src/lhttpc.erl b/src/lhttpc.erl index 9896f60c..0083ea63 100644 --- a/src/lhttpc.erl +++ b/src/lhttpc.erl @@ -325,20 +325,18 @@ request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options) -> Args = [self(), Host, Port, Ssl, Path, Method, Hdrs, Body, Options], Pid = spawn_link(lhttpc_client, request, Args), receive - X -> - X - % {response, Pid, R} -> - % R; - % {exit, Pid, Reason} -> - % % We would rather want to exit here, instead of letting the - % % linked client send us an exit signal, since this can be - % % caught by the caller. - % exit(Reason); - % {'EXIT', Pid, Reason} -> - % % This could happen if the process we're running in taps exits - % % and the client process exits due to some exit signal being - % % sent to it. Very unlikely though - % exit(Reason) + {response, Pid, R} -> + R; + {exit, Pid, Reason} -> + % We would rather want to exit here, instead of letting the + % linked client send us an exit signal, since this can be + % caught by the caller. + exit(Reason); + {'EXIT', Pid, Reason} -> + % This could happen if the process we're running in taps exits + % and the client process exits due to some exit signal being + % sent to it. Very unlikely though + exit(Reason) after Timeout -> kill_client(Pid) end. diff --git a/test/lhttpc_manager_tests.erl b/test/lhttpc_manager_tests.erl deleted file mode 100644 index 67a2344a..00000000 --- a/test/lhttpc_manager_tests.erl +++ /dev/null @@ -1,119 +0,0 @@ -%%% ---------------------------------------------------------------------------- -%%% Copyright (c) 2009, Erlang Training and Consulting Ltd. -%%% All rights reserved. -%%% -%%% Redistribution and use in source and binary forms, with or without -%%% modification, are permitted provided that the following conditions are met: -%%% * Redistributions of source code must retain the above copyright -%%% notice, this list of conditions and the following disclaimer. -%%% * Redistributions in binary form must reproduce the above copyright -%%% notice, this list of conditions and the following disclaimer in the -%%% documentation and/or other materials provided with the distribution. -%%% * Neither the name of Erlang Training and Consulting Ltd. nor the -%%% names of its contributors may be used to endorse or promote products -%%% derived from this software without specific prior written permission. -%%% -%%% THIS SOFTWARE IS PROVIDED BY Erlang Training and Consulting Ltd. ''AS IS'' -%%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -%%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -%%% ARE DISCLAIMED. IN NO EVENT SHALL Erlang Training and Consulting Ltd. BE -%%% LIABLE SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR -%%% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -%%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -%%% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -%%% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -%%% ---------------------------------------------------------------------------- - -%%% @author Oscar Hellström --module(lhttpc_manager_tests). - --include_lib("eunit/include/eunit.hrl"). - --define(HOST, "www.example.com"). --define(PORT, 666). --define(SSL, false). - -%%% Eunit setup stuff - -start_app() -> - application:start(public_key), - ok = application:start(ssl), - ok = application:start(lhttpc). - -stop_app(_) -> - ok = application:stop(lhttpc), - ok = application:stop(ssl). - -manager_test_() -> - {inorder, - {setup, fun start_app/0, fun stop_app/1, [ - ?_test(empty_manager()), - ?_test(one_socket()), - ?_test(many_sockets()), - ?_test(closed_race_cond()) - ]} - }. - -%%% Tests - -empty_manager() -> - ?assertEqual(no_socket, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})). - -one_socket() -> - {LS, Socket} = socket_server:open(), - gen_tcp:close(LS), % no use of this - give_away(Socket, ?HOST, ?PORT, ?SSL), - ?assertEqual({ok, Socket}, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})), - ?assertEqual(no_socket, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})), - gen_tcp:close(Socket). - -many_sockets() -> - {LS, Socket1} = socket_server:open(), - {ok, Port} = inet:port(LS), - Pid2 = socket_server:accept(LS), - Pid3 = socket_server:accept(LS), - Socket2 = socket_server:connect(Pid2, Port), - Socket3 = socket_server:connect(Pid3, Port), - gen_tcp:close(LS), - give_away(Socket1, ?HOST, ?PORT, ?SSL), - give_away(Socket2, ?HOST, ?PORT, ?SSL), - give_away(Socket3, ?HOST, ?PORT, ?SSL), - ?assertEqual({ok, Socket3}, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})), - ?assertEqual({ok, Socket2}, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})), - ?assertEqual({ok, Socket1}, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})), - link(whereis(lhttpc_manager)), % want to make sure it doesn't crash - lhttpc_manager ! {tcp_closed, Socket1}, - ?assertEqual(0, lhttpc_manager:connection_count()), - unlink(whereis(lhttpc_manager)), - gen_tcp:close(Socket1), - gen_tcp:close(Socket2), - gen_tcp:close(Socket3). - -closed_race_cond() -> - {LS, Socket} = socket_server:open(), - gen_tcp:close(LS), % no use of this - give_away(Socket, ?HOST, ?PORT, ?SSL), - Pid = self(), - ManagerPid = whereis(lhttpc_manager), - true = erlang:suspend_process(ManagerPid), - spawn_link(fun() -> - Pid ! {result, gen_server:call(lhttpc_manager, - {socket, self(), ?HOST, ?PORT, ?SSL})} - end), - erlang:yield(), % make sure that the spawned process has run - gen_tcp:close(Socket), % a closed message should be sent to the manager - true = erlang:resume_process(ManagerPid), - Result = receive {result, R} -> R end, - ?assertEqual(no_socket, Result). - -%%% Helpers functions - -give_away(Socket, Host, Port, Ssl) -> - gen_tcp:controlling_process(Socket, whereis(lhttpc_manager)), - gen_server:cast(lhttpc_manager, {done, Host, Port, Ssl, Socket}). diff --git a/test/lhttpc_tests.erl b/test/lhttpc_tests.erl index ad35e56b..b7e4f428 100644 --- a/test/lhttpc_tests.erl +++ b/test/lhttpc_tests.erl @@ -1,7 +1,7 @@ %%% ---------------------------------------------------------------------------- %%% Copyright (c) 2009, Erlang Training and Consulting Ltd. %%% All rights reserved. -%%% +%%% %%% Redistribution and use in source and binary forms, with or without %%% modification, are permitted provided that the following conditions are met: %%% * Redistributions of source code must retain the above copyright @@ -12,7 +12,7 @@ %%% * Neither the name of Erlang Training and Consulting Ltd. nor the %%% names of its contributors may be used to endorse or promote products %%% derived from this software without specific prior written permission. -%%% +%%% %%% THIS SOFTWARE IS PROVIDED BY Erlang Training and Consulting Ltd. ''AS IS'' %%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE %%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -109,7 +109,7 @@ stop_app(_) -> ok = application:stop(ssl). tcp_test_() -> - {inorder, + {inorder, {setup, fun start_app/0, fun stop_app/1, [ ?_test(simple_get()), ?_test(empty_get()), @@ -134,7 +134,6 @@ tcp_test_() -> ?_test(bad_url()), ?_test(persistent_connection()), ?_test(request_timeout()), - ?_test(connection_timeout()), ?_test(suspended_manager()), ?_test(chunked_encoding()), ?_test(partial_upload_identity()), @@ -152,8 +151,7 @@ tcp_test_() -> ?_test(partial_download_smallish_chunks()), ?_test(partial_download_slow_chunks()), ?_test(close_connection()), - ?_test(message_queue()), - ?_test(connection_count()) % just check that it's 0 (last) + ?_test(message_queue()) ]} }. @@ -162,8 +160,7 @@ ssl_test_() -> {setup, fun start_app/0, fun stop_app/1, [ ?_test(ssl_get()), ?_test(ssl_post()), - ?_test(ssl_chunked()), - ?_test(connection_count()) % just check that it's 0 (last) + ?_test(ssl_chunked()) ]} }. @@ -379,18 +376,6 @@ request_timeout() -> URL = url(Port, "/slow"), ?assertEqual({error, timeout}, lhttpc:request(URL, get, [], 50)). -connection_timeout() -> - Port = start(gen_tcp, [fun simple_response/5, fun simple_response/5]), - URL = url(Port, "/close_conn"), - lhttpc_manager:update_connection_timeout(50), % very short keep alive - {ok, Response} = lhttpc:request(URL, get, [], 100), - ?assertEqual({200, "OK"}, status(Response)), - ?assertEqual(<>, body(Response)), - timer:sleep(100), - ?assertEqual(0, - lhttpc_manager:connection_count({"localhost", Port, false})), - lhttpc_manager:update_connection_timeout(300000). % set back - suspended_manager() -> Port = start(gen_tcp, [fun simple_response/5, fun simple_response/5]), URL = url(Port, "/persistent"), @@ -401,8 +386,6 @@ suspended_manager() -> true = erlang:suspend_process(Pid), ?assertEqual({error, timeout}, lhttpc:request(URL, get, [], 50)), true = erlang:resume_process(Pid), - ?assertEqual(1, - lhttpc_manager:connection_count({"localhost", Port, false})), {ok, SecondResponse} = lhttpc:request(URL, get, [], 50), ?assertEqual({200, "OK"}, status(SecondResponse)), ?assertEqual(<>, body(SecondResponse)). @@ -484,7 +467,7 @@ partial_upload_chunked() -> ?assertEqual(<>, body(Response1)), ?assertEqual("This is chunky stuff!", lhttpc_lib:header_value("x-test-orig-body", headers(Response1))), - ?assertEqual(element(2, Trailer), + ?assertEqual(element(2, Trailer), lhttpc_lib:header_value("x-test-orig-trailer-1", headers(Response1))), % Make sure it works with no body part in the original request as well Headers = [{"Transfer-Encoding", "chunked"}], @@ -497,7 +480,7 @@ partial_upload_chunked() -> ?assertEqual(<>, body(Response2)), ?assertEqual("This is chunky stuff!", lhttpc_lib:header_value("x-test-orig-body", headers(Response2))), - ?assertEqual(element(2, Trailer), + ?assertEqual(element(2, Trailer), lhttpc_lib:header_value("x-test-orig-trailer-1", headers(Response2))). partial_upload_chunked_no_trailer() -> @@ -681,10 +664,6 @@ ssl_chunked() -> ?assertEqual("2", lhttpc_lib:header_value("Trailer-2", headers(SecondResponse))). -connection_count() -> - timer:sleep(50), % give the TCP stack time to deliver messages - ?assertEqual(0, lhttpc_manager:connection_count()). - invalid_options() -> ?assertError({bad_options, [{foo, bar}, bad_option]}, lhttpc:request("http://localhost/", get, [], <<>>, 1000,