From f7ba78c7b02b6fc2f60695458701f7453ecb7f5f Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 6 Jul 2023 08:05:01 -0700 Subject: [PATCH] Release log capture at end of test (flutter/engine#43429) Fixes https://github.com/flutter/flutter/issues/130036 Also fixes a potential issue where the shell wasn't getting destroyed at the end of the test. Since no log is actually printed ont he second go, the destructor of `LogMessage` never clears the static pointer, and the next test that tries to print a log tries to print to that pointer which is now garbage. --- engine/src/flutter/fml/logging.cc | 16 +++++- engine/src/flutter/fml/logging.h | 12 ++++- .../flutter/shell/common/shell_unittests.cc | 52 +++++++++++-------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/engine/src/flutter/fml/logging.cc b/engine/src/flutter/fml/logging.cc index bc5dd353f2..0bffd08776 100644 --- a/engine/src/flutter/fml/logging.cc +++ b/engine/src/flutter/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/engine/src/flutter/fml/logging.h b/engine/src/flutter/fml/logging.h index 21a5a5d827..39dac4c8e1 100644 --- a/engine/src/flutter/fml/logging.h +++ b/engine/src/flutter/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/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 16abef0dbc..18dec36314 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -4243,34 +4243,40 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) { task_runner); auto shell = CreateShell(settings, task_runners); - auto stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); + { + 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)); + // 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")); + 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")); + } - stream = std::make_shared(); - fml::CaptureNextLog(stream.get()); + { + 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)); + // 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(), ""); + EXPECT_EQ(log_capture.str(), ""); + } + + DestroyShell(std::move(shell), task_runners); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } } // namespace testing