forked from premiere/premiere-libtorrent
fix torrent_status test (#1499)
fix torrent_status and tracker interval test
This commit is contained in:
parent
d2248b1021
commit
5d1fb97b14
|
@ -368,9 +368,9 @@ void setup_swarm(int num_nodes
|
|||
{
|
||||
shut_down |= std::all_of(nodes.begin() + 1, nodes.end()
|
||||
, [](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!");
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<int> announces;
|
||||
// the timestamps of all announces
|
||||
std::vector<lt::time_point> 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<std::string, std::string>&)
|
||||
{
|
||||
boost::uint32_t seconds = chrono::duration_cast<lt::seconds>(
|
||||
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<int> announce_alerts;
|
||||
std::vector<lt::time_point> 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<lt::tracker_announce_alert>(a))
|
||||
{
|
||||
boost::uint32_t seconds = chrono::duration_cast<lt::seconds>(
|
||||
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<lt::milliseconds>(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<lt::milliseconds>(announce_alerts[i] - last_alert).count();
|
||||
TEST_CHECK(abs(alert_interval_ms - interval * 1000) < 500);
|
||||
last_alert = announce_alerts[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue