From 5d1fb97b1465392fe900595f95c21ba33818f3e4 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 8 Jan 2017 09:51:01 -0500 Subject: [PATCH] fix torrent_status test (#1499) fix torrent_status and tracker interval test --- simulation/setup_swarm.cpp | 4 +-- simulation/test_torrent_status.cpp | 10 ++++-- simulation/test_tracker.cpp | 52 ++++++++++++++++++------------ test/test.cpp | 2 +- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/simulation/setup_swarm.cpp b/simulation/setup_swarm.cpp index a4dc06bfe..c88713495 100644 --- a/simulation/setup_swarm.cpp +++ b/simulation/setup_swarm.cpp @@ -368,9 +368,9 @@ void setup_swarm(int num_nodes { shut_down |= std::all_of(nodes.begin() + 1, nodes.end() , [](boost::shared_ptr const& s) - { return is_seed(*s); }); + { return is_seed(*s); }) && num_nodes > 1; - if (tick > 70 * (num_nodes - 1) && !shut_down) + if (tick > 70 * (num_nodes - 1) && !shut_down && num_nodes > 1) { TEST_ERROR("seeding failed!"); } diff --git a/simulation/test_torrent_status.cpp b/simulation/test_torrent_status.cpp index 7b6959015..c5e979d51 100644 --- a/simulation/test_torrent_status.cpp +++ b/simulation/test_torrent_status.cpp @@ -45,6 +45,7 @@ TORRENT_TEST(status_timers) { lt::time_point start_time; lt::torrent_handle handle; + bool ran_to_completion = false; setup_swarm(1, swarm_test::upload // add session @@ -69,13 +70,16 @@ TORRENT_TEST(status_timers) // represent about 18 hours. The clock steps forward in 4 hour increments // to stay within that range if (ticks > 20 * 60 * 60) + { + ran_to_completion = true; return true; + } // once an hour, verify that the timers seem correct if ((ticks % 3600) == 0) { - lt::time_point now = lt::clock_type::now(); - int since_start = total_seconds(now - start_time) - 1; + lt::time_point const now = lt::clock_type::now(); + int const since_start = total_seconds(now - start_time) - 1; torrent_status st = handle.status(); TEST_EQUAL(st.active_time, since_start); TEST_EQUAL(st.seeding_time, since_start); @@ -94,9 +98,9 @@ TORRENT_TEST(status_timers) TEST_EQUAL(st.time_since_download, since_start); } } - return false; }); + TEST_CHECK(ran_to_completion); } // This test makes sure that adding a torrent causes no torrent related alert to diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp index 064937a59..e3466c4be 100644 --- a/simulation/test_tracker.cpp +++ b/simulation/test_tracker.cpp @@ -59,28 +59,30 @@ void test_interval(int interval) sim::simulation sim{network_cfg}; lt::time_point start = lt::clock_type::now(); + bool ran_to_completion = false; sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2")); // listen on port 8080 sim::http_server http(web_server, 8080); - // the timestamps (in seconds) of all announces - std::vector announces; + // the timestamps of all announces + std::vector announces; http.register_handler("/announce" - , [&announces,interval,start](std::string method, std::string req + , [&announces,interval,start,&ran_to_completion](std::string method, std::string req , std::map&) { - boost::uint32_t seconds = chrono::duration_cast( - lt::clock_type::now() - start).count(); - announces.push_back(seconds); + // don't collect events once we're done. We're not interested in the + // tracker stopped announce for instance + if (!ran_to_completion) + announces.push_back(lt::clock_type::now()); char response[500]; int size = snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval); return sim::send_response(200, "OK", size) + response; }); - std::vector announce_alerts; + std::vector announce_alerts; lt::settings_pack default_settings = settings(); lt::add_torrent_params default_add_torrent; @@ -95,26 +97,36 @@ void test_interval(int interval) // on alert , [&](lt::alert const* a, lt::session& ses) { + if (ran_to_completion) return; if (lt::alert_cast(a)) - { - boost::uint32_t seconds = chrono::duration_cast( - a->timestamp() - start).count(); - - announce_alerts.push_back(seconds); - } + announce_alerts.push_back(a->timestamp()); } // terminate - , [](int ticks, lt::session& ses) -> bool { return ticks > duration; }); + , [&](int ticks, lt::session& ses) -> bool { + if (ticks > duration + 1) + { + ran_to_completion = true; + return true; + } + return false; + }); + TEST_CHECK(ran_to_completion); TEST_EQUAL(announce_alerts.size(), announces.size()); - int counter = 0; - for (int i = 0; i < int(announces.size()); ++i) + lt::time_point last_announce = announces[0]; + lt::time_point last_alert = announce_alerts[0]; + for (int i = 1; i < int(announces.size()); ++i) { - TEST_EQUAL(announces[i], counter); - TEST_EQUAL(announce_alerts[i], counter); - counter += interval; - if (counter > duration + 1) counter = duration + 1; + // make sure the interval is within 500 ms of what it's supposed to be + // (this accounts for network latencies) + int const actual_interval_ms = duration_cast(announces[i] - last_announce).count(); + TEST_CHECK(abs(actual_interval_ms - interval * 1000) < 500); + last_announce = announces[i]; + + int const alert_interval_ms = duration_cast(announce_alerts[i] - last_alert).count(); + TEST_CHECK(abs(alert_interval_ms - interval * 1000) < 500); + last_alert = announce_alerts[i]; } } diff --git a/test/test.cpp b/test/test.cpp index 93cacd099..9e5e290b7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -50,7 +50,7 @@ void report_failure(char const* err, char const* file, int line) { char buf[500]; snprintf(buf, sizeof(buf), "\x1b[41m***** %s:%d \"%s\" *****\x1b[0m\n", file, line, err); - fprintf(stderr, "\n%s\n", buf); + printf("\n%s\n", buf); failure_strings.push_back(buf); ++_g_test_failures; }