Skip to content

Commit

Permalink
Fix testLog
Browse files Browse the repository at this point in the history
  • Loading branch information
herve-er committed Jan 3, 2025
1 parent 58d00ca commit 5e8b930
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions test/libcommonserver/log/testlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,37 @@ void TestLog::testExpiredLogFiles(void) {
// This test checks that old archived log files are deleted after a certain time
clearLogDirectory();

// Generate a fake log file
// Generate a fake old log file
std::ofstream fakeLogFile(_logDir / APPLICATION_NAME "_fake.log.gz");
fakeLogFile << "Fake old log file" << std::endl;
fakeLogFile.close();
LOG_INFO(_logger, "Test log file expiration"); // Ensure the log file is created.

CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The current log file and the fake archived log file.
// Ensure that a new log file is created
LOG_INFO(_logger, "Test log file expiration");

// Check that we got 2 log files (the current one and the fake old one)
CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir));

// Set the expiration time to 2 seconds
auto *appender = static_cast<CustomRollingFileAppender *>(_logger.getAppender(Log::rfName).get());
appender->setExpire(2); // 2 seconds
Utility::msleep(1000);
appender->checkForExpiredFiles();

const auto now = std::chrono::system_clock::now();
KDC::testhelpers::setModificationDate(Log::instance()->getLogFilePath(), now);

CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir)); // The fake log file should not be deleted yet (< 2 seconds).

Utility::msleep(1000);
appender->checkForExpiredFiles();
const auto start = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
while (now - start < std::chrono::seconds(3)) {
now = std::chrono::system_clock::now();
KDC::testhelpers::setModificationDate(Log::instance()->getLogFilePath(),
now); // Prevent the current log file from being deleted.
appender->checkForExpiredFiles();
if (now - start < std::chrono::milliseconds(1500)) { // The fake log file should not be deleted yet.
CPPUNIT_ASSERT_EQUAL(2, countFilesInDirectory(_logDir));
} else if (countFilesInDirectory(_logDir) == 1) { // The fake log file MIGHT be deleted now.
break;
}
Utility::msleep(500);
}

CPPUNIT_ASSERT_EQUAL(1, countFilesInDirectory(_logDir)); // The fake log file should be deleted now.
CPPUNIT_ASSERT_EQUAL(1, countFilesInDirectory(_logDir)); // The fake log file SHOULD be deleted now.
appender->setExpire(CommonUtility::logsPurgeRate * 24 * 3600);
}

Expand Down Expand Up @@ -145,6 +154,8 @@ void TestLog::clearLogDirectory(void) const {
continue;
}
IoHelper::deleteItem(entry.path(), ioError);
CPPUNIT_ASSERT_EQUAL(IoError::Success, ioError);

}
CPPUNIT_ASSERT_EQUAL(IoError::Success, ioError);
CPPUNIT_ASSERT(endOfDirectory);
Expand Down

0 comments on commit 5e8b930

Please sign in to comment.