From 381db57b42acfd893788304ae9d5fb8d771355c9 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 8 Nov 2017 23:48:51 +0100 Subject: [PATCH] improve simulations by reseeding the random number generator. also erase all output in between iterations in test_error_handling --- simulation/test_error_handling.cpp | 30 +++++++++++++++++++++++++++++- simulation/utils.cpp | 4 +++- test/main.cpp | 25 ++++++++++++++++++++++++- test/test.hpp | 1 + 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/simulation/test_error_handling.cpp b/simulation/test_error_handling.cpp index 0b3ce3127..a5661b87c 100644 --- a/simulation/test_error_handling.cpp +++ b/simulation/test_error_handling.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/aux_/proxy_settings.hpp" +#include "libtorrent/random.hpp" #include "libtorrent/settings_pack.hpp" #include "simulator/simulator.hpp" #include "simulator/socks_server.hpp" @@ -146,6 +147,9 @@ void* operator new(std::size_t sz) { if (--g_alloc_counter == 0) { + char stack[10000]; + print_backtrace(stack, sizeof(stack), 40, nullptr); + std::printf("\n\nthrowing bad_alloc (as part of test)\n%s\n\n\n", stack); throw std::bad_alloc(); } return std::malloc(sz); @@ -156,10 +160,19 @@ void operator delete(void* ptr) noexcept std::free(ptr); } -TORRENT_TEST(no_proxy_tcp) +TORRENT_TEST(error_handling) { for (int i = 0; i < 3000; ++i) { + // this will clear the history of all output we've printed so far. + // if we encounter an error from now on, we'll only print the relevant + // iteration + reset_output(); + + // re-seed the random engine each iteration, to make the runs + // deterministic + lt::aux::random_engine().seed(0x82daf973); + std::printf("\n\n === ROUND %d ===\n\n", i); try { @@ -174,10 +187,25 @@ TORRENT_TEST(no_proxy_tcp) { // this is kind of expected } + catch (boost::system::system_error const& err) + { + TEST_ERROR("session constructor terminated with unexpected exception. \"" + + err.code().message() + "\" round: " + + std::to_string(i)); + break; + } + catch (std::exception const& err) + { + TEST_ERROR("session constructor terminated with unexpected exception. \"" + + std::string(err.what()) + "\" round: " + + std::to_string(i)); + break; + } catch (...) { TEST_ERROR("session constructor terminated with unexpected exception. round: " + std::to_string(i)); + break; } // if we didn't fail any allocations this run, there's no need to // continue, we won't exercise any new code paths diff --git a/simulation/utils.cpp b/simulation/utils.cpp index 36e4de3f0..490809f08 100644 --- a/simulation/utils.cpp +++ b/simulation/utils.cpp @@ -124,11 +124,13 @@ void print_alerts(lt::session& ses { lt::time_point start_time = lt::clock_type::now(); + static std::vector alerts; + ses.set_alert_notify([&ses,start_time,on_alert] { ses.get_io_service().post([&ses,start_time,on_alert] { try { - std::vector alerts; + alerts.clear(); ses.pop_alerts(&alerts); for (lt::alert const* a : alerts) diff --git a/test/main.cpp b/test/main.cpp index 53f2df390..df14c9be0 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -267,7 +267,25 @@ struct unit_directory_guard } }; -EXPORT int main(int argc, char const* argv[]) +void EXPORT reset_output() +{ + if (current_test == nullptr || current_test->output == 0) return; + fflush(stdout); + fflush(stderr); + rewind(current_test->output); +#ifdef TORRENT_WINDOWS + int const r = _chsize(fileno(current_test->output), 0); +#else + int const r = ftruncate(fileno(current_test->output), 0); +#endif + if (r != 0) + { + // this is best effort, it's not the end of the world if we fail + std::cerr << "ftruncate of temporary test output file failed: " << strerror(errno) << "\n"; + } +} + +int EXPORT main(int argc, char const* argv[]) { char const* executable = argv[0]; // skip executable name @@ -443,6 +461,11 @@ EXPORT int main(int argc, char const* argv[]) try { #endif + +#if defined TORRENT_BUILD_SIMULATOR + lt::aux::random_engine().seed(0x82daf973); +#endif + _g_test_failures = 0; (*t.fun)(); #ifndef BOOST_NO_EXCEPTIONS diff --git a/test/test.hpp b/test/test.hpp index c26e6d7de..7a83dd523 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE. void EXPORT report_failure(char const* err, char const* file, int line); int EXPORT print_failures(); int EXPORT test_counter(); +void EXPORT reset_output(); typedef void (*unit_test_fun_t)();