Fixed a bug in the test case
Summary: The unit test was supposed to check that the old file and the new file contains the header message. Test Plan: Run the unit test. Reviewers: sdong Reviewed By: sdong Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D33705
This commit is contained in:
parent
4f514a53d6
commit
a047409ae9
@ -69,6 +69,11 @@ class AutoRollLogger : public Logger {
|
|||||||
call_NowMicros_every_N_records_ = call_NowMicros_every_N_records;
|
call_NowMicros_every_N_records_ = call_NowMicros_every_N_records;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expose the log file path for testing purpose
|
||||||
|
std::string TEST_log_fname() const {
|
||||||
|
return log_fname_;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool LogExpired();
|
bool LogExpired();
|
||||||
Status ResetLogger();
|
Status ResetLogger();
|
||||||
|
73
util/auto_roll_logger_test.cc
Executable file → Normal file
73
util/auto_roll_logger_test.cc
Executable file → Normal file
@ -4,6 +4,7 @@
|
|||||||
// of patent rights can be found in the PATENTS file in the same directory.
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
//
|
//
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@ -287,6 +288,43 @@ TEST(AutoRollLoggerTest, InfoLogLevel) {
|
|||||||
|
|
||||||
// Test the logger Header function for roll over logs
|
// Test the logger Header function for roll over logs
|
||||||
// We expect the new logs creates as roll over to carry the headers specified
|
// We expect the new logs creates as roll over to carry the headers specified
|
||||||
|
static list<string> GetOldFileNames(const string& path) {
|
||||||
|
const string dirname = path.substr(/*start=*/ 0, path.find_last_of("/"));
|
||||||
|
const string fname = path.substr(path.find_last_of("/") + 1);
|
||||||
|
|
||||||
|
vector<string> children;
|
||||||
|
Env::Default()->GetChildren(dirname, &children);
|
||||||
|
|
||||||
|
// We know that the old log files are named [path]<something>
|
||||||
|
// Return all entities that match the pattern
|
||||||
|
list<string> ret;
|
||||||
|
for (auto child : children) {
|
||||||
|
if (fname != child && child.find(fname) == 0) {
|
||||||
|
ret.push_back(dirname + "/" + child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the number of lines where a given pattern was found in the file
|
||||||
|
static size_t GetLinesCount(const string& fname, const string& pattern) {
|
||||||
|
stringstream ssbuf;
|
||||||
|
string line;
|
||||||
|
size_t count = 0;
|
||||||
|
|
||||||
|
ifstream inFile(fname.c_str());
|
||||||
|
ssbuf << inFile.rdbuf();
|
||||||
|
|
||||||
|
while (getline(ssbuf, line)) {
|
||||||
|
if (line.find(pattern) != std::string::npos) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(AutoRollLoggerTest, LogHeaderTest) {
|
TEST(AutoRollLoggerTest, LogHeaderTest) {
|
||||||
static const size_t MAX_HEADERS = 10;
|
static const size_t MAX_HEADERS = 10;
|
||||||
static const size_t LOG_MAX_SIZE = 1024 * 5;
|
static const size_t LOG_MAX_SIZE = 1024 * 5;
|
||||||
@ -302,29 +340,32 @@ TEST(AutoRollLoggerTest, LogHeaderTest) {
|
|||||||
Header(&logger, "%s %d", HEADER_STR.c_str(), i);
|
Header(&logger, "%s %d", HEADER_STR.c_str(), i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const string& newfname = logger.TEST_log_fname().c_str();
|
||||||
|
|
||||||
// log enough data to cause a roll over
|
// log enough data to cause a roll over
|
||||||
size_t i = 0;
|
int i = 0;
|
||||||
while (logger.GetLogFileSize() < LOG_MAX_SIZE) {
|
for (size_t iter = 0; iter < 2; iter++) {
|
||||||
Info(&logger, (kSampleMessage + ":LogHeaderTest line %d").c_str(), i);
|
while (logger.GetLogFileSize() < LOG_MAX_SIZE) {
|
||||||
++i;
|
Info(&logger, (kSampleMessage + ":LogHeaderTest line %d").c_str(), i);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
Info(&logger, "Rollover");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flus the log for the latest file
|
||||||
LogFlush(&logger);
|
LogFlush(&logger);
|
||||||
|
|
||||||
// verify that the new log contains all the header logs
|
const list<string> oldfiles = GetOldFileNames(newfname);
|
||||||
std::stringstream ssbuf;
|
|
||||||
std::string line;
|
|
||||||
size_t count = 0;
|
|
||||||
|
|
||||||
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
ASSERT_EQ(oldfiles.size(), (size_t) 2);
|
||||||
ssbuf << inFile.rdbuf();
|
|
||||||
|
|
||||||
while (getline(ssbuf, line)) {
|
for (auto oldfname : oldfiles) {
|
||||||
if (line.find(HEADER_STR) != std::string::npos) {
|
// verify that the files rolled over
|
||||||
count++;
|
ASSERT_NE(oldfname, newfname);
|
||||||
}
|
// verify that the old log contains all the header logs
|
||||||
|
ASSERT_EQ(GetLinesCount(oldfname, HEADER_STR), MAX_HEADERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT_EQ(count, MAX_HEADERS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AutoRollLoggerTest, LogFileExistence) {
|
TEST(AutoRollLoggerTest, LogFileExistence) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user