ImmediateFileWriterTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <folly/Conv.h>
  20. #include <folly/Exception.h>
  21. #include <folly/FileUtil.h>
  22. #include <folly/experimental/TestUtil.h>
  23. #include <folly/logging/ImmediateFileWriter.h>
  24. #include <folly/logging/LoggerDB.h>
  25. #include <folly/portability/GMock.h>
  26. #include <folly/portability/GTest.h>
  27. using namespace folly;
  28. using folly::test::TemporaryFile;
  29. TEST(ImmediateFileWriter, readBatch) {
  30. TemporaryFile tmpFile{"logging_test"};
  31. ImmediateFileWriter writer{folly::File{tmpFile.fd(), false}};
  32. // Write several messages
  33. for (int n = 0; n < 10; ++n) {
  34. writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
  35. }
  36. // Read the log file and confirm it contains all of the expected messages
  37. std::string data;
  38. auto ret = folly::readFile(tmpFile.path().string().c_str(), data);
  39. ASSERT_TRUE(ret);
  40. std::string expected =
  41. "message 0\n"
  42. "message 1\n"
  43. "message 2\n"
  44. "message 3\n"
  45. "message 4\n"
  46. "message 5\n"
  47. "message 6\n"
  48. "message 7\n"
  49. "message 8\n"
  50. "message 9\n";
  51. EXPECT_EQ(expected, data);
  52. }
  53. TEST(ImmediateFileWriter, immediateRead) {
  54. TemporaryFile tmpFile{"logging_test"};
  55. ImmediateFileWriter writer{tmpFile.path().string()};
  56. // Write several messages, and read each one back immediately
  57. // after we write it.
  58. folly::File readf{tmpFile.path().string()};
  59. for (int n = 0; n < 10; ++n) {
  60. writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
  61. std::array<char, 1024> buf;
  62. auto sizeRead = folly::readFull(readf.fd(), buf.data(), buf.size());
  63. ASSERT_GT(sizeRead, 0);
  64. EXPECT_EQ(
  65. folly::to<std::string>("message ", n, "\n"),
  66. StringPiece(buf.data(), sizeRead));
  67. }
  68. }
  69. #ifndef _WIN32
  70. namespace {
  71. static std::vector<std::string>* internalWarnings;
  72. void handleLoggingError(
  73. StringPiece /* file */,
  74. int /* lineNumber */,
  75. std::string&& msg) {
  76. internalWarnings->emplace_back(std::move(msg));
  77. }
  78. } // namespace
  79. TEST(ImmediateFileWriter, ioError) {
  80. std::array<int, 2> fds;
  81. auto rc = pipe(fds.data());
  82. folly::checkUnixError(rc, "failed to create pipe");
  83. signal(SIGPIPE, SIG_IGN);
  84. // Set the LoggerDB internal warning handler so we can record the messages
  85. std::vector<std::string> logErrors;
  86. internalWarnings = &logErrors;
  87. LoggerDB::setInternalWarningHandler(handleLoggingError);
  88. // Create an ImmediateFileWriter that refers to a pipe whose read end is
  89. // closed, then log a bunch of messages to it.
  90. ::close(fds[0]);
  91. size_t numMessages = 100;
  92. {
  93. ImmediateFileWriter writer{folly::File{fds[1], true}};
  94. for (size_t n = 0; n < numMessages; ++n) {
  95. writer.writeMessage(folly::to<std::string>("message ", n, "\n"));
  96. sched_yield();
  97. }
  98. }
  99. LoggerDB::setInternalWarningHandler(nullptr);
  100. // ImmediateFileWriter should have generated one warning
  101. // for each attempt to log a message.
  102. //
  103. // (The default internalWarning() handler would have rate limited these
  104. // messages, but our test handler does not perform rate limiting.)
  105. for (const auto& msg : logErrors) {
  106. EXPECT_THAT(msg, testing::HasSubstr("error writing to log file"));
  107. EXPECT_THAT(msg, testing::HasSubstr("Broken pipe"));
  108. }
  109. EXPECT_EQ(numMessages, logErrors.size());
  110. }
  111. #endif