From 08c09e4af8d350297d2f1dcb3f5b9c8719daca84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Me=C3=9Fmer?= Date: Sat, 17 Oct 2015 16:42:56 +0200 Subject: [PATCH] Add a fork test case for logging --- test/logging/LoggingTest.cpp | 17 +++++++++++++++++ test/logging/testutils/LoggingTest.h | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/test/logging/LoggingTest.cpp b/test/logging/LoggingTest.cpp index 0513bb68..e356e755 100644 --- a/test/logging/LoggingTest.cpp +++ b/test/logging/LoggingTest.cpp @@ -62,3 +62,20 @@ TEST_F(LoggingTest, ErrorLog) { LOG(ERROR) << "My log message"; EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[error\\].*My log message.*")); } + +void logAndExit(const string &message) { + LOG(INFO) << message; + abort(); +} + +// fork() only forks the main thread. This test ensures that logging doesn't depend on threads that suddenly aren't +// there anymore after a fork(). +TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) { + auto sink = std::make_shared>(std::cerr, true); + setLogger(spdlog::create("StderrLogger", {sink})); + //TODO Use EXPECT_EXIT instead once the gtest version is new enough to support it + EXPECT_DEATH( + logAndExit("My log message"), + "My log message" + ); +} diff --git a/test/logging/testutils/LoggingTest.h b/test/logging/testutils/LoggingTest.h index f24e469f..54af4a2f 100644 --- a/test/logging/testutils/LoggingTest.h +++ b/test/logging/testutils/LoggingTest.h @@ -11,7 +11,7 @@ public: MockLogger(): _capturedLogData(), _sink(std::make_shared>(_capturedLogData, true)), - _logger(std::make_shared("MockLogger", _sink)) { + _logger(spdlog::create("MockLogger", {_sink})) { } ~MockLogger() {