Skip to content

Commit

Permalink
Avoid leaking the VM in the shell unittests and assert VM state in ex…
Browse files Browse the repository at this point in the history
…isting tests. (#8628)
  • Loading branch information
chinmaygarde committed Apr 18, 2019
1 parent edef8bb commit 57f8abb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions shell/common/shell_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
26 changes: 26 additions & 0 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -95,9 +96,11 @@ TEST_F(ShellTest, InitializeWithInvalidThreads) {
return std::make_unique<Rasterizer>(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() + ".",
Expand All @@ -117,9 +120,13 @@ TEST_F(ShellTest, InitializeWithDifferentThreads) {
return std::make_unique<Rasterizer>(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() + ".",
Expand All @@ -136,10 +143,14 @@ TEST_F(ShellTest, InitializeWithSingleThread) {
[](Shell& shell) {
return std::make_unique<Rasterizer>(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();
Expand All @@ -155,10 +166,14 @@ TEST_F(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) {
return std::make_unique<Rasterizer>(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() + ".",
Expand All @@ -179,9 +194,13 @@ TEST_F(ShellTest,
return std::make_unique<Rasterizer>(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() + ".",
Expand All @@ -202,10 +221,14 @@ TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) {
[](Shell& shell) {
return std::make_unique<Rasterizer>(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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 57f8abb

Please sign in to comment.