improve simulations by reseeding the random number generator. also erase all output in between iterations in test_error_handling

This commit is contained in:
arvidn 2017-11-08 23:48:51 +01:00 committed by Arvid Norberg
parent fc74c032f0
commit 381db57b42
4 changed files with 57 additions and 3 deletions

View File

@ -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

View File

@ -124,11 +124,13 @@ void print_alerts(lt::session& ses
{
lt::time_point start_time = lt::clock_type::now();
static std::vector<lt::alert*> alerts;
ses.set_alert_notify([&ses,start_time,on_alert] {
ses.get_io_service().post([&ses,start_time,on_alert] {
try {
std::vector<lt::alert*> alerts;
alerts.clear();
ses.pop_alerts(&alerts);
for (lt::alert const* a : alerts)

View File

@ -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

View File

@ -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)();