Make test_event more reliable

Look for the expected message several times, with short sleeps in
between. That way we will not sleep more than necessary, yet will
not immediately give up either.
This commit is contained in:
Johan Wikman
2018-06-21 15:27:41 +03:00
parent 75ddfe4c14
commit 998798c90c

View File

@ -481,26 +481,41 @@ int test_logging()
MXS_LOG_EVENT(event::AUTHENTICATION_FAILURE, "%s", id.c_str()); MXS_LOG_EVENT(event::AUTHENTICATION_FAILURE, "%s", id.c_str());
// Short sleep to increase the likelyhood that the logged message ends // We have no control over how quickly syslog messages are flushed
// ends up where we expect it to be. // to the file. So, we try a few times before giving up.
sleep(2);
bool found = false; bool found = false;
int attempts = 0;
const int MAX_ATTEMPTS = 10;
ifstream in("/var/log/auth.log"); do
if (in)
{ {
string line; ++attempts;
while (std::getline(in, line))
sleep(1);
const char* zName = "/var/log/auth.log";
ifstream in(zName);
if (in)
{ {
if (line.find(id) != string::npos) string line;
while (std::getline(in, line))
{ {
found = true; if (line.find(id) != string::npos)
cout << "notice: Found '" << id << "' in line '" << line << "'." << endl; {
found = true;
cout << "notice: Found '" << id << "' in line '" << line << "'." << endl;
}
} }
} }
else
{
cerr << "error: Could not open '" << zName << "'." << endl;
attempts = MAX_ATTEMPTS;
}
} }
while (!found && (attempts < MAX_ATTEMPTS));
return found ? 0 : 1; return found ? 0 : 1;
} }