Skip to content

Commit 24365fc

Browse files
authored
Release log capture at end of test (flutter#43429)
Fixes flutter/flutter#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.
1 parent 442d4f0 commit 24365fc

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

fml/logging.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,22 @@ LogMessage::LogMessage(LogSeverity severity,
7575
// static
7676
thread_local std::ostringstream* LogMessage::capture_next_log_stream_ = nullptr;
7777

78-
void CaptureNextLog(std::ostringstream* stream) {
79-
LogMessage::CaptureNextLog(stream);
78+
namespace testing {
79+
80+
LogCapture::LogCapture() {
81+
fml::LogMessage::CaptureNextLog(&stream_);
82+
}
83+
84+
LogCapture::~LogCapture() {
85+
fml::LogMessage::CaptureNextLog(nullptr);
8086
}
8187

88+
std::string LogCapture::str() const {
89+
return stream_.str();
90+
}
91+
92+
} // namespace testing
93+
8294
// static
8395
void LogMessage::CaptureNextLog(std::ostringstream* stream) {
8496
LogMessage::capture_next_log_stream_ = stream;

fml/logging.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@
1212

1313
namespace fml {
1414

15-
void CaptureNextLog(std::ostringstream* stream);
15+
namespace testing {
16+
struct LogCapture {
17+
LogCapture();
18+
~LogCapture();
19+
20+
std::string str() const;
21+
22+
private:
23+
std::ostringstream stream_;
24+
};
25+
} // namespace testing
1626

1727
class LogMessageVoidify {
1828
public:

shell/common/shell_unittests.cc

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,34 +4243,40 @@ TEST_F(ShellTest, PrintsErrorWhenPlatformMessageSentFromWrongThread) {
42434243
task_runner);
42444244
auto shell = CreateShell(settings, task_runners);
42454245

4246-
auto stream = std::make_shared<std::ostringstream>();
4247-
fml::CaptureNextLog(stream.get());
4248-
4249-
// The next call will result in a thread checker violation.
4250-
fml::ThreadChecker::DisableNextThreadCheckFailure();
4251-
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
4252-
"com.test.plugin", nullptr));
4253-
4254-
EXPECT_THAT(stream->str(),
4255-
::testing::EndsWith(
4256-
"The 'com.test.plugin' channel sent a message from native to "
4257-
"Flutter on a non-platform thread. Platform channel messages "
4258-
"must be sent on the platform thread. Failure to do so may "
4259-
"result in data loss or crashes, and must be fixed in the "
4260-
"plugin or application code creating that channel.\nSee "
4261-
"https://docs.flutter.dev/platform-integration/"
4262-
"platform-channels#channels-and-platform-threading for more "
4263-
"information.\n"));
4264-
4265-
stream = std::make_shared<std::ostringstream>();
4266-
fml::CaptureNextLog(stream.get());
4267-
4268-
// The next call will result in a thread checker violation.
4269-
fml::ThreadChecker::DisableNextThreadCheckFailure();
4270-
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
4271-
"com.test.plugin", nullptr));
4272-
4273-
EXPECT_EQ(stream->str(), "");
4246+
{
4247+
fml::testing::LogCapture log_capture;
4248+
4249+
// The next call will result in a thread checker violation.
4250+
fml::ThreadChecker::DisableNextThreadCheckFailure();
4251+
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
4252+
"com.test.plugin", nullptr));
4253+
4254+
EXPECT_THAT(
4255+
log_capture.str(),
4256+
::testing::EndsWith(
4257+
"The 'com.test.plugin' channel sent a message from native to "
4258+
"Flutter on a non-platform thread. Platform channel messages "
4259+
"must be sent on the platform thread. Failure to do so may "
4260+
"result in data loss or crashes, and must be fixed in the "
4261+
"plugin or application code creating that channel.\nSee "
4262+
"https://docs.flutter.dev/platform-integration/"
4263+
"platform-channels#channels-and-platform-threading for more "
4264+
"information.\n"));
4265+
}
4266+
4267+
{
4268+
fml::testing::LogCapture log_capture;
4269+
4270+
// The next call will result in a thread checker violation.
4271+
fml::ThreadChecker::DisableNextThreadCheckFailure();
4272+
SendPlatformMessage(shell.get(), std::make_unique<PlatformMessage>(
4273+
"com.test.plugin", nullptr));
4274+
4275+
EXPECT_EQ(log_capture.str(), "");
4276+
}
4277+
4278+
DestroyShell(std::move(shell), task_runners);
4279+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
42744280
}
42754281

42764282
} // namespace testing

0 commit comments

Comments
 (0)