From fd20958e3d4ef9224d6c5c7a8902bc820c878894 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 27 Jan 2015 22:18:13 +0000 Subject: [PATCH] polish unit tests --- test/test_privacy.cpp | 21 ++++++++++++++++----- test/test_resume.cpp | 20 ++++++++++++-------- test/test_utp.cpp | 8 +++++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/test/test_privacy.cpp b/test/test_privacy.cpp index 9c3d64ce1..5a6f5dd63 100644 --- a/test/test_privacy.cpp +++ b/test/test_privacy.cpp @@ -77,6 +77,7 @@ enum flags_t expect_dht_msg = 32, expect_peer_connection = 64, expect_possible_udp_connection = 128, + expect_possible_dht_msg = 256, }; session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags) @@ -194,13 +195,23 @@ session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags) { TEST_EQUAL(num_udp_announces(), prev_udp_announces + bool(flags & expect_udp_connection)); } - if (flags & expect_dht_msg) + + if (flags & expect_possible_udp_connection) { - TEST_CHECK(num_dht_hits() > 0); + // this flag is true if we may fail open, but also might not have had + // enough time to fail yet + TEST_CHECK(num_dht_hits() == 0 || num_dht_hits() == 1); } else { - TEST_EQUAL(num_dht_hits(), 0); + if (flags & expect_dht_msg) + { + TEST_CHECK(num_dht_hits() > 0); + } + else + { + TEST_EQUAL(num_dht_hits(), 0); + } } if (flags & expect_peer_connection) @@ -237,8 +248,8 @@ int test_main() // or if the proxy doesn't support UDP pr[0] = test_proxy(settings_pack::none, expect_udp_connection | expect_http_connection | expect_dht_msg | expect_peer_connection); pr[1] = test_proxy(settings_pack::socks4, expect_udp_connection | expect_dht_msg); - pr[2] = test_proxy(settings_pack::socks5, expect_possible_udp_connection | expect_dht_msg); - pr[3] = test_proxy(settings_pack::socks5_pw,expect_possible_udp_connection | expect_dht_msg); + pr[2] = test_proxy(settings_pack::socks5, expect_possible_udp_connection | expect_possible_dht_msg); + pr[3] = test_proxy(settings_pack::socks5_pw,expect_possible_udp_connection | expect_possible_dht_msg); pr[4] = test_proxy(settings_pack::http, expect_udp_connection | expect_dht_msg); pr[5] = test_proxy(settings_pack::http_pw, expect_udp_connection | expect_dht_msg); pr[6] = test_proxy(settings_pack::i2p_proxy, expect_udp_connection | expect_dht_msg); diff --git a/test/test_resume.cpp b/test/test_resume.cpp index 222a9663b..7fa5c72ba 100644 --- a/test/test_resume.cpp +++ b/test/test_resume.cpp @@ -43,8 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; -ptime g_start_time; - boost::shared_ptr generate_torrent() { file_storage fs; @@ -69,7 +67,6 @@ boost::shared_ptr generate_torrent() std::vector generate_resume_data(torrent_info* ti) { - g_start_time = libtorrent::time_now(); entry rd; rd["file-format"] = "libtorrent resume file"; @@ -159,12 +156,19 @@ torrent_status test_resume_flags(int flags) void default_tests(torrent_status const& s) { - int offset = total_seconds(libtorrent::time_now() - g_start_time); + // allow some slack in the time stamps since they are reported as + // relative times. If the computer is busy while running the unit test + // or running under valgrind it may take several seconds + TEST_CHECK(s.last_scrape >= 1349); + TEST_CHECK(s.time_since_download >= 1350); + TEST_CHECK(s.time_since_upload >= 1351); + TEST_CHECK(s.active_time >= 1339); + + TEST_CHECK(s.last_scrape < 1349 + 10); + TEST_CHECK(s.time_since_download < 1350 + 10); + TEST_CHECK(s.time_since_upload < 1351 + 10); + TEST_CHECK(s.active_time < 1339 + 10); - TEST_EQUAL(s.last_scrape, 1349 + offset); - TEST_EQUAL(s.time_since_download, 1350 + offset); - TEST_EQUAL(s.time_since_upload, 1351 + offset); - TEST_EQUAL(s.active_time, 1339 + offset); TEST_EQUAL(s.finished_time, 1352); TEST_EQUAL(s.seeding_time, 1340); TEST_EQUAL(s.added_time, 1347); diff --git a/test/test_utp.cpp b/test/test_utp.cpp index aa50b8bb0..26234e8d1 100644 --- a/test/test_utp.cpp +++ b/test/test_utp.cpp @@ -95,7 +95,13 @@ void test_transfer() boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 , true, false, true, "_utp", 0, &t, false, &atp); - for (int i = 0; i < 6; ++i) +#ifdef TORRENT_USE_VALGRIND + const int timeout = 12; +#else + const int timeout = 6; +#endif + + for (int i = 0; i < timeout; ++i) { print_alerts(ses1, "ses1", true, true, true); print_alerts(ses2, "ses2", true, true, true);