attempt to make unit tests more likely to clean up proxy processes after themselves

This commit is contained in:
Arvid Norberg 2013-08-03 23:30:37 +00:00
parent 9319c1e073
commit 45607520e9
4 changed files with 41 additions and 15 deletions

View File

@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <stdio.h>
#include <stdlib.h> // 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);

View File

@ -260,14 +260,26 @@ void test_sleep(int millisec)
libtorrent::sleep(millisec);
}
static std::set<int> 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<int> proxies = running_proxies;
for (std::set<int>::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;

View File

@ -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();

View File

@ -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<std::string, std::string>::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;
}