split out test_priority from test_transfer

This commit is contained in:
Arvid Norberg
2013-10-10 17:00:32 +00:00
parent 3b882a35de
commit 9ec2f48a6e
4 changed files with 403 additions and 289 deletions

View File

@@ -69,88 +69,6 @@ bool on_alert(alert* a)
return false;
}
// test the maximum transfer rate
void test_rate()
{
// in case the previous run was terminated
error_code ec;
remove_all("tmp1_transfer", ec);
remove_all("tmp2_transfer", ec);
remove_all("tmp1_transfer_moved", ec);
remove_all("tmp2_transfer_moved", ec);
// these are declared before the session objects
// so that they are destructed last. This enables
// the sessions to destruct in parallel
session_proxy p1;
session_proxy p2;
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48575, 49000), "0.0.0.0", 0, alert_mask);
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49575, 50000), "0.0.0.0", 0, alert_mask);
torrent_handle tor1;
torrent_handle tor2;
create_directory("tmp1_transfer", ec);
std::ofstream file("tmp1_transfer/temporary");
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 4 * 1024 * 1024, 7);
file.close();
wait_for_listen(ses1, "ses1");
wait_for_listen(ses2, "ses2");
peer_disconnects = 0;
session_settings sett = high_performance_seed();
sett.enable_outgoing_utp = true;
sett.enable_incoming_utp = true;
sett.enable_outgoing_tcp = false;
sett.enable_incoming_tcp = false;
ses1.set_settings(sett);
ses2.set_settings(sett);
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0
, true, false, true, "_transfer", 0, &t);
ptime start = time_now();
// it shouldn't take more than 7 seconds
for (int i = 0; i < 70; ++i)
{
print_alerts(ses1, "ses1", true, true, true, &on_alert);
print_alerts(ses2, "ses2", true, true, true, &on_alert);
torrent_status st1 = tor1.status();
torrent_status st2 = tor2.status();
if (i % 10 == 0)
print_ses_rate(i / 10.f, &st1, &st2);
if (peer_disconnects >= 1) break;
if (st2.is_seeding) break;
test_sleep(100);
}
TEST_CHECK(tor2.status().is_seeding);
time_duration dt = time_now() - start;
std::cerr << "downloaded " << t->total_size() << " bytes "
"in " << (total_milliseconds(dt) / 1000.f) << " seconds" << std::endl;
std::cerr << "average download rate: " << (t->total_size() / (std::max)(total_milliseconds(dt), 1))
<< " kB/s" << std::endl;
// this allows shutting down the sessions in parallel
p1 = ses1.abort();
p2 = ses2.abort();
}
void print_alert(std::auto_ptr<alert>)
{
std::cout << "ses1 (alert dispatch function): "/* << a.message() */ << std::endl;
}
// simulate a full disk
struct test_storage : storage_interface
{
@@ -252,14 +170,13 @@ storage_interface* test_storage_constructor(file_storage const& fs
return new test_storage(fs, path, fp);
}
void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowed_fast = false, bool test_priorities = false)
void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowed_fast = false)
{
char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
fprintf(stderr, "\n\n ==== TESTING %s proxy ==== disk-full: %s allow-fast: %s priorities: %s\n\n\n"
, test_name[proxy_type], test_disk_full ? "true": "false", test_allowed_fast ? "true" : "false"
, test_priorities ? "true" : "false");
fprintf(stderr, "\n\n ==== TESTING %s proxy ==== disk-full: %s allow-fast: %s\n\n\n"
, test_name[proxy_type], test_disk_full ? "true": "false", test_allowed_fast ? "true" : "false");
// in case the previous run was terminated
error_code ec;
@@ -342,18 +259,7 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 16 * 1024, 13, false);
file.close();
if (test_priorities)
{
int udp_tracker_port = start_tracker();
int tracker_port = start_web_server();
char tracker_url[200];
snprintf(tracker_url, sizeof(tracker_url), "http://127.0.0.1:%d/announce", tracker_port);
t->add_tracker(tracker_url);
snprintf(tracker_url, sizeof(tracker_url), "udp://127.0.0.1:%d/announce", udp_tracker_port);
t->add_tracker(tracker_url);
}
TEST_CHECK(exists(combine_path("tmp1_transfer", "temporary")));
add_torrent_params addp(&test_storage_constructor);
addp.flags &= ~add_torrent_params::flag_paused;
@@ -370,21 +276,6 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe
int num_pieces = tor2.torrent_file()->num_pieces();
std::vector<int> priorities(num_pieces, 1);
if (test_priorities)
{
// set half of the pieces to priority 0
std::fill(priorities.begin(), priorities.begin() + (num_pieces / 2), 0);
tor2.prioritize_pieces(priorities);
std::cerr << "setting priorities: ";
std::copy(priorities.begin(), priorities.end(), std::ostream_iterator<int>(std::cerr, ", "));
std::cerr << std::endl;
}
// ses1.set_alert_dispatch(&print_alert);
// sett = ses2.settings();
// sett.download_rate_limit = tor2.get_torrent_info().piece_length() * 5;
// ses2.set_settings(sett);
// also test to move the storage of the downloader and the uploader
// to make sure it can handle switching paths
@@ -450,194 +341,19 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe
test_sleep(100);
}
if (test_priorities)
{
// 1 announce per tracker to start
TEST_CHECK(tracker_responses >= 2);
TEST_CHECK(!tor2.status().is_seeding);
TEST_CHECK(tor2.status().is_finished);
if (tor2.status().is_finished)
std::cerr << "torrent is finished (50% complete)" << std::endl;
else return;
std::cerr << "force recheck" << std::endl;
tor2.force_recheck();
peer_disconnects = 0;
for (int i = 0; i < 50; ++i)
{
print_alerts(ses2, "ses2", true, true, true, &on_alert);
torrent_status st2 = tor2.status();
if (i % 10 == 0)
{
std::cerr << int(st2.progress * 100) << "% " << std::endl;
}
if (st2.state != torrent_status::checking_files) break;
if (peer_disconnects >= 1) break;
test_sleep(100);
}
std::vector<int> priorities2 = tor2.piece_priorities();
TEST_CHECK(std::equal(priorities.begin(), priorities.end(), priorities2.begin()));
peer_disconnects = 0;
for (int i = 0; i < 5; ++i)
{
print_alerts(ses2, "ses2", true, true, true, &on_alert);
torrent_status st2 = tor2.status();
TEST_CHECK(st2.state == torrent_status::finished);
if (peer_disconnects >= 1) break;
test_sleep(100);
}
tor2.pause();
alert const* a = ses2.wait_for_alert(seconds(10));
bool got_paused_alert = false;
while (a)
{
std::auto_ptr<alert> holder = ses2.pop_alert();
std::cerr << "ses2: " << a->message() << std::endl;
if (alert_cast<torrent_paused_alert>(a))
{
got_paused_alert = true;
break;
}
a = ses2.wait_for_alert(seconds(10));
}
TEST_CHECK(got_paused_alert);
std::vector<announce_entry> tr = tor2.trackers();
tr.push_back(announce_entry("http://test.com/announce"));
tor2.replace_trackers(tr);
tr.clear();
tor2.save_resume_data();
std::vector<char> resume_data;
a = ses2.wait_for_alert(seconds(10));
while (a)
{
std::auto_ptr<alert> holder = ses2.pop_alert();
std::cerr << "ses2: " << a->message() << std::endl;
if (alert_cast<save_resume_data_alert>(a))
{
bencode(std::back_inserter(resume_data)
, *alert_cast<save_resume_data_alert>(a)->resume_data);
break;
}
a = ses2.wait_for_alert(seconds(10));
}
TEST_CHECK(resume_data.size());
std::cerr << "saved resume data" << std::endl;
ses2.remove_torrent(tor2);
std::cerr << "removed" << std::endl;
test_sleep(100);
std::cout << "re-adding" << std::endl;
add_torrent_params p;
p.flags &= ~add_torrent_params::flag_paused;
p.flags &= ~add_torrent_params::flag_auto_managed;
p.ti = t;
p.save_path = "tmp2_transfer_moved";
p.resume_data = resume_data;
tor2 = ses2.add_torrent(p, ec);
ses2.set_alert_mask(alert::all_categories
& ~alert::progress_notification
& ~alert::stats_notification);
tor2.prioritize_pieces(priorities);
std::cout << "resetting priorities" << std::endl;
tor2.resume();
tr = tor2.trackers();
TEST_CHECK(std::find_if(tr.begin(), tr.end()
, boost::bind(&announce_entry::url, _1) == "http://test.com/announce") != tr.end());
peer_disconnects = 0;
for (int i = 0; i < 5; ++i)
{
print_alerts(ses1, "ses1", true, true, true, &on_alert);
print_alerts(ses2, "ses2", true, true, true, &on_alert);
torrent_status st1 = tor1.status();
torrent_status st2 = tor2.status();
TEST_CHECK(st1.state == torrent_status::seeding);
TEST_CHECK(st2.state == torrent_status::finished);
if (peer_disconnects >= 1) break;
if (st2.is_finished) break;
test_sleep(100);
}
TEST_CHECK(!tor2.status().is_seeding);
std::fill(priorities.begin(), priorities.end(), 1);
tor2.prioritize_pieces(priorities);
std::cout << "setting priorities to 1" << std::endl;
TEST_EQUAL(tor2.status().is_finished, false);
peer_disconnects = 0;
for (int i = 0; i < 130; ++i)
{
print_alerts(ses1, "ses1", true, true, true, &on_alert);
print_alerts(ses2, "ses2", true, true, true, &on_alert);
torrent_status st1 = tor1.status();
torrent_status st2 = tor2.status();
if (i % 10 == 0)
print_ses_rate(i / 10.f, &st1, &st2);
if (st2.is_seeding) break;
TEST_EQUAL(st1.state, torrent_status::seeding);
TEST_EQUAL(st2.state, torrent_status::downloading);
if (peer_disconnects >= 1) break;
test_sleep(100);
}
}
TEST_CHECK(tor2.status().is_seeding);
// this allows shutting down the sessions in parallel
p1 = ses1.abort();
p2 = ses2.abort();
if (test_priorities)
{
stop_tracker();
stop_web_server();
}
if (proxy_type) stop_proxy(ps.port);
}
int test_main()
{
using namespace libtorrent;
#if !defined TORRENT_DEBUG \
&& defined TORRENT_DISABLE_INVARIANT_CHECKS \
&& !defined _GLIBCXX_DEBUG
// test rate only makes sense in release mode
test_rate();
#endif
// test with all kinds of proxies
for (int i = 0; i < 6; ++i)
test_transfer(i);
@@ -646,7 +362,7 @@ int test_main()
test_transfer(0, true, true);
// test allowed fast
test_transfer(0, false, true, true);
test_transfer(0, false, true);
error_code ec;
remove_all("tmp1_transfer", ec);