a bunch of fixes to make test_web_seeds a lot faster, and fail slightly fewer tests

This commit is contained in:
Arvid Norberg
2013-08-19 03:54:45 +00:00
parent 61be6b6ec8
commit 1afc0c6740
12 changed files with 172 additions and 102 deletions

View File

@@ -158,9 +158,12 @@ int main()
fflush(stdout);
fflush(stderr);
remove_all(test_dir, ec);
if (ec)
fprintf(stderr, "failed to remove test dir: %s\n", ec.message().c_str());
if (!tests_failure)
{
remove_all(test_dir, ec);
if (ec)
fprintf(stderr, "failed to remove test dir: %s\n", ec.message().c_str());
}
return tests_failure ? 1 : 0;
}

View File

@@ -263,26 +263,30 @@ void test_sleep(int millisec)
libtorrent::sleep(millisec);
}
static std::set<int> running_proxies;
// maps port to proxy type
static std::map<int, 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);
// don't shut down proxies until the test is
// completely done. This saves a lot of time.
// they're closed at the end of main() by
// calling stop_all_proxies().
}
void stop_all_proxies()
{
std::set<int> proxies = running_proxies;
for (std::set<int>::iterator i = proxies.begin()
std::map<int, int> proxies = running_proxies;
for (std::map<int, int>::iterator i = proxies.begin()
, end(proxies.end()); i != end; ++i)
{
stop_proxy(*i);
char buf[100];
snprintf(buf, sizeof(buf), "delegated -P%d -Fkill", i->first);
int ret = system(buf);
if (ret == 0)
perror("system");
else
running_proxies.erase(i->first);
}
}
@@ -290,9 +294,13 @@ int start_proxy(int proxy_type)
{
using namespace libtorrent;
int port = 10000 + (rand() % 50000);
for (std::map<int, int>::iterator i = running_proxies.begin()
, end(running_proxies.end()); i != end; ++i)
{
if (i->second == proxy_type) return i->first;
}
stop_proxy(port);
int port = 10000 + (rand() % 50000);
char const* type = "";
char const* auth = "";
@@ -332,7 +340,7 @@ int start_proxy(int proxy_type)
fprintf(stderr, "failed (%d) %s\n", errno, strerror(errno));
exit(1);
}
running_proxies.insert(port);
running_proxies.insert(std::make_pair(port, proxy_type));
fprintf(stderr, "%s launched\n", time_now_string());
// apparently delegate takes a while to open its listen port
test_sleep(500);

View File

@@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bencode.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/thread.hpp"
#include "libtorrent/alert_types.hpp"
#include <boost/tuple/tuple.hpp>
#include <fstream>
#include <iostream>
@@ -46,31 +47,36 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace libtorrent;
int peer_disconnects = 0;
bool on_alert(alert* a)
{
if (alert_cast<peer_disconnected_alert>(a))
++peer_disconnects;
else if (alert_cast<peer_error_alert>(a))
++peer_disconnects;
return false;
}
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file
void test_transfer(session& ses, boost::intrusive_ptr<torrent_info> torrent_file
, int proxy, int port, char const* protocol, bool url_seed, bool chunked_encoding, bool test_ban)
{
using namespace libtorrent;
session ses(fingerprint(" ", 0,0,0,0), 0);
session_settings settings;
settings.max_queued_disk_bytes = 256 * 1024;
ses.set_settings(settings);
ses.set_alert_mask(~(alert::progress_notification | alert::stats_notification));
error_code ec;
ses.listen_on(std::make_pair(51000, 52000), ec);
if (ec) fprintf(stderr, "listen_on failed: %s\n", ec.message().c_str());
remove_all("tmp2_web_seed", ec);
char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
fprintf(stderr, "\n\n ==== TESTING === proxy: %s ==== protocol: %s ==== seed: %s === transfer-encoding: %s === corruption: %s\n\n\n"
, test_name[proxy], protocol, url_seed ? "URL seed" : "HTTP seed", chunked_encoding ? "chunked": "none", test_ban ? "yes" : "no");
proxy_settings ps;
if (proxy)
{
proxy_settings ps;
ps.port = start_proxy(proxy);
ps.hostname = "127.0.0.1";
ps.username = "testuser";
@@ -107,7 +113,9 @@ void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file
if (f.pad_file) pad_file_size += f.size;
}
for (int i = 0; i < 30; ++i)
peer_disconnects = 0;
for (int i = 0; i < 40; ++i)
{
torrent_status s = th.status();
session_status ss = ses.status();
@@ -117,41 +125,35 @@ void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file
cs = ses.get_cache_status();
if (cs.blocks_read < 1) cs.blocks_read = 1;
if (cs.blocks_written < 1) cs.blocks_written = 1;
/*
std::cerr << (s.progress * 100.f) << " %"
<< " torrent rate: " << (s.download_rate / 1000.f) << " kB/s"
<< " session rate: " << (ss.download_rate / 1000.f) << " kB/s"
<< " session total: " << ss.total_payload_download
<< " torrent total: " << s.total_payload_download
<< " rate sum:" << ses_rate_sum
<< " cache: " << cs.cache_size
<< " rcache: " << cs.read_cache_size
<< " buffers: " << cs.total_used_buffers
<< std::endl;
*/
print_alerts(ses, " >> ses", test_ban, false, false, 0, true);
if (test_ban && th.url_seeds().empty())
print_ses_rate(i / 10.f, &s, NULL);
print_alerts(ses, " >> ses", test_ban, false, false, &on_alert);
if (test_ban && th.url_seeds().empty() && th.http_seeds().empty())
{
// when we don't have any web seeds left, we know we successfully banned it
break;
}
// if (test_ban && peer_disconnects >= 1) break;
if (s.is_seeding /* && ss.download_rate == 0.f*/)
{
TEST_EQUAL(s.total_payload_download - s.total_redundant_bytes, total_size - pad_file_size);
// we need to sleep here a bit to let the session sync with the torrent stats
test_sleep(1000);
TEST_EQUAL(ses.status().total_payload_download - ses.status().total_redundant_bytes
, total_size - pad_file_size);
// commented out because it takes such a long time
// test_sleep(1000);
// TEST_EQUAL(ses.status().total_payload_download - ses.status().total_redundant_bytes
// , total_size - pad_file_size);
break;
}
test_sleep(500);
test_sleep(100);
}
// for test_ban tests, make sure we removed
// the url seed (i.e. banned it)
TEST_CHECK(!test_ban || th.url_seeds().empty());
TEST_CHECK(!test_ban || (th.url_seeds().empty() && th.http_seeds().empty()));
TEST_EQUAL(cs.cache_size, 0);
TEST_EQUAL(cs.total_used_buffers, 0);
@@ -172,7 +174,14 @@ void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file
// otherwise, we are supposed to have
TEST_CHECK(th.status().is_seeding == !test_ban);
if (proxy) stop_proxy(8002);
if (proxy) stop_proxy(ps.port);
ses.remove_torrent(th);
// call this to synchronize with the network thread
ses.status();
print_alerts(ses, " >> ses", true, true, false, &on_alert, true);
TEST_CHECK(exists(combine_path("tmp2_web_seed", torrent_file->files().file_path(0))) || test_ban);
remove_all("tmp2_web_seed", ec);
@@ -209,6 +218,8 @@ sha1_hash file_hash(std::string const& name)
return h.final();
}
const int num_pieces = 9;
// test_url_seed determines whether to use url-seed or http-seed
int run_suite(char const* protocol, bool test_url_seed, bool chunked_encoding, bool test_ban)
{
@@ -232,10 +243,10 @@ int run_suite(char const* protocol, bool test_url_seed, bool chunked_encoding, b
else
{
piece_size = 64 * 1024;
char* random_data = (char*)malloc(64 * 1024 * 25);
std::generate(random_data, random_data + 64 * 1024 * 25, &std::rand);
save_file("tmp1_web_seed/seed", random_data, 64 * 1024 * 25);
fs.add_file("seed", 64 * 1024 * 25);
char* random_data = (char*)malloc(64 * 1024 * num_pieces);
std::generate(random_data, random_data + 64 * 1024 * num_pieces, &std::rand);
save_file("tmp1_web_seed/seed", random_data, 64 * 1024 * num_pieces);
fs.add_file("seed", 64 * 1024 * num_pieces);
free(random_data);
}
@@ -265,8 +276,6 @@ int run_suite(char const* protocol, bool test_url_seed, bool chunked_encoding, b
fprintf(stderr, " %04x: %d %s\n", int(f.offset), f.pad_file, f.path.c_str());
}
// for (int i = 0; i < 1000; ++i) sleep(1000);
// calculate the hash for all pieces
set_piece_hashes(t, "tmp1_web_seed", ec);
@@ -288,9 +297,9 @@ int run_suite(char const* protocol, bool test_url_seed, bool chunked_encoding, b
else
{
piece_size = 64 * 1024;
char* random_data = (char*)malloc(64 * 1024 * 25);
std::generate(random_data, random_data + 64 * 1024 * 25, &std::rand);
save_file("tmp1_web_seed/seed", random_data, 64 * 1024 * 25);
char* random_data = (char*)malloc(64 * 1024 * num_pieces);
std::generate(random_data, random_data + 64 * 1024 * num_pieces, &std::rand);
save_file("tmp1_web_seed/seed", random_data, 64 * 1024 * num_pieces);
free(random_data);
}
}
@@ -315,13 +324,24 @@ int run_suite(char const* protocol, bool test_url_seed, bool chunked_encoding, b
}
}
for (int i = 0; i < 6; ++i)
test_transfer(torrent_file, i, port, protocol, test_url_seed, chunked_encoding, test_ban);
if (test_url_seed)
{
torrent_file->rename_file(0, "tmp2_web_seed/test_torrent_dir/renamed_test1");
test_transfer(torrent_file, 0, port, protocol, test_url_seed, chunked_encoding, test_ban);
session ses(fingerprint(" ", 0,0,0,0), 0);
session_settings settings;
settings.max_queued_disk_bytes = 256 * 1024;
ses.set_settings(settings);
ses.set_alert_mask(~(alert::progress_notification | alert::stats_notification));
error_code ec;
ses.listen_on(std::make_pair(51000, 52000), ec);
if (ec) fprintf(stderr, "listen_on failed: %s\n", ec.message().c_str());
for (int i = 0; i < 6; ++i)
test_transfer(ses, torrent_file, i, port, protocol, test_url_seed, chunked_encoding, test_ban);
if (test_url_seed)
{
torrent_file->rename_file(0, "tmp2_web_seed/test_torrent_dir/renamed_test1");
test_transfer(ses, torrent_file, 0, port, protocol, test_url_seed, chunked_encoding, test_ban);
}
}
stop_web_server();