fix dht tests (#803)

fix tests
This commit is contained in:
Arvid Norberg 2016-06-11 15:37:28 -04:00 committed by GitHub
parent dfc2206b9f
commit ef1a24518d
6 changed files with 91 additions and 22 deletions

View File

@ -99,6 +99,7 @@ struct dht_node final : lt::dht::udp_socket_interface
, m_add_dead_nodes(flags & dht_network::add_dead_nodes)
, m_ipv6(flags & dht_network::bind_ipv6)
{
m_dht_storage->update_node_ids({id_from_addr(m_io_service.get_ips().front())});
error_code ec;
sock().open(m_ipv6 ? asio::ip::udp::v6() : asio::ip::udp::v4());
sock().bind(asio::ip::udp::endpoint(

View File

@ -152,6 +152,30 @@ int completed_pieces(lt::session& ses)
return h.status().num_pieces;
}
namespace {
bool should_print(lt::alert* a)
{
using namespace libtorrent;
#ifndef TORRENT_DISABLE_LOGGING
if (auto pla = alert_cast<peer_log_alert>(a))
{
if (pla->direction != peer_log_alert::incoming_message
&& pla->direction != peer_log_alert::outgoing_message)
return false;
}
#endif
if (alert_cast<session_stats_alert>(a)
|| alert_cast<piece_finished_alert>(a)
|| alert_cast<block_finished_alert>(a)
|| alert_cast<block_downloading_alert>(a))
{
return false;
}
return true;
}
}
void utp_only(lt::settings_pack& p)
{
using namespace libtorrent;
@ -327,10 +351,14 @@ void setup_swarm(int num_nodes
// only print alerts from the session under test
lt::time_duration d = a->timestamp() - start_time;
boost::uint32_t millis = lt::duration_cast<lt::milliseconds>(d).count();
std::printf("%4d.%03d: %-25s %s\n", millis / 1000, millis % 1000
, a->what()
, a->message().c_str());
boost::uint32_t const millis = lt::duration_cast<lt::milliseconds>(d).count();
if (should_print(a))
{
std::printf("%4d.%03d: %-25s %s\n", millis / 1000, millis % 1000
, a->what()
, a->message().c_str());
}
// if a torrent was added save the torrent handle
if (lt::add_torrent_alert* at = lt::alert_cast<lt::add_torrent_alert>(a))

View File

@ -3264,12 +3264,14 @@ namespace libtorrent
abort_jobs();
TORRENT_ASSERT(m_magic == 0x1337);
// release the io_service to allow the run() call to return
// we do this once we stop posting new callbacks to it.
COMPLETE_ASYNC("disk_io_thread::work");
w.reset();
TORRENT_ASSERT(m_magic == 0x1337);
// at this point, the disk_io_thread object may have been destructed.
// the call to w.reset() above is what synchronizes with the main thread
}
void disk_io_thread::abort_jobs()

View File

@ -507,7 +507,12 @@ namespace aux {
TORRENT_ASSERT(is_single_thread());
#ifndef TORRENT_DISABLE_LOGGING
if (m_alerts.should_post<log_alert>())
// this alert is a bit special. Since it's so verbose it's not only
// filtered by its own alert type (log_alert) but also whether session
// stats alerts are actually enabled. Without session_stats alerts the
// headers aren't very useful anyway
if (m_alerts.should_post<log_alert>()
&& m_alerts.should_post<session_stats_alert>())
{
session_log(" *** session thread init");

View File

@ -109,7 +109,7 @@ sha1_hash rand_hash()
address rand_v6()
{
address_v6::bytes_type bytes;
for (int i = 0; i < int(bytes.size()); ++i) bytes[i] = rand();
for (int i = 0; i < int(bytes.size()); ++i) bytes[i] = lt::random();
return address_v6(bytes);
}
#endif
@ -149,7 +149,27 @@ std::map<std::string, boost::int64_t> get_counters(libtorrent::session& s)
ret[metrics[i].name] = sa->values[metrics[i].value_index];
return ret;
}
namespace {
bool should_print(lt::alert* a)
{
#ifndef TORRENT_DISABLE_LOGGING
if (auto pla = alert_cast<peer_log_alert>(a))
{
if (pla->direction != peer_log_alert::incoming_message
&& pla->direction != peer_log_alert::outgoing_message)
return false;
}
#endif
if (alert_cast<session_stats_alert>(a)
|| alert_cast<piece_finished_alert>(a)
|| alert_cast<block_finished_alert>(a)
|| alert_cast<block_downloading_alert>(a))
{
return false;
}
return true;
}
}
alert const* wait_for_alert(lt::session& ses, int type, char const* name)
{
time_point end = libtorrent::clock_type::now() + seconds(10);
@ -166,8 +186,11 @@ alert const* wait_for_alert(lt::session& ses, int type, char const* name)
for (std::vector<alert*>::iterator i = alerts.begin()
, end(alerts.end()); i != end; ++i)
{
std::fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name
, (*i)->what(), (*i)->message().c_str());
if (should_print(*i))
{
std::fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name
, (*i)->what(), (*i)->message().c_str());
}
if ((*i)->type() == type && !ret)
{
ret = *i;
@ -278,9 +301,7 @@ bool print_alerts(lt::session& ses, char const* name
{
std::fprintf(stderr, "%s: %s: [%s] (%s): %s\n", time_now_string(), name, (*i)->what(), print_endpoint(p->ip).c_str(), p->message().c_str());
}
else if ((*i)->message() != "block downloading"
&& (*i)->message() != "block finished"
&& (*i)->message() != "piece finished"
else if (should_print(*i)
&& !no_output)
{
std::fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name, (*i)->what(), (*i)->message().c_str());
@ -591,7 +612,7 @@ boost::shared_ptr<T> clone_ptr(boost::shared_ptr<T> const& ptr)
}
unsigned char random_byte()
{ return std::rand() & 0xff; }
{ return lt::random() & 0xff; }
void create_random_files(std::string const& path, const int file_sizes[], int num_files)
{

View File

@ -509,6 +509,7 @@ void do_test_dht(address(&rand_addr)())
obs observer;
counters cnt;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id(0)});
udp::endpoint source(rand_addr(), 20);
std::map<std::string, node*> nodes;
dht::node node(source.protocol(), &s, sett
@ -1427,7 +1428,8 @@ void do_test_dht(address(&rand_addr)())
do
{
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
std::vector<udp::endpoint> nodesv;
@ -1499,8 +1501,9 @@ void do_test_dht(address(&rand_addr)())
do
{
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id::min()});
dht::node_id target = to_hash("1234876923549721020394873245098347598635");
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1596,7 +1599,8 @@ void do_test_dht(address(&rand_addr)())
do
{
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1643,7 +1647,8 @@ void do_test_dht(address(&rand_addr)())
do
{
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1731,7 +1736,8 @@ void do_test_dht(address(&rand_addr)())
int old_branching = sett.search_branching;
sett.search_branching = 8;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
enum { num_test_nodes = 8 };
node_entry nodes[num_test_nodes] =
{ node_entry(items[0].target, udp::endpoint(address_v4::from_string("1.1.1.1"), 1231))
@ -1832,7 +1838,8 @@ void do_test_dht(address(&rand_addr)())
int old_branching = sett.search_branching;
sett.search_branching = 8;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
enum { num_test_nodes = 8 };
node_entry nodes[num_test_nodes] =
{ node_entry(items[0].target, udp::endpoint(address_v4::from_string("1.1.1.1"), 1231))
@ -1935,7 +1942,8 @@ void do_test_dht(address(&rand_addr)())
int old_branching = sett.search_branching;
sett.search_branching = 8;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes, *dht_storage);
dht_storage->update_node_ids({node_id::min()});
dht::node node(udp::v4(), &s, sett, node_id::min(), &observer, cnt, nodes, *dht_storage);
sha1_hash target = hasher(public_key, item_pk_len).final();
enum { num_test_nodes = 9 }; // we need K + 1 nodes to create the failing sequence
node_entry nodes[num_test_nodes] =
@ -2032,6 +2040,7 @@ TORRENT_TEST(dht_dual_stack)
counters cnt;
std::map<std::string, node*> nodes;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id(0)});
dht::node node4(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes, *dht_storage);
dht::node node6(udp::v6(), &s, sett, node_id(0), &observer, cnt, nodes, *dht_storage);
nodes.insert(std::make_pair("n4", &node4));
@ -2493,6 +2502,7 @@ TORRENT_TEST(read_only_node)
std::map<std::string, node*> nodes;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id(0)});
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes, *dht_storage);
udp::endpoint source(address::from_string("10.0.0.1"), 20);
bdecode_node response;
@ -2583,6 +2593,7 @@ TORRENT_TEST(invalid_error_msg)
std::map<std::string, node*> nodes;
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id(0)});
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes, *dht_storage);
udp::endpoint source(address::from_string("10.0.0.1"), 20);
@ -2624,6 +2635,7 @@ TORRENT_TEST(rpc_invalid_error_msg)
dht::routing_table table(node_id(), udp::v4(), 8, sett, &observer);
dht::rpc_manager rpc(node_id(), sett, table, &s, &observer);
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
dht_storage->update_node_ids({node_id(0)});
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes, *dht_storage);
udp::endpoint source(address::from_string("10.0.0.1"), 20);