From 998798c90ccc6517e6d3ea850e450d8fb28c7097 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 21 Jun 2018 15:27:41 +0300 Subject: [PATCH] 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. --- server/core/test/test_event.cc | 37 ++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/server/core/test/test_event.cc b/server/core/test/test_event.cc index bc4d719d3..68a07005b 100644 --- a/server/core/test/test_event.cc +++ b/server/core/test/test_event.cc @@ -481,26 +481,41 @@ int test_logging() MXS_LOG_EVENT(event::AUTHENTICATION_FAILURE, "%s", id.c_str()); - // Short sleep to increase the likelyhood that the logged message ends - // ends up where we expect it to be. - sleep(2); + // We have no control over how quickly syslog messages are flushed + // to the file. So, we try a few times before giving up. bool found = false; + int attempts = 0; + const int MAX_ATTEMPTS = 10; - ifstream in("/var/log/auth.log"); - - if (in) + do { - string line; - while (std::getline(in, line)) + ++attempts; + + 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; - cout << "notice: Found '" << id << "' in line '" << line << "'." << endl; + if (line.find(id) != string::npos) + { + 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; }