fix torrent_status test (#1499)

fix torrent_status and tracker interval test
This commit is contained in:
Arvid Norberg 2017-01-08 09:51:01 -05:00 committed by GitHub
parent d2248b1021
commit 5d1fb97b14
4 changed files with 42 additions and 26 deletions

View File

@ -368,9 +368,9 @@ void setup_swarm(int num_nodes
{ {
shut_down |= std::all_of(nodes.begin() + 1, nodes.end() shut_down |= std::all_of(nodes.begin() + 1, nodes.end()
, [](boost::shared_ptr<lt::session> const& s) , [](boost::shared_ptr<lt::session> 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!"); TEST_ERROR("seeding failed!");
} }

View File

@ -45,6 +45,7 @@ TORRENT_TEST(status_timers)
{ {
lt::time_point start_time; lt::time_point start_time;
lt::torrent_handle handle; lt::torrent_handle handle;
bool ran_to_completion = false;
setup_swarm(1, swarm_test::upload setup_swarm(1, swarm_test::upload
// add session // add session
@ -69,13 +70,16 @@ TORRENT_TEST(status_timers)
// represent about 18 hours. The clock steps forward in 4 hour increments // represent about 18 hours. The clock steps forward in 4 hour increments
// to stay within that range // to stay within that range
if (ticks > 20 * 60 * 60) if (ticks > 20 * 60 * 60)
{
ran_to_completion = true;
return true; return true;
}
// once an hour, verify that the timers seem correct // once an hour, verify that the timers seem correct
if ((ticks % 3600) == 0) if ((ticks % 3600) == 0)
{ {
lt::time_point now = lt::clock_type::now(); lt::time_point const now = lt::clock_type::now();
int since_start = total_seconds(now - start_time) - 1; int const since_start = total_seconds(now - start_time) - 1;
torrent_status st = handle.status(); torrent_status st = handle.status();
TEST_EQUAL(st.active_time, since_start); TEST_EQUAL(st.active_time, since_start);
TEST_EQUAL(st.seeding_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); TEST_EQUAL(st.time_since_download, since_start);
} }
} }
return false; return false;
}); });
TEST_CHECK(ran_to_completion);
} }
// This test makes sure that adding a torrent causes no torrent related alert to // This test makes sure that adding a torrent causes no torrent related alert to

View File

@ -59,28 +59,30 @@ void test_interval(int interval)
sim::simulation sim{network_cfg}; sim::simulation sim{network_cfg};
lt::time_point start = lt::clock_type::now(); 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")); sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2"));
// listen on port 8080 // listen on port 8080
sim::http_server http(web_server, 8080); sim::http_server http(web_server, 8080);
// the timestamps (in seconds) of all announces // the timestamps of all announces
std::vector<int> announces; std::vector<lt::time_point> announces;
http.register_handler("/announce" 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<std::string, std::string>&) , std::map<std::string, std::string>&)
{ {
boost::uint32_t seconds = chrono::duration_cast<lt::seconds>( // don't collect events once we're done. We're not interested in the
lt::clock_type::now() - start).count(); // tracker stopped announce for instance
announces.push_back(seconds); if (!ran_to_completion)
announces.push_back(lt::clock_type::now());
char response[500]; char response[500];
int size = snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval); int size = snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
return sim::send_response(200, "OK", size) + response; return sim::send_response(200, "OK", size) + response;
}); });
std::vector<int> announce_alerts; std::vector<lt::time_point> announce_alerts;
lt::settings_pack default_settings = settings(); lt::settings_pack default_settings = settings();
lt::add_torrent_params default_add_torrent; lt::add_torrent_params default_add_torrent;
@ -95,26 +97,36 @@ void test_interval(int interval)
// on alert // on alert
, [&](lt::alert const* a, lt::session& ses) { , [&](lt::alert const* a, lt::session& ses) {
if (ran_to_completion) return;
if (lt::alert_cast<lt::tracker_announce_alert>(a)) if (lt::alert_cast<lt::tracker_announce_alert>(a))
{ announce_alerts.push_back(a->timestamp());
boost::uint32_t seconds = chrono::duration_cast<lt::seconds>(
a->timestamp() - start).count();
announce_alerts.push_back(seconds);
}
} }
// terminate // 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()); TEST_EQUAL(announce_alerts.size(), announces.size());
int counter = 0; lt::time_point last_announce = announces[0];
for (int i = 0; i < int(announces.size()); ++i) lt::time_point last_alert = announce_alerts[0];
for (int i = 1; i < int(announces.size()); ++i)
{ {
TEST_EQUAL(announces[i], counter); // make sure the interval is within 500 ms of what it's supposed to be
TEST_EQUAL(announce_alerts[i], counter); // (this accounts for network latencies)
counter += interval; int const actual_interval_ms = duration_cast<lt::milliseconds>(announces[i] - last_announce).count();
if (counter > duration + 1) counter = duration + 1; TEST_CHECK(abs(actual_interval_ms - interval * 1000) < 500);
last_announce = announces[i];
int const alert_interval_ms = duration_cast<lt::milliseconds>(announce_alerts[i] - last_alert).count();
TEST_CHECK(abs(alert_interval_ms - interval * 1000) < 500);
last_alert = announce_alerts[i];
} }
} }

View File

@ -50,7 +50,7 @@ void report_failure(char const* err, char const* file, int line)
{ {
char buf[500]; char buf[500];
snprintf(buf, sizeof(buf), "\x1b[41m***** %s:%d \"%s\" *****\x1b[0m\n", file, line, err); 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); failure_strings.push_back(buf);
++_g_test_failures; ++_g_test_failures;
} }