proxygen
ImmediateFileWriterTest.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 #ifndef _WIN32
17 #include <unistd.h>
18 #endif
19 
20 #include <folly/Conv.h>
21 #include <folly/Exception.h>
22 #include <folly/FileUtil.h>
25 #include <folly/logging/LoggerDB.h>
28 
29 using namespace folly;
31 
33  TemporaryFile tmpFile{"logging_test"};
34  ImmediateFileWriter writer{folly::File{tmpFile.fd(), false}};
35 
36  // Write several messages
37  for (int n = 0; n < 10; ++n) {
38  writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
39  }
40 
41  // Read the log file and confirm it contains all of the expected messages
43  auto ret = folly::readFile(tmpFile.path().string().c_str(), data);
44  ASSERT_TRUE(ret);
45 
46  std::string expected =
47  "message 0\n"
48  "message 1\n"
49  "message 2\n"
50  "message 3\n"
51  "message 4\n"
52  "message 5\n"
53  "message 6\n"
54  "message 7\n"
55  "message 8\n"
56  "message 9\n";
57  EXPECT_EQ(expected, data);
58 }
59 
60 TEST(ImmediateFileWriter, immediateRead) {
61  TemporaryFile tmpFile{"logging_test"};
62  ImmediateFileWriter writer{tmpFile.path().string()};
63 
64  // Write several messages, and read each one back immediately
65  // after we write it.
66  folly::File readf{tmpFile.path().string()};
67  for (int n = 0; n < 10; ++n) {
68  writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
69 
70  std::array<char, 1024> buf;
71  auto sizeRead = folly::readFull(readf.fd(), buf.data(), buf.size());
72  ASSERT_GT(sizeRead, 0);
73  EXPECT_EQ(
74  folly::to<std::string>("message ", n, "\n"),
75  StringPiece(buf.data(), sizeRead));
76  }
77 }
78 
79 #ifndef _WIN32
80 namespace {
81 static std::vector<std::string>* internalWarnings;
82 
83 void handleLoggingError(
84  StringPiece /* file */,
85  int /* lineNumber */,
86  std::string&& msg) {
87  internalWarnings->emplace_back(std::move(msg));
88 }
89 } // namespace
90 
92  std::array<int, 2> fds;
93  auto rc = pipe(fds.data());
94  folly::checkUnixError(rc, "failed to create pipe");
95  signal(SIGPIPE, SIG_IGN);
96 
97  // Set the LoggerDB internal warning handler so we can record the messages
98  std::vector<std::string> logErrors;
99  internalWarnings = &logErrors;
100  LoggerDB::setInternalWarningHandler(handleLoggingError);
101 
102  // Create an ImmediateFileWriter that refers to a pipe whose read end is
103  // closed, then log a bunch of messages to it.
104  ::close(fds[0]);
105 
106  size_t numMessages = 100;
107  {
108  ImmediateFileWriter writer{folly::File{fds[1], true}};
109  for (size_t n = 0; n < numMessages; ++n) {
110  writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
111  sched_yield();
112  }
113  }
114 
116 
117  // ImmediateFileWriter should have generated one warning
118  // for each attempt to log a message.
119  //
120  // (The default internalWarning() handler would have rate limited these
121  // messages, but our test handler does not perform rate limiting.)
122  for (const auto& msg : logErrors) {
123  EXPECT_THAT(msg, testing::HasSubstr("error writing to log file"));
124  EXPECT_THAT(msg, testing::HasSubstr("Broken pipe"));
125  }
126  EXPECT_EQ(numMessages, logErrors.size());
127 }
128 #endif
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
ssize_t readFull(int fd, void *buf, size_t count)
Definition: FileUtil.cpp:126
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
int fd() const
Definition: File.h:85
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
static void setInternalWarningHandler(InternalWarningHandler handler)
Definition: LoggerDB.cpp:656
#define EXPECT_THAT(value, matcher)
const char * string
Definition: Conv.cpp:212
Range< const char * > StringPiece
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
int close(NetworkSocket s)
Definition: NetOps.cpp:90
TEST(SequencedExecutor, CPUThreadPoolExecutor)
void pipe(CPUExecutor cpu, IOExecutor io)