From bc9e58962ba0f1d835cd01a0d426fce1b738698f Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 10 Apr 2009 08:40:13 +0000 Subject: [PATCH] optimized unit tests --- test/Jamfile | 21 ++++++++++----------- test/main.cpp | 8 +------- test/setup_transfer.cpp | 16 +++++++++++++++- test/setup_transfer.hpp | 2 +- test/test_pe_crypto.cpp | 2 +- test/test_primitives.cpp | 8 ++++---- test/test_transfer.cpp | 13 +++++++++---- 7 files changed, 41 insertions(+), 29 deletions(-) diff --git a/test/Jamfile b/test/Jamfile index 0017de7cf..b7a211aad 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -2,33 +2,32 @@ use-project /torrent : .. ; lib test_common : - main.cpp setup_transfer.cpp : - static + /torrent//torrent multi : - static multi - /torrent//torrent ; +exe test_upnp : test_upnp.cpp /torrent//torrent + : shared multi on full ; + +exe test_natpmp : test_natpmp.cpp /torrent//torrent + : shared multi on full ; + project : requirements test_common + /torrent//torrent + main.cpp : default-build multi full on ; -exe test_upnp : test_upnp.cpp /torrent//torrent - : static multi on full ; - -exe test_natpmp : test_natpmp.cpp /torrent//torrent - : static multi on full ; - -test-suite libtorrent : +test-suite libtorrent : [ run test_auto_unchoke.cpp ] [ run test_http_connection.cpp ] [ run test_buffer.cpp ] diff --git a/test/main.cpp b/test/main.cpp index 03f6c792a..839cf7287 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -35,13 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. int test_main(); -bool tests_failure = false; - -void report_failure(char const* err, char const* file, int line) -{ - std::cerr << "\033[31m" << file << ":" << line << " \"" << err << "\"\033[0m\n"; - tests_failure = true; -} +extern bool tests_failure; int main() { diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 9bf5a7174..f39bd38f0 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -51,6 +51,14 @@ using boost::filesystem::create_directory; using namespace libtorrent; namespace sf = boost::filesystem; +bool tests_failure = false; + +void report_failure(char const* err, char const* file, int line) +{ + std::cerr << "\033[31m" << file << ":" << line << " \"" << err << "\"\033[0m\n"; + tests_failure = true; +} + bool print_alerts(libtorrent::session& ses, char const* name , bool allow_disconnects, bool allow_no_torrents, bool allow_failed_fastresume , bool (*predicate)(libtorrent::alert*)) @@ -253,6 +261,12 @@ setup_transfer(session* ses1, session* ses2, session* ses3 assert(ses1); assert(ses2); + session_settings sess_set; + sess_set.ignore_limits_on_local_network = false; + ses1->set_settings(sess_set); + ses2->set_settings(sess_set); + if (ses3) ses3->set_settings(sess_set); + std::srand(time(0)); peer_id pid; std::generate(&pid[0], &pid[0] + 20, std::rand); @@ -272,7 +286,7 @@ setup_transfer(session* ses1, session* ses2, session* ses3 { create_directory("./tmp1" + suffix); std::ofstream file(("./tmp1" + suffix + "/temporary").c_str()); - t = ::create_torrent(&file, piece_size, 1024 / 8); + t = ::create_torrent(&file, piece_size, 19); file.close(); if (clear_files) { diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index a6e2fae88..0f75f0669 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -50,7 +50,7 @@ bool print_alerts(libtorrent::session& ses, char const* name void test_sleep(int millisec); -boost::intrusive_ptr create_torrent(std::ostream* file = 0, int piece_size = 16 * 1024, int num_pieces = 1024 / 8); +boost::intrusive_ptr create_torrent(std::ostream* file = 0, int piece_size = 16 * 1024, int num_pieces = 13); boost::tuple= 0 && dist < 160); TEST_CHECK(dist == ((i == j)?0:(std::max)(i, j))); - for (int k = 0; k < 160; k += 4) + for (int k = 0; k < 160; k += 8) { node_id c(0); c[(159-k) / 8] = 1 << (k & 7); @@ -676,7 +676,7 @@ int test_main() node_id tmp; node_id diff = to_hash("00001f7459456a9453f8719b09547c11d5f34064"); std::vector nodes; - for (int i = 0; i < 10000; ++i) + for (int i = 0; i < 1000; ++i) { table.node_seen(tmp, udp::endpoint(address_v4::any(), rand())); add_and_replace(tmp, diff); diff --git a/test/test_transfer.cpp b/test/test_transfer.cpp index 16634022e..c4a0795c6 100644 --- a/test/test_transfer.cpp +++ b/test/test_transfer.cpp @@ -59,7 +59,7 @@ void test_rate() create_directory("./tmp1_transfer"); std::ofstream file("./tmp1_transfer/temporary"); - boost::intrusive_ptr t = ::create_torrent(&file, 4 * 1024 * 1024, 50); + boost::intrusive_ptr t = ::create_torrent(&file, 4 * 1024 * 1024, 7); file.close(); boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 @@ -134,13 +134,18 @@ void test_transfer() // set half of the pieces to priority 0 int num_pieces = tor2.get_torrent_info().num_pieces(); std::vector priorities(num_pieces, 1); - std::fill(priorities.begin(), priorities.begin() + num_pieces / 2, 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(std::cerr, ", ")); + std::cerr << std::endl; ses1.set_alert_mask(alert::all_categories & ~alert::progress_notification); ses2.set_alert_mask(alert::all_categories & ~alert::progress_notification); ses1.set_alert_dispatch(&print_alert); + ses2.set_download_rate_limit(tor2.get_torrent_info().piece_length() / 2); + // also test to move the storage of the downloader and the uploader // to make sure it can handle switching paths bool test_move_storage = false; @@ -165,8 +170,6 @@ void test_transfer() << st2.num_peers << std::endl; - if (tor2.is_finished()) break; - if (!test_move_storage && st2.progress > 0.25f) { test_move_storage = true; @@ -175,6 +178,8 @@ void test_transfer() std::cerr << "moving storage" << std::endl; } + if (tor2.is_finished()) break; + TEST_CHECK(st1.state == torrent_status::seeding || st1.state == torrent_status::checking_files); TEST_CHECK(st2.state == torrent_status::downloading);