Skip to content

Commit 381db57

Browse files
committed
improve simulations by reseeding the random number generator. also erase all output in between iterations in test_error_handling
1 parent fc74c03 commit 381db57

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

simulation/test_error_handling.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
4040
#include "libtorrent/ip_filter.hpp"
4141
#include "libtorrent/alert_types.hpp"
4242
#include "libtorrent/aux_/proxy_settings.hpp"
43+
#include "libtorrent/random.hpp"
4344
#include "libtorrent/settings_pack.hpp"
4445
#include "simulator/simulator.hpp"
4546
#include "simulator/socks_server.hpp"
@@ -146,6 +147,9 @@ void* operator new(std::size_t sz)
146147
{
147148
if (--g_alloc_counter == 0)
148149
{
150+
char stack[10000];
151+
print_backtrace(stack, sizeof(stack), 40, nullptr);
152+
std::printf("\n\nthrowing bad_alloc (as part of test)\n%s\n\n\n", stack);
149153
throw std::bad_alloc();
150154
}
151155
return std::malloc(sz);
@@ -156,10 +160,19 @@ void operator delete(void* ptr) noexcept
156160
std::free(ptr);
157161
}
158162

159-
TORRENT_TEST(no_proxy_tcp)
163+
TORRENT_TEST(error_handling)
160164
{
161165
for (int i = 0; i < 3000; ++i)
162166
{
167+
// this will clear the history of all output we've printed so far.
168+
// if we encounter an error from now on, we'll only print the relevant
169+
// iteration
170+
reset_output();
171+
172+
// re-seed the random engine each iteration, to make the runs
173+
// deterministic
174+
lt::aux::random_engine().seed(0x82daf973);
175+
163176
std::printf("\n\n === ROUND %d ===\n\n", i);
164177
try
165178
{
@@ -174,10 +187,25 @@ TORRENT_TEST(no_proxy_tcp)
174187
{
175188
// this is kind of expected
176189
}
190+
catch (boost::system::system_error const& err)
191+
{
192+
TEST_ERROR("session constructor terminated with unexpected exception. \""
193+
+ err.code().message() + "\" round: "
194+
+ std::to_string(i));
195+
break;
196+
}
197+
catch (std::exception const& err)
198+
{
199+
TEST_ERROR("session constructor terminated with unexpected exception. \""
200+
+ std::string(err.what()) + "\" round: "
201+
+ std::to_string(i));
202+
break;
203+
}
177204
catch (...)
178205
{
179206
TEST_ERROR("session constructor terminated with unexpected exception. round: "
180207
+ std::to_string(i));
208+
break;
181209
}
182210
// if we didn't fail any allocations this run, there's no need to
183211
// continue, we won't exercise any new code paths

simulation/utils.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ void print_alerts(lt::session& ses
124124
{
125125
lt::time_point start_time = lt::clock_type::now();
126126

127+
static std::vector<lt::alert*> alerts;
128+
127129
ses.set_alert_notify([&ses,start_time,on_alert] {
128130
ses.get_io_service().post([&ses,start_time,on_alert] {
129131

130132
try {
131-
std::vector<lt::alert*> alerts;
133+
alerts.clear();
132134
ses.pop_alerts(&alerts);
133135

134136
for (lt::alert const* a : alerts)

test/main.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,25 @@ struct unit_directory_guard
267267
}
268268
};
269269

270-
EXPORT int main(int argc, char const* argv[])
270+
void EXPORT reset_output()
271+
{
272+
if (current_test == nullptr || current_test->output == 0) return;
273+
fflush(stdout);
274+
fflush(stderr);
275+
rewind(current_test->output);
276+
#ifdef TORRENT_WINDOWS
277+
int const r = _chsize(fileno(current_test->output), 0);
278+
#else
279+
int const r = ftruncate(fileno(current_test->output), 0);
280+
#endif
281+
if (r != 0)
282+
{
283+
// this is best effort, it's not the end of the world if we fail
284+
std::cerr << "ftruncate of temporary test output file failed: " << strerror(errno) << "\n";
285+
}
286+
}
287+
288+
int EXPORT main(int argc, char const* argv[])
271289
{
272290
char const* executable = argv[0];
273291
// skip executable name
@@ -443,6 +461,11 @@ EXPORT int main(int argc, char const* argv[])
443461
try
444462
{
445463
#endif
464+
465+
#if defined TORRENT_BUILD_SIMULATOR
466+
lt::aux::random_engine().seed(0x82daf973);
467+
#endif
468+
446469
_g_test_failures = 0;
447470
(*t.fun)();
448471
#ifndef BOOST_NO_EXCEPTIONS

test/test.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE.
7373
void EXPORT report_failure(char const* err, char const* file, int line);
7474
int EXPORT print_failures();
7575
int EXPORT test_counter();
76+
void EXPORT reset_output();
7677

7778
typedef void (*unit_test_fun_t)();
7879

0 commit comments

Comments
 (0)