logging: update initialization code to use the new LogConfig logic
[folly.git] / folly / experimental / logging / test / AsyncFileWriterTest.cpp
index 962ec8c77c3848da8c121d4fb88b780ab9fad778..eefcf746f6c378efdd61936dd54b23b90d169dd1 100644 (file)
@@ -29,6 +29,7 @@
 #include <folly/futures/Future.h>
 #include <folly/futures/Promise.h>
 #include <folly/init/Init.h>
+#include <folly/lang/SafeAssert.h>
 #include <folly/portability/GFlags.h>
 #include <folly/portability/GMock.h>
 #include <folly/portability/GTest.h>
@@ -86,6 +87,7 @@ TEST(AsyncFileWriter, simpleMessages) {
       sched_yield();
     }
   }
+  tmpFile.close();
 
   std::string data;
   auto ret = folly::readFile(tmpFile.path().string().c_str(), data);
@@ -105,7 +107,6 @@ TEST(AsyncFileWriter, simpleMessages) {
   EXPECT_EQ(expected, data);
 }
 
-#ifndef _WIN32
 namespace {
 static std::vector<std::string>* internalWarnings;
 
@@ -115,7 +116,7 @@ void handleLoggingError(
     std::string&& msg) {
   internalWarnings->emplace_back(std::move(msg));
 }
-}
+} // namespace
 
 TEST(AsyncFileWriter, ioError) {
   // Set the LoggerDB internal warning handler so we can record the messages
@@ -127,7 +128,9 @@ TEST(AsyncFileWriter, ioError) {
   std::array<int, 2> fds;
   auto rc = pipe(fds.data());
   folly::checkUnixError(rc, "failed to create pipe");
+#ifndef _WIN32
   signal(SIGPIPE, SIG_IGN);
+#endif
   ::close(fds[0]);
 
   // Log a bunch of messages to the writer
@@ -145,11 +148,23 @@ TEST(AsyncFileWriter, ioError) {
   // AsyncFileWriter should have some internal warning messages about the
   // log failures.  This will generally be many fewer than the number of
   // messages we wrote, though, since it performs write batching.
+  //
+  // GTest on Windows doesn't support alternation in the regex syntax -_-....
+  const std::string kExpectedErrorMessage =
+#if _WIN32
+      // The `pipe` call above is actually implemented via sockets, so we get
+      // a different error message.
+      "An established connection was aborted by the software in your host machine\\.";
+#else
+      "Broken pipe";
+#endif
+
   for (const auto& msg : logErrors) {
     EXPECT_THAT(
         msg,
         testing::ContainsRegex(
-            "error writing to log file .* in AsyncFileWriter.*: Broken pipe"));
+            "error writing to log file .* in AsyncFileWriter.*: " +
+            kExpectedErrorMessage));
   }
   EXPECT_GT(logErrors.size(), 0);
   EXPECT_LE(logErrors.size(), numMessages);
@@ -191,7 +206,7 @@ size_t fillUpPipe(int fd) {
 
   return totalBytes;
 }
-}
+} // namespace
 
 TEST(AsyncFileWriter, flush) {
   // Set up a pipe(), then write data to the write endpoint until it fills up
@@ -239,7 +254,6 @@ TEST(AsyncFileWriter, flush) {
   // Make sure flush completes successfully now
   future.get(10ms);
 }
-#endif
 
 // A large-ish message suffix, just to consume space and help fill up
 // log buffers faster.
@@ -281,7 +295,9 @@ class ReadStats {
   }
   void writerFinished(size_t threadID, size_t messagesWritten, uint32_t flags) {
     auto map = perThreadWriteData_.wlock();
-    assert(map->find(threadID) == map->end());
+    FOLLY_SAFE_CHECK(
+        map->find(threadID) == map->end(),
+        "multiple writer threads with same ID");
     auto& data = (*map)[threadID];
     data.numMessagesWritten = messagesWritten;
     data.flags = flags;
@@ -599,8 +615,7 @@ TEST(AsyncFileWriter, discard) {
 int main(int argc, char* argv[]) {
   testing::InitGoogleTest(&argc, argv);
   folly::init(&argc, &argv);
-  // Don't use async logging in the async logging tests :-)
-  folly::initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, /* async */ false);
+  folly::initLogging(FLAGS_logging);
 
   return RUN_ALL_TESTS();
 }