Commit e1fc1acd authored by Murali Vilayannur's avatar Murali Vilayannur Committed by Facebook Github Bot

Fix for failing travis CI tests

Summary:
Running the xlog test binary stand-alone revealed a bug that
was otherwise masked when running with buck test. The bug was that
2 tests ended up depending on the log-level associated with a default
category and would thus fail since the tests did not clean up
the log-level setting associated with those categories.
Attached diff fixes the issue by restoring the category's log-level in
a scope exit block.

Reviewed By: simpkins

Differential Revision: D7922086

fbshipit-source-id: 9d8ed917633ae214dc81075fbd38275160e64376
parent 5817bad2
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
#include <folly/logging/xlog.h> #include <folly/logging/xlog.h>
#include <folly/logging/LogCategory.h> #include <folly/logging/LogConfigParser.h>
#include <folly/logging/LogHandler.h> #include <folly/logging/LogHandler.h>
#include <folly/logging/LogMessage.h> #include <folly/logging/LogMessage.h>
#include <folly/logging/LoggerDB.h> #include <folly/logging/LoggerDB.h>
...@@ -31,19 +31,29 @@ using std::make_shared; ...@@ -31,19 +31,29 @@ using std::make_shared;
XLOG_SET_CATEGORY_NAME("xlog_test.main_file") XLOG_SET_CATEGORY_NAME("xlog_test.main_file")
// Note that the XLOG* macros always use the main LoggerDB singleton. namespace {
// There is no way to get them to use a test LoggerDB during unit tests. class XlogTest : public testing::Test {
// public:
// Therefore any configuration we do here affects the main log category XlogTest() {
// settings for the entire program. Fortunately all of the other unit tests do // Note that the XLOG* macros always use the main LoggerDB singleton.
// use testing LoggerDB objects. // There is no way to get them to use a test LoggerDB during unit tests.
//
// In order to ensure that changes to the LoggerDB singleton do not persist
// across test functions we reset the configuration to a fixed state before
// each test starts.
auto config =
parseLogConfig(".=WARN:default; default=stream:stream=stderr");
LoggerDB::get().resetConfig(config);
}
};
} // namespace
TEST(Xlog, xlogName) { TEST_F(XlogTest, xlogName) {
EXPECT_EQ("xlog_test.main_file", XLOG_GET_CATEGORY_NAME()); EXPECT_EQ("xlog_test.main_file", XLOG_GET_CATEGORY_NAME());
EXPECT_EQ("xlog_test.main_file", XLOG_GET_CATEGORY()->getName()); EXPECT_EQ("xlog_test.main_file", XLOG_GET_CATEGORY()->getName());
} }
TEST(Xlog, xlogIf) { TEST_F(XlogTest, xlogIf) {
auto handler = make_shared<TestLogHandler>(); auto handler = make_shared<TestLogHandler>();
LoggerDB::get().getCategory("xlog_test")->addHandler(handler); LoggerDB::get().getCategory("xlog_test")->addHandler(handler);
auto& messages = handler->getMessages(); auto& messages = handler->getMessages();
...@@ -115,7 +125,7 @@ TEST(Xlog, xlogIf) { ...@@ -115,7 +125,7 @@ TEST(Xlog, xlogIf) {
messages.clear(); messages.clear();
} }
TEST(Xlog, xlog) { TEST_F(XlogTest, xlog) {
auto handler = make_shared<TestLogHandler>(); auto handler = make_shared<TestLogHandler>();
LoggerDB::get().getCategory("xlog_test")->addHandler(handler); LoggerDB::get().getCategory("xlog_test")->addHandler(handler);
auto& messages = handler->getMessages(); auto& messages = handler->getMessages();
...@@ -166,7 +176,7 @@ TEST(Xlog, xlog) { ...@@ -166,7 +176,7 @@ TEST(Xlog, xlog) {
messages.clear(); messages.clear();
} }
TEST(Xlog, perFileCategoryHandling) { TEST_F(XlogTest, perFileCategoryHandling) {
using namespace logging_test; using namespace logging_test;
auto handler = make_shared<TestLogHandler>(); auto handler = make_shared<TestLogHandler>();
...@@ -256,7 +266,7 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -256,7 +266,7 @@ TEST(Xlog, perFileCategoryHandling) {
messages.clear(); messages.clear();
} }
TEST(Xlog, getXlogCategoryName) { TEST_F(XlogTest, getXlogCategoryName) {
EXPECT_EQ("foo.cpp", getXlogCategoryNameForFile("foo.cpp")); EXPECT_EQ("foo.cpp", getXlogCategoryNameForFile("foo.cpp"));
EXPECT_EQ("foo.h", getXlogCategoryNameForFile("foo.h")); EXPECT_EQ("foo.h", getXlogCategoryNameForFile("foo.h"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment