From 45607520e9fd260daf69db40072b1cccfa3f60c0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 3 Aug 2013 23:30:37 +0000 Subject: [PATCH] attempt to make unit tests more likely to clean up proxy processes after themselves --- test/main.cpp | 11 +++++++++++ test/setup_transfer.cpp | 19 ++++++++++++++++--- test/setup_transfer.hpp | 1 + test/test_http_connection.cpp | 25 +++++++++++++------------ 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/test/main.cpp b/test/main.cpp index 4538a0981..bbb81b10a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include // for exit() #include "setup_transfer.hpp" // for tests_failure +#include "dht_server.hpp" // for stop_dht +#include "peer_server.hpp" // for stop_peer int test_main(); @@ -134,6 +136,15 @@ int main() tests_failure = true; } #endif + + // just in case of premature exits + // make sure we try to clean up some + stop_tracker(); + stop_all_proxies(); + stop_web_server(); + stop_peer(); + stop_dht(); + fflush(stdout); fflush(stderr); remove_all(test_dir, ec); diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 76d559ad0..df6a46734 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -260,14 +260,26 @@ void test_sleep(int millisec) libtorrent::sleep(millisec); } +static std::set running_proxies; + void stop_proxy(int port) { char buf[100]; snprintf(buf, sizeof(buf), "delegated -P%d -Fkill", port); int ret = system(buf); if (ret == 0) - { perror("system"); + else + running_proxies.erase(port); +} + +void stop_all_proxies() +{ + std::set proxies = running_proxies; + for (std::set::iterator i = proxies.begin() + , end(proxies.end()); i != end; ++i) + { + stop_proxy(*i); } } @@ -310,14 +322,15 @@ int start_proxy(int proxy_type) "SERVER=%s %s" , port, type, auth); - fprintf(stderr, "starting delegated proxy on port %d (%s %s)...\n", port, type, auth); + fprintf(stderr, "%s starting delegated proxy on port %d (%s %s)...\n", time_now_string(), port, type, auth); int r = system(buf); if (r != 0) { fprintf(stderr, "failed (%d) %s\n", errno, strerror(errno)); exit(1); } - fprintf(stderr, "launched\n"); + running_proxies.insert(port); + fprintf(stderr, "%s launched\n", time_now_string()); // apparently delegate takes a while to open its listen port test_sleep(500); return port; diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 6ccdd030d..f95dd51bb 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -89,6 +89,7 @@ int EXPORT start_web_server(bool ssl = false, bool chunked = false); void EXPORT stop_web_server(); int EXPORT start_proxy(int type); void EXPORT stop_proxy(int port); +void EXPORT stop_all_proxies(); void EXPORT stop_tracker(); int EXPORT start_tracker(); diff --git a/test/test_http_connection.cpp b/test/test_http_connection.cpp index 8e1f10d2b..a72ddd94e 100644 --- a/test/test_http_connection.cpp +++ b/test/test_http_connection.cpp @@ -55,12 +55,12 @@ char data_buffer[4000]; void print_http_header(http_parser const& p) { - std::cerr << " < " << p.status_code() << " " << p.message() << std::endl; + std::cerr << time_now_string() << " < " << p.status_code() << " " << p.message() << std::endl; for (std::multimap::const_iterator i = p.headers().begin(), end(p.headers().end()); i != end; ++i) { - std::cerr << " < " << i->first << ": " << i->second << std::endl; + std::cerr << time_now_string() << " < " << i->first << ": " << i->second << std::endl; } } @@ -69,7 +69,7 @@ void http_connect_handler(http_connection& c) ++connect_handler_called; TEST_CHECK(c.socket().is_open()); error_code ec; - std::cerr << "connected to: " << print_endpoint(c.socket().remote_endpoint(ec)) + std::cerr << time_now_string() << " connected to: " << print_endpoint(c.socket().remote_endpoint(ec)) << std::endl; // this is not necessarily true when using a proxy and proxying hostnames // TEST_CHECK(c.socket().remote_endpoint(ec).address() == address::from_string("127.0.0.1", ec)); @@ -110,7 +110,8 @@ void run_test(std::string const& url, int size, int status, int connected std::cerr << " ===== TESTING: " << url << " =====" << std::endl; - std::cerr << " expecting: size: " << size + std::cerr << time_now_string() + << " expecting: size: " << size << " status: " << status << " connected: " << connected << " error: " << (ec?ec->message():"no error") << std::endl; @@ -121,13 +122,14 @@ void run_test(std::string const& url, int size, int status, int connected ios.reset(); error_code e; ios.run(e); + if (e) std::cerr << time_now_string() << " run failed: " << e.message() << std::endl; - std::cerr << "connect_handler_called: " << connect_handler_called << std::endl; - std::cerr << "handler_called: " << handler_called << std::endl; - std::cerr << "status: " << http_status << std::endl; - std::cerr << "size: " << data_size << std::endl; - std::cerr << "expected-size: " << size << std::endl; - std::cerr << "error_code: " << g_error_code.message() << std::endl; + std::cerr << time_now_string() << " connect_handler_called: " << connect_handler_called << std::endl; + std::cerr << time_now_string() << " handler_called: " << handler_called << std::endl; + std::cerr << time_now_string() << " status: " << http_status << std::endl; + std::cerr << time_now_string() << " size: " << data_size << std::endl; + std::cerr << time_now_string() << " expected-size: " << size << std::endl; + std::cerr << time_now_string() << " error_code: " << g_error_code.message() << std::endl; TEST_CHECK(connect_handler_called == connected); TEST_CHECK(handler_called == 1); TEST_CHECK(data_size == size || size == -1); @@ -175,7 +177,6 @@ void run_suite(std::string const& protocol, proxy_settings ps, int port) else run_test(protocol + "://non-existent-domain.se/non-existing-file", -1, -1, 0, err(), ps); } - if (ps.type != proxy_settings::none) stop_proxy(ps.port); } @@ -203,6 +204,7 @@ int test_main() int port = 0; port = start_web_server(); + for (int i = 0; i < 5; ++i) { ps.type = (proxy_settings::proxy_type)i; @@ -226,7 +228,6 @@ int test_main() run_suite("http", ps, port); stop_web_server(); - std::remove("test_file"); return 0; }