refactor and clean up some sim tests

This commit is contained in:
arvidn 2016-03-26 21:02:49 -04:00
parent 710442ed91
commit 2b00eb1b38
12 changed files with 53 additions and 75 deletions

@ -1 +1 @@
Subproject commit 277389e87cfddd1747da7a8c7287e52ef592d345
Subproject commit 66356b0ce99cd126c7c99df50609b3050ba44e5d

View File

@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "setup_transfer.hpp" // for create_torrent
#include "utils.hpp"
namespace lt = libtorrent;
using namespace sim;
@ -104,11 +105,6 @@ std::string save_path(int swarm_id, int idx)
return path;
}
lt::address addr(char const* str)
{
return lt::address::from_string(str);
}
void add_extra_peers(lt::session& ses)
{
auto handles = ses.get_torrents();

View File

@ -95,9 +95,6 @@ lt::torrent_status get_status(lt::session& ses);
std::string save_path(int swarm_id, int idx);
// construct an address from string
lt::address addr(char const* str);
// disable TCP and enable uTP
void utp_only(lt::settings_pack& pack);

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "settings.hpp"
#include "create_torrent.hpp"
#include "simulator/simulator.hpp"
#include "simulator/utils.hpp"
#include <iostream>
using namespace sim;
@ -80,9 +81,8 @@ void run_test(Settings const& sett, Setup const& setup, Test const& test)
// set up a timer to fire later, to verify everything we expected to happen
// happened
lt::deadline_timer timer(*ios);
timer.expires_from_now(lt::seconds((num_torrents + 1) * 60));
timer.async_wait([&](boost::system::error_code const& ec)
sim::timer t(sim, lt::seconds((num_torrents + 1) * 60)
, [&](boost::system::error_code const& ec)
{
test(*ses);

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/address.hpp"
#include "libtorrent/torrent_status.hpp"
#include "simulator/simulator.hpp"
#include "simulator/utils.hpp"
#include "test.hpp"
#include "settings.hpp"
@ -63,9 +64,7 @@ void run_test(Setup const& setup, Test const& test)
print_alerts(*ses);
lt::deadline_timer timer(*ios);
timer.expires_from_now(lt::seconds(6));
timer.async_wait([&](lt::error_code const& ec)
sim::timer t(sim, lt::seconds(6), [&](boost::system::error_code const& ec)
{
test(*ses);

View File

@ -40,6 +40,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/alert_types.hpp"
#include "simulator/simulator.hpp"
#include "simulator/utils.hpp"
#include "utils.hpp" // for print_alerts
using namespace sim;
@ -85,8 +87,6 @@ void run_test(Setup const& setup
, HandleAlerts const& on_alert
, Test const& test)
{
const lt::time_point start_time = lt::clock_type::now();
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
@ -114,22 +114,11 @@ void run_test(Setup const& setup
// the alert notification function is called from within libtorrent's
// context. It's not OK to talk to libtorrent in there, post it back out and
// then ask for alerts.
ses->set_alert_notify([&] { ios.post([&] {
std::vector<lt::alert*> alerts;
ses->pop_alerts(&alerts);
// call the user handler
for (auto const a : alerts)
{
printf("%-3d %s\n", int(lt::duration_cast<lt::seconds>(a->timestamp()
- start_time).count()), a->message().c_str());
print_alerts(*ses, [=](lt::session& ses, lt::alert const* a) {
on_alert(ses, a);
});
on_alert(*ses, a);
}
} ); } );
lt::deadline_timer timer(ios);
timer.expires_from_now(lt::seconds(60));
timer.async_wait([&](lt::error_code const& ec)
sim::timer t(sim, lt::seconds(60), [&](boost::system::error_code const& ec)
{
test(*ses, test_peers);

View File

@ -35,7 +35,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "create_torrent.hpp"
#include "bittorrent_peer.hpp"
#include "settings.hpp"
#include "print_alerts.hpp"
#include "utils.hpp"
#include "simulator/utils.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/alert_types.hpp"
@ -61,7 +62,7 @@ struct choke_state
TORRENT_TEST(optimistic_unchoke)
{
int const num_nodes = 20;
lt::time_duration const test_duration = libtorrent::seconds(1201);
lt::time_duration const test_duration = libtorrent::seconds(num_nodes * 30 + 4);
dsl_config network_cfg;
sim::simulation sim{network_cfg};
@ -82,31 +83,25 @@ TORRENT_TEST(optimistic_unchoke)
session_proxy proxy;
boost::shared_ptr<lt::session> ses = boost::make_shared<lt::session>(
boost::ref(pack), boost::ref(ios));
auto ses = std::make_shared<lt::session>(
std::ref(pack), std::ref(ios));
ses->async_add_torrent(atp);
std::vector<boost::shared_ptr<sim::asio::io_service> > io_service;
std::vector<boost::shared_ptr<peer_conn> > peers;
std::vector<std::shared_ptr<sim::asio::io_service> > io_service;
std::vector<std::shared_ptr<peer_conn> > peers;
ses->set_alert_notify([&]() {
// this function is called inside libtorrent and we cannot perform work
// immediately in it. We have to notify the outside to pull all the alerts
ios.post(boost::bind(&print_alerts, ses.get(), start_time));
});
print_alerts(*ses);
lt::deadline_timer timer(ios);
timer.expires_from_now(libtorrent::seconds(2));
timer.async_wait([&](error_code const& ec)
sim::timer t(sim, lt::seconds(2), [&](boost::system::error_code const& ec)
{
for (int i = 0; i < num_nodes; ++i)
{
// create a new io_service
char ep[30];
snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
io_service.push_back(boost::make_shared<sim::asio::io_service>(
boost::ref(sim), addr(ep)));
peers.push_back(boost::make_shared<peer_conn>(boost::ref(*io_service.back())
io_service.push_back(std::make_shared<sim::asio::io_service>(
std::ref(sim), addr(ep)));
peers.push_back(std::make_shared<peer_conn>(std::ref(*io_service.back())
, [&,i](int msg, char const* bug, int len)
{
choke_state& cs = peer_choke_state[i];
@ -136,7 +131,7 @@ TORRENT_TEST(optimistic_unchoke)
char const* msg_str[] = {"choke", "unchoke"};
lt::time_duration d = lt::clock_type::now() - start_time;
boost::uint32_t millis = lt::duration_cast<lt::milliseconds>(d).count();
std::uint32_t millis = lt::duration_cast<lt::milliseconds>(d).count();
printf("\x1b[35m%4d.%03d: [%d] %s (%d ms)\x1b[0m\n"
, millis / 1000, millis % 1000, i, msg_str[msg]
, int(lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count()));
@ -147,9 +142,7 @@ TORRENT_TEST(optimistic_unchoke)
}
});
lt::deadline_timer end_timer(ios);
timer.expires_from_now(test_duration);
timer.async_wait([&](error_code const& ec)
sim::timer t2(sim, test_duration, [&](boost::system::error_code const& ec)
{
for (auto& p : peers)
{
@ -161,12 +154,12 @@ TORRENT_TEST(optimistic_unchoke)
sim.run();
boost::int64_t const duration_ms = lt::duration_cast<lt::milliseconds>(test_duration).count();
boost::int64_t const average_unchoke_time = duration_ms / num_nodes;
std::int64_t const duration_ms = lt::duration_cast<lt::milliseconds>(test_duration).count();
std::int64_t const average_unchoke_time = duration_ms / num_nodes;
printf("EXPECT: %" PRId64 " ms\n", average_unchoke_time);
for (auto const& cs : peer_choke_state)
{
boost::int64_t unchoke_duration = lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count();
std::int64_t unchoke_duration = lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count();
printf("%" PRId64 " ms\n", unchoke_duration);
TEST_CHECK(std::abs(unchoke_duration - average_unchoke_time) < 1000);
}

View File

@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "setup_swarm.hpp"
#include "test.hpp"
#include "utils.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/session.hpp"

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "setup_swarm.hpp"
#include "simulator/simulator.hpp"
#include "simulator/http_server.hpp"
#include "simulator/utils.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/session.hpp"
@ -323,7 +324,6 @@ TORRENT_TEST(ipv6_support)
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
@ -334,9 +334,8 @@ TORRENT_TEST(ipv6_support)
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
asio::high_resolution_timer stop(ios);
stop.expires_from_now(chrono::seconds(5));
stop.async_wait([&ses](boost::system::error_code const& ec)
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const& ec)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
@ -346,9 +345,8 @@ TORRENT_TEST(ipv6_support)
});
// then shut down 10 seconds in
asio::high_resolution_timer terminate(ios);
terminate.expires_from_now(chrono::seconds(10));
terminate.async_wait([&ses,&zombie](boost::system::error_code const& ec)
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const& ec)
{
zombie = ses->abort();
ses->set_alert_notify([]{});
@ -403,9 +401,8 @@ void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
ses->async_add_torrent(p);
// run the test 5 seconds in
asio::high_resolution_timer t1(ios);
t1.expires_from_now(chrono::seconds(5));
t1.async_wait([&ses,&test1](boost::system::error_code const& ec)
sim::timer t1(sim, lt::seconds(5)
, [&ses,&test1](boost::system::error_code const& ec)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
@ -413,9 +410,8 @@ void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
test1(h);
});
asio::high_resolution_timer t2(ios);
t2.expires_from_now(chrono::seconds(5 + delay));
t2.async_wait([&ses,&test2](boost::system::error_code const& ec)
sim::timer t2(sim, lt::seconds(5 + delay)
, [&ses,&test2](boost::system::error_code const& ec)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
@ -424,9 +420,8 @@ void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
});
// then shut down 10 seconds in
asio::high_resolution_timer t3(ios);
t3.expires_from_now(chrono::seconds(10 + delay));
t3.async_wait([&ses,&zombie](boost::system::error_code const& ec)
sim::timer t3(sim, lt::seconds(10 + delay)
, [&ses,&zombie](boost::system::error_code const& ec)
{
zombie = ses->abort();
ses->set_alert_notify([]{});

View File

@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/settings_pack.hpp"
#include "simulator/simulator.hpp"
#include "simulator/socks_server.hpp"
#include "simulator/utils.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
@ -128,9 +129,7 @@ void run_test(
params.save_path = save_path(1);
ses[1]->async_add_torrent(params);
lt::deadline_timer timer(ios0);
timer.expires_from_now(lt::seconds(60));
timer.async_wait([&](lt::error_code const& ec)
sim::timer t(sim, lt::seconds(60), [&](boost::system::error_code const& ec)
{
test(ses);

View File

@ -116,6 +116,11 @@ void set_proxy(lt::session& ses, int proxy_type, int flags, bool proxy_peer_conn
ses.apply_settings(p);
}
lt::address addr(char const* str)
{
return lt::address::from_string(str);
}
void print_alerts(lt::session& ses
, std::function<void(lt::session&, lt::alert const*)> on_alert)
{

View File

@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include <functional>
#include "libtorrent/address.hpp"
namespace libtorrent
{
@ -40,6 +41,9 @@ namespace libtorrent
namespace lt = libtorrent;
// construct an address from string
lt::address addr(char const* str);
void utp_only(lt::session& ses);
void enable_enc(lt::session& ses);
void filter_ips(lt::session& ses);