From 57f8abb73f430636697225bfde3e555112c7fad2 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 18 Apr 2019 11:32:33 -0700 Subject: [PATCH] Avoid leaking the VM in the shell unittests and assert VM state in existing tests. (#8628) --- shell/common/shell_test.cc | 1 + shell/common/shell_unittests.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/shell/common/shell_test.cc b/shell/common/shell_test.cc index d1dc5c406..9a9c16db4 100644 --- a/shell/common/shell_test.cc +++ b/shell/common/shell_test.cc @@ -83,6 +83,7 @@ void ShellTest::SetSnapshotsAndAssets(Settings& settings) { Settings ShellTest::CreateSettingsForFixture() { Settings settings; + settings.leak_vm = false; settings.task_observer_add = [](intptr_t key, fml::closure handler) { fml::MessageLoop::GetCurrent().AddTaskObserver(key, handler); }; diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index e4ba61e2b..9def55303 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -83,6 +83,7 @@ static bool ValidateShell(Shell* shell) { } TEST_F(ShellTest, InitializeWithInvalidThreads) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); TaskRunners task_runners("test", nullptr, nullptr, nullptr, nullptr); auto shell = Shell::Create( @@ -95,9 +96,11 @@ TEST_F(ShellTest, InitializeWithInvalidThreads) { return std::make_unique(shell.GetTaskRunners()); }); ASSERT_FALSE(shell); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, InitializeWithDifferentThreads) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( "io.flutter.test." + ::testing::GetCurrentTestName() + ".", @@ -117,9 +120,13 @@ TEST_F(ShellTest, InitializeWithDifferentThreads) { return std::make_unique(shell.GetTaskRunners()); }); ASSERT_TRUE(ValidateShell(shell.get())); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, InitializeWithSingleThread) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( "io.flutter.test." + ::testing::GetCurrentTestName() + ".", @@ -136,10 +143,14 @@ TEST_F(ShellTest, InitializeWithSingleThread) { [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); ASSERT_TRUE(ValidateShell(shell.get())); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); fml::MessageLoop::EnsureInitializedForCurrentThread(); auto task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); @@ -155,10 +166,14 @@ TEST_F(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { return std::make_unique(shell.GetTaskRunners()); }); ASSERT_TRUE(ValidateShell(shell.get())); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( "io.flutter.test." + ::testing::GetCurrentTestName() + ".", @@ -179,9 +194,13 @@ TEST_F(ShellTest, return std::make_unique(shell.GetTaskRunners()); }); ASSERT_TRUE(ValidateShell(shell.get())); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); Settings settings = CreateSettingsForFixture(); ThreadHost thread_host( "io.flutter.test." + ::testing::GetCurrentTestName() + ".", @@ -202,10 +221,14 @@ TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) { [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); ASSERT_TRUE(ValidateShell(shell.get())); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } TEST_F(ShellTest, FixturesAreFunctional) { + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); const auto settings = CreateSettingsForFixture(); auto shell = Shell::Create( GetTaskRunnersForFixture(), settings, @@ -239,6 +262,9 @@ TEST_F(ShellTest, FixturesAreFunctional) { latch.Wait(); main_latch.Wait(); + ASSERT_TRUE(DartVMRef::IsInstanceRunning()); + shell.reset(); + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } } // namespace testing -- GitLab