From c4422cd34e33f12dfbda87e0f7cbf1e9974ad4be Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 24 Dec 2010 00:37:01 +0000 Subject: [PATCH] optimize test_transfer. fix set_upload_mode() bug for seeds. clean up some verbose logging. fix tests to not fail by EAGAIN on stdout --- src/peer_connection.cpp | 28 ++++++++++++++++---------- src/session_impl.cpp | 4 ++++ test/main.cpp | 8 ++++++++ test/setup_transfer.cpp | 38 ++++++++++++++++++++++++++++++----- test/setup_transfer.hpp | 1 + test/test_transfer.cpp | 44 +++++++++++++++++++++++++++++------------ 6 files changed, 95 insertions(+), 28 deletions(-) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 74598d0bf..892ba1d13 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -196,9 +196,10 @@ namespace libtorrent error_code ec; m_logger = m_ses.create_log(m_remote.address().to_string(ec) + "_" + to_string(m_remote.port()).elems, m_ses.listen_port()); - peer_log(">>> OUTGOING_CONNECTION [ ep: %s transport: %s ]" + peer_log(">>> OUTGOING_CONNECTION [ ep: %s transport: %s seed: %d p: %p]" , print_endpoint(m_remote).c_str() - , (m_socket->get()) ? "uTP connection" : "TCP connection"); + , (m_socket->get()) ? "uTP connection" : "TCP connection" + , m_peer_info ? m_peer_info->seed : 0, m_peer_info); #endif #ifdef TORRENT_DEBUG piece_failed = false; @@ -772,7 +773,7 @@ namespace libtorrent if (m_num_pieces == int(m_have_piece.size())) { #ifdef TORRENT_VERBOSE_LOGGING - peer_log("*** on_metadata(): THIS IS A SEED ***"); + peer_log("*** on_metadata(): THIS IS A SEED [ p: %p ]", m_peer_info); #endif // if this is a web seed. we don't have a peer_info struct t->get_policy().set_seed(m_peer_info, true); @@ -840,7 +841,7 @@ namespace libtorrent if (m_num_pieces == int(m_have_piece.size())) { #ifdef TORRENT_VERBOSE_LOGGING - peer_log("*** THIS IS A SEED ***"); + peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info); #endif // if this is a web seed. we don't have a peer_info struct t->get_policy().set_seed(m_peer_info, true); @@ -1727,6 +1728,9 @@ namespace libtorrent // decrement the piece count without first incrementing it if (is_seed()) { +#ifdef TORRENT_VERBOSE_LOGGING + peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info); +#endif t->seen_complete(); t->get_policy().set_seed(m_peer_info, true); m_upload_only = true; @@ -1804,6 +1808,10 @@ namespace libtorrent // (since it doesn't exist yet) if (!t->ready_for_connections()) { +#ifdef TORRENT_VERBOSE_LOGGING + if (m_num_pieces == int(bits.size())) + peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info); +#endif m_have_piece = bits; m_num_pieces = bits.count(); t->get_policy().set_seed(m_peer_info, m_num_pieces == int(bits.size())); @@ -1816,7 +1824,7 @@ namespace libtorrent if (num_pieces == int(m_have_piece.size())) { #ifdef TORRENT_VERBOSE_LOGGING - peer_log("*** THIS IS A SEED ***"); + peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info); #endif // if this is a web seed. we don't have a peer_info struct t->get_policy().set_seed(m_peer_info, true); @@ -2577,14 +2585,14 @@ namespace libtorrent m_have_all = true; +#ifdef TORRENT_VERBOSE_LOGGING + peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info); +#endif + t->get_policy().set_seed(m_peer_info, true); m_upload_only = true; m_bitfield_received = true; -#ifdef TORRENT_VERBOSE_LOGGING - peer_log("*** THIS IS A SEED ***"); -#endif - // if we don't have metadata yet // just remember the bitmask // don't update the piecepicker @@ -5560,7 +5568,7 @@ namespace libtorrent { // if the peer is a seed, don't allow setting // upload_only to false - if (m_upload_only && is_seed()) return; + if (m_upload_only || is_seed()) return; m_upload_only = u; boost::shared_ptr t = associated_torrent().lock(); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 81db5ce32..dee7de525 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -819,6 +819,10 @@ namespace aux { url_random((char*)&m_peer_id[print.length()], (char*)&m_peer_id[0] + 20); +#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING + (*m_logger) << time_now_string() << " generated peer ID: " << m_peer_id.to_string() << "\n"; +#endif + update_rate_settings(); update_connections_limit(); update_unchoke_limit(); diff --git a/test/main.cpp b/test/main.cpp index 839cf7287..4bfaa095f 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -39,6 +39,14 @@ extern bool tests_failure; int main() { +#ifdef O_NONBLOCK + // on darwin, stdout is set to non-blocking mode by default + // which sometimes causes tests to fail with EAGAIN just + // by printing logs + int flags = fcntl(stdout, F_GETFL, 0); + fcntl(stdout, F_SETFL, flags & ~O_NONBLOCK); +#endif + #ifndef BOOST_NO_EXCEPTIONS try { diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 07394cda6..4bcf830e1 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -114,6 +114,27 @@ bool print_alerts(libtorrent::session& ses, char const* name return ret; } +bool listen_done = false; +bool listen_alert(libtorrent::alert* a) +{ + if (alert_cast(a) + || alert_cast(a)) + listen_done = true; + return true; +} + +void wait_for_listen(libtorrent::session& ses, char const* name) +{ + listen_done = false; + alert const* a = 0; + do + { + print_alerts(ses, name, true, true, true, &listen_alert); + if (listen_done) break; + a = ses.wait_for_alert(milliseconds(500)); + } while (a); +} + void test_sleep(int millisec) { libtorrent::sleep(millisec); @@ -163,7 +184,7 @@ void start_proxy(int port, int proxy_type) "SERVER=%s %s" , port, type, auth); - fprintf(stderr, "starting delegated proxy on port %d...\n", port); + fprintf(stderr, "starting delegated proxy on port %d (%s %s)...\n", port, type, auth); system(buf); fprintf(stderr, "launched\n"); // apparently delegate takes a while to open its listen port @@ -235,9 +256,14 @@ setup_transfer(session* ses1, session* ses2, session* ses3 assert(ses1); assert(ses2); + ses1->stop_lsd(); + ses2->stop_lsd(); + if (ses3) ses3->stop_lsd(); + session_settings sess_set = ses1->settings(); - sess_set.allow_multiple_connections_per_ip = true; + if (ses3) sess_set.allow_multiple_connections_per_ip = true; sess_set.ignore_limits_on_local_network = false; + sess_set.max_failcount = 1; ses1->set_settings(sess_set); ses2->set_settings(sess_set); if (ses3) ses3->set_settings(sess_set); @@ -286,6 +312,8 @@ setup_transfer(session* ses1, session* ses2, session* ses3 // use the same files sha1_hash info_hash = t->info_hash(); add_torrent_params param; + param.paused = false; + param.auto_managed = false; if (p) param = *p; param.ti = clone_ptr(t); param.save_path = "./tmp1" + suffix; @@ -326,7 +354,7 @@ setup_transfer(session* ses1, session* ses2, session* ses3 assert(ses1->get_torrents().size() == 1); assert(ses2->get_torrents().size() == 1); - test_sleep(100); +// test_sleep(100); if (connect_peers) { @@ -388,7 +416,7 @@ int start_tracker() libtorrent::mutex::scoped_lock l(tracker_lock); tracker_initialized.wait(l); } - test_sleep(100); +// test_sleep(100); return port; } @@ -551,7 +579,7 @@ int start_web_server(bool ssl, bool chunked_encoding) // "relative/../test_file" can resolve error_code ec; create_directory("relative", ec); - test_sleep(100); +// test_sleep(100); return port; } diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 17153710b..771e94b9e 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -48,6 +48,7 @@ bool print_alerts(libtorrent::session& ses, char const* name , bool allow_failed_fastresume = false , bool (*)(libtorrent::alert*) = 0); +void wait_for_listen(libtorrent::session& ses, char const* name); void test_sleep(int millisec); boost::intrusive_ptr create_torrent(std::ostream* file = 0 diff --git a/test/test_transfer.cpp b/test/test_transfer.cpp index 02017fd85..9c0d59ac1 100644 --- a/test/test_transfer.cpp +++ b/test/test_transfer.cpp @@ -59,8 +59,11 @@ void test_rate() remove_all("./tmp1_transfer_moved", ec); remove_all("./tmp2_transfer_moved", ec); - session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48575, 49000), "0.0.0.0", 0); - session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49575, 50000), "0.0.0.0", 0); + int alert_mask = alert::all_categories + & ~alert::progress_notification + & ~alert::stats_notification; + 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; @@ -70,16 +73,12 @@ void test_rate() boost::intrusive_ptr t = ::create_torrent(&file, 4 * 1024 * 1024, 7); file.close(); + wait_for_listen(ses1, "ses1"); + wait_for_listen(ses2, "ses1"); + boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 , true, false, true, "_transfer", 0, &t); - ses1.set_alert_mask(alert::all_categories - & ~alert::progress_notification - & ~alert::stats_notification); - ses2.set_alert_mask(alert::all_categories - & ~alert::progress_notification - & ~alert::stats_notification); - ptime start = time_now(); for (int i = 0; i < 70; ++i) @@ -262,6 +261,7 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe } session_settings sett; + sett.allow_multiple_connections_per_ip = false; if (test_allowed_fast) { @@ -269,7 +269,12 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe sett.unchoke_slots_limit = 0; } - sett.min_reconnect_time = 1; + // we need a short reconnect time since we + // finish the torrent and then restart it + // immediately to complete the second half. + // using a reconnect time > 0 will just add + // to the time it will take to complete the test + sett.min_reconnect_time = 0; sett.announce_to_all_trackers = true; sett.announce_to_all_tiers = true; // make sure we announce to both http and udp trackers @@ -307,6 +312,8 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe t->add_tracker(tracker_url); add_torrent_params addp(&test_storage_constructor); + addp.paused = false; + addp.auto_managed = false; // test using piece sizes smaller than 16kB boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 @@ -329,9 +336,9 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe & ~alert::stats_notification); // ses1.set_alert_dispatch(&print_alert); - sett = ses2.settings(); - sett.download_rate_limit = tor2.get_torrent_info().piece_length() * 5; - ses2.set_settings(sett); +// 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 @@ -381,6 +388,14 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe if (!test_disk_full && st2.is_finished) break; + if (st2.state != torrent_status::downloading) + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating", "checking (r)"}; + std::cerr << "st2 state: " << state_str[st2.state] << std::endl; + } + TEST_CHECK(st1.state == torrent_status::seeding || st1.state == torrent_status::checking_files); TEST_CHECK(st2.state == torrent_status::downloading @@ -396,6 +411,7 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe 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(); @@ -471,6 +487,8 @@ void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowe std::cout << "re-adding" << std::endl; add_torrent_params p; + p.paused = false; + p.auto_managed = false; p.ti = t; p.save_path = "./tmp2_transfer_moved"; p.resume_data = &resume_data;