From d79249ba0e3eda94286fcac8cba244dcdd4bf944 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 5 Jul 2023 13:25:56 -0700 Subject: [PATCH 1/2] Release log capture at end of test --- shell/common/shell_unittests.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 16abef0dbc0f3..6cf94f3c0c6f7 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -4271,6 +4271,12 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) { "com.test.plugin", nullptr)); EXPECT_EQ(stream->str(), ""); + + // Reset the log stream capturing after verifying no log was printed. + fml::CaptureNextLog(nullptr); + + DestroyShell(std::move(shell), task_runners); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } } // namespace testing From ceaf6b16a9cbf0df96c737458cb3fd1430c03ec2 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 5 Jul 2023 15:13:29 -0700 Subject: [PATCH 2/2] RAII --- fml/logging.cc | 16 +++++++-- fml/logging.h | 12 ++++++- shell/common/shell_unittests.cc | 62 ++++++++++++++++----------------- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/fml/logging.cc b/fml/logging.cc index bc5dd353f2120..0bffd08776f29 100644 --- a/fml/logging.cc +++ b/fml/logging.cc @@ -75,10 +75,22 @@ LogMessage::LogMessage(LogSeverity severity, // static thread_local std::ostringstream* LogMessage::capture_next_log_stream_ = nullptr; -void CaptureNextLog(std::ostringstream* stream) { - LogMessage::CaptureNextLog(stream); +namespace testing { + +LogCapture::LogCapture() { + fml::LogMessage::CaptureNextLog(&stream_); +} + +LogCapture::~LogCapture() { + fml::LogMessage::CaptureNextLog(nullptr); } +std::string LogCapture::str() const { + return stream_.str(); +} + +} // namespace testing + // static void LogMessage::CaptureNextLog(std::ostringstream* stream) { LogMessage::capture_next_log_stream_ = stream; diff --git a/fml/logging.h b/fml/logging.h index 21a5a5d82715d..39dac4c8e1568 100644 --- a/fml/logging.h +++ b/fml/logging.h @@ -12,7 +12,17 @@ namespace fml { -void CaptureNextLog(std::ostringstream* stream); +namespace testing { +struct LogCapture { + LogCapture(); + ~LogCapture(); + + std::string str() const; + + private: + std::ostringstream stream_; +}; +} // namespace testing class LogMessageVoidify { public: diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 6cf94f3c0c6f7..18dec36314c68 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -4243,37 +4243,37 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) { task_runner); auto shell = CreateShell(settings, task_runners); - auto stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); - - // The next call will result in a thread checker violation. - fml::ThreadChecker::DisableNextThreadCheckFailure(); - SendPlatformMessage(shell.get(), std::make_unique( - "com.test.plugin", nullptr)); - - EXPECT_THAT(stream->str(), - ::testing::EndsWith( - "The 'com.test.plugin' channel sent a message from native to " - "Flutter on a non-platform thread. Platform channel messages " - "must be sent on the platform thread. Failure to do so may " - "result in data loss or crashes, and must be fixed in the " - "plugin or application code creating that channel.\nSee " - "https://docs.flutter.dev/platform-integration/" - "platform-channels#channels-and-platform-threading for more " - "information.\n")); - - stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); - - // The next call will result in a thread checker violation. - fml::ThreadChecker::DisableNextThreadCheckFailure(); - SendPlatformMessage(shell.get(), std::make_unique( - "com.test.plugin", nullptr)); - - EXPECT_EQ(stream->str(), ""); - - // Reset the log stream capturing after verifying no log was printed. - fml::CaptureNextLog(nullptr); + { + fml::testing::LogCapture log_capture; + + // The next call will result in a thread checker violation. + fml::ThreadChecker::DisableNextThreadCheckFailure(); + SendPlatformMessage(shell.get(), std::make_unique( + "com.test.plugin", nullptr)); + + EXPECT_THAT( + log_capture.str(), + ::testing::EndsWith( + "The 'com.test.plugin' channel sent a message from native to " + "Flutter on a non-platform thread. Platform channel messages " + "must be sent on the platform thread. Failure to do so may " + "result in data loss or crashes, and must be fixed in the " + "plugin or application code creating that channel.\nSee " + "https://docs.flutter.dev/platform-integration/" + "platform-channels#channels-and-platform-threading for more " + "information.\n")); + } + + { + fml::testing::LogCapture log_capture; + + // The next call will result in a thread checker violation. + fml::ThreadChecker::DisableNextThreadCheckFailure(); + SendPlatformMessage(shell.get(), std::make_unique( + "com.test.plugin", nullptr)); + + EXPECT_EQ(log_capture.str(), ""); + } DestroyShell(std::move(shell), task_runners); ASSERT_FALSE(DartVMRef::IsInstanceRunning());