proxygen
ImmediateFileWriterTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (ImmediateFileWriter, readBatch)
 
 TEST (ImmediateFileWriter, immediateRead)
 
 TEST (ImmediateFileWriter, ioError)
 

Function Documentation

TEST ( ImmediateFileWriter  ,
readBatch   
)

Definition at line 32 of file ImmediateFileWriterTest.cpp.

References ASSERT_TRUE, folly::data(), EXPECT_EQ, folly::File::fd(), folly::readFile(), and string.

32  {
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 }
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
int fd() const
Definition: File.h:85
const char * string
Definition: Conv.cpp:212
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST ( ImmediateFileWriter  ,
immediateRead   
)

Definition at line 60 of file ImmediateFileWriterTest.cpp.

References ASSERT_GT, EXPECT_EQ, folly::gen::move, folly::readFull(), and string.

60  {
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 }
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ssize_t readFull(int fd, void *buf, size_t count)
Definition: FileUtil.cpp:126
Range< const char * > StringPiece
TEST ( ImmediateFileWriter  ,
ioError   
)

Definition at line 91 of file ImmediateFileWriterTest.cpp.

References folly::checkUnixError(), folly::netops::close(), EXPECT_EQ, EXPECT_THAT, testing::HasSubstr(), pipe(), and folly::LoggerDB::setInternalWarningHandler().

91  {
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 
115  LoggerDB::setInternalWarningHandler(nullptr);
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
#define EXPECT_THAT(value, matcher)
int close(NetworkSocket s)
Definition: NetOps.cpp:90
void pipe(CPUExecutor cpu, IOExecutor io)