proxygen
SyncLevelTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <folly/test/TestUtils.h>
18 #include <queue>
19 
20 #include <folly/logging/Init.h>
24 #include <folly/logging/LoggerDB.h>
27 #include <folly/logging/xlog.h>
28 
29 namespace folly {
30 
31 class TestLogWriter : public LogWriter {
32  public:
33  void writeMessage(folly::StringPiece /* buffer */, uint32_t /* flags */ = 0)
34  override {
36  }
37 
38  void flush() override {
41  }
42 
45 
46  bool ttyOutput() const override {
47  return false;
48  }
49 };
50 
52  public:
53  TestHandlerFactory(const std::shared_ptr<TestLogWriter> writer)
54  : writer_(writer) {}
55 
56  StringPiece getType() const override {
57  return "test";
58  }
59 
60  std::shared_ptr<LogHandler> createHandler(const Options& options) override {
61  TestWriterFactory writerFactory{writer_};
63  getType(), &writerFactory, options);
64  }
65 
66  private:
67  std::shared_ptr<TestLogWriter> writer_;
69  public:
70  TestWriterFactory(std::shared_ptr<TestLogWriter> writer)
71  : writer_(writer) {}
72 
73  bool processOption(StringPiece /* name */, StringPiece /* value */)
74  override {
75  return false;
76  }
77 
78  std::shared_ptr<LogWriter> createWriter() override {
79  return writer_;
80  }
81 
82  private:
83  std::shared_ptr<TestLogWriter> writer_;
84  };
85 };
86 
87 } // namespace folly
88 
89 using namespace folly;
90 
91 namespace {
92 class SyncLevelTest : public testing::Test {
93  public:
94  SyncLevelTest() {
95  writer = std::make_shared<TestLogWriter>();
96  db.registerHandlerFactory(
97  std::make_unique<TestHandlerFactory>(writer), true);
98  db.resetConfig(
99  parseLogConfig("test=INFO:default; default=test:sync_level=WARN"));
100  }
101 
103  Logger logger{&db, "test"};
104  std::shared_ptr<TestLogWriter> writer;
105 };
106 } // namespace
107 
108 TEST_F(SyncLevelTest, NoLogTest) {
109  FB_LOG(logger, DBG9) << "DBG9";
110  FB_LOG(logger, DBG1) << "DBG1";
111  FB_LOG(logger, DBG0) << "DBG0";
112 
113  EXPECT_EQ(writer->unflushed_messages_count, 0);
114  EXPECT_EQ(writer->flushed_messages_count, 0);
115 }
116 
117 TEST_F(SyncLevelTest, SimpleAsyncTest) {
118  FB_LOG(logger, INFO) << "INFO";
119  FB_LOG(logger, INFO) << "INFO";
120  FB_LOG(logger, INFO) << "INFO";
121  FB_LOG(logger, INFO) << "INFO";
122 
123  EXPECT_EQ(writer->unflushed_messages_count, 4);
124  EXPECT_EQ(writer->flushed_messages_count, 0);
125 
126  FB_LOG(logger, WARN) << "WARN";
127 
128  EXPECT_EQ(writer->unflushed_messages_count, 0);
129  EXPECT_EQ(writer->flushed_messages_count, 5);
130 
131  FB_LOG(logger, DBG0) << "DBG0";
132  FB_LOG(logger, INFO9) << "INFO9";
133 
134  EXPECT_EQ(writer->unflushed_messages_count, 1);
135  EXPECT_EQ(writer->flushed_messages_count, 5);
136 
137  FB_LOG(logger, INFO) << "INFO";
138  FB_LOG(logger, WARN) << "WARN";
139 
140  EXPECT_EQ(writer->unflushed_messages_count, 0);
141  EXPECT_EQ(writer->flushed_messages_count, 8);
142 
143  FB_LOG(logger, INFO) << "INFO";
144  FB_LOG(logger, CRITICAL) << "CRITICAL";
145 
146  EXPECT_EQ(writer->unflushed_messages_count, 0);
147  EXPECT_EQ(writer->flushed_messages_count, 10);
148 }
std::shared_ptr< TestLogWriter > writer_
static std::shared_ptr< StandardLogHandler > createHandler(StringPiece type, WriterFactory *writerFactory, const Options &options)
TestHandlerFactory(const std::shared_ptr< TestLogWriter > writer)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool ttyOutput() const override
void flush() override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define FB_LOG(logger, level,...)
Definition: Logger.h:34
LogConfig parseLogConfig(StringPiece value)
StringPiece getType() const override
std::shared_ptr< LogHandler > createHandler(const Options &options) override
std::shared_ptr< LogWriter > createWriter() override
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
std::shared_ptr< TestLogWriter > writer_
bool processOption(StringPiece, StringPiece) override
std::unordered_map< std::string, std::string > Options
TestWriterFactory(std::shared_ptr< TestLogWriter > writer)
void writeMessage(folly::StringPiece, uint32_t=0) override