Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with a race condition in logging tests #222

Merged
merged 2 commits into from Jan 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions tests/logging_tests.cpp
Expand Up @@ -36,22 +36,27 @@ BOOST_AUTO_TEST_CASE(log_reboot)
fc::path prev_log_filename = "";

BOOST_TEST_MESSAGE("Starting Loop");
int file_changed_count = 0;
for(int i = 0; i < conf.rotation_limit.to_seconds(); i++)
{
fc::log_context ctx(fc::log_level::all, "my_file.cpp", i, "my_method()");
fc::log_message my_log_message( ctx, "${message}", {"message","This is a test"} );
fc::time_point now = fc::time_point::now();
fa->log(my_log_message);

fc::time_point now = fc::time_point::now();
fc::time_point new_now = fc::time_point::now();
int64_t interval_seconds = conf.rotation_interval.to_seconds();
int64_t file_number = now.sec_since_epoch() / interval_seconds;
bool file_changed = ( new_now.sec_since_epoch() / interval_seconds != file_number );
if( file_changed )
++file_changed_count;
fc::time_point_sec start_time = fc::time_point_sec( (uint32_t)(file_number * interval_seconds) );
std::string timestamp_string = start_time.to_non_delimited_iso_string();
fc::path link_filename = conf.filename;
fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string);

if (prev_log_filename != log_filename) {
if (i > conf.rotation_interval.to_seconds()) {
if (i > conf.rotation_interval.to_seconds() && !file_changed ) {
std::string rez;
fc::read_file_contents(prev_log_filename, rez);
std::size_t found = rez.find("my_file.cpp:" + std::to_string(i - 1));
Expand All @@ -66,7 +71,10 @@ BOOST_AUTO_TEST_CASE(log_reboot)

fc::usleep(fc::seconds(1));
}
if( file_changed_count >= 3 ) {
BOOST_FAIL( "It is not expected that file name changes too frequently" );
}
BOOST_TEST_MESSAGE("Loop complete");
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()