From 2add29d0355298937227b0b9adc73596167772f1 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 9 Oct 2018 16:37:30 -0700 Subject: [PATCH] Add tests that launch isolates in a relaunched VM. --- runtime/BUILD.gn | 1 + runtime/dart_isolate_unittests.cc | 15 ++++------ runtime/dart_vm.cc | 8 +++-- runtime/dart_vm_unittests.cc | 50 +++++++++++++++++++++++++++++++ testing/testing.cc | 4 ++- testing/testing.h | 4 +++ 6 files changed, 69 insertions(+), 13 deletions(-) diff --git a/runtime/BUILD.gn b/runtime/BUILD.gn index c4cb72d1170e5..6e8812320f7e0 100644 --- a/runtime/BUILD.gn +++ b/runtime/BUILD.gn @@ -129,6 +129,7 @@ executable("runtime_unittests") { ":runtime", ":runtime_fixtures", "$flutter_root/fml", + "$flutter_root/shell/common", "$flutter_root/lib/snapshot", "$flutter_root/testing", "//third_party/dart/runtime:libdart_jit", diff --git a/runtime/dart_isolate_unittests.cc b/runtime/dart_isolate_unittests.cc index cb7797abdcb74..9598b2a98e4ee 100644 --- a/runtime/dart_isolate_unittests.cc +++ b/runtime/dart_isolate_unittests.cc @@ -8,11 +8,6 @@ #include "flutter/testing/testing.h" #include "flutter/testing/thread_test.h" -#define CURRENT_TEST_NAME \ - std::string { \ - ::testing::UnitTest::GetInstance()->current_test_info()->name() \ - } - namespace blink { using DartIsolateTest = ::testing::ThreadTest; @@ -23,11 +18,11 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) { settings.task_observer_remove = [](intptr_t) {}; auto vm = DartVM::ForProcess(settings); ASSERT_TRUE(vm); - TaskRunners task_runners(CURRENT_TEST_NAME, // - GetCurrentTaskRunner(), // - GetCurrentTaskRunner(), // - GetCurrentTaskRunner(), // - GetCurrentTaskRunner() // + TaskRunners task_runners(testing::GetCurrentTestName(), // + GetCurrentTaskRunner(), // + GetCurrentTaskRunner(), // + GetCurrentTaskRunner(), // + GetCurrentTaskRunner() // ); auto weak_isolate = DartIsolate::CreateRootIsolate( vm.get(), // vm diff --git a/runtime/dart_vm.cc b/runtime/dart_vm.cc index 885f75ed17a17..b6d93a5b805cc 100644 --- a/runtime/dart_vm.cc +++ b/runtime/dart_vm.cc @@ -399,8 +399,6 @@ DartVM::DartVM(const Settings& settings, DartUI::InitForGlobal(); - Dart_SetFileModifiedCallback(&DartFileModifiedCallback); - { TRACE_EVENT0("flutter", "Dart_Initialize"); Dart_InitializeParams params = {}; @@ -438,6 +436,8 @@ DartVM::DartVM(const Settings& settings, } } + Dart_SetFileModifiedCallback(&DartFileModifiedCallback); + // Allow streaming of stdout and stderr by the Dart vm. Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, &ServiceStreamCancelCallback); @@ -449,12 +449,16 @@ DartVM::~DartVM() { if (Dart_CurrentIsolate() != nullptr) { Dart_ExitIsolate(); } + char* result = Dart_Cleanup(); + if (result != nullptr) { FML_LOG(ERROR) << "Could not cleanly shut down the Dart VM. Message: \"" << result << "\"."; free(result); } + + dart::bin::CleanupDartIo(); } const Settings& DartVM::GetSettings() const { diff --git a/runtime/dart_vm_unittests.cc b/runtime/dart_vm_unittests.cc index 37bdf43b2b2e6..9716020b9c1ae 100644 --- a/runtime/dart_vm_unittests.cc +++ b/runtime/dart_vm_unittests.cc @@ -3,6 +3,9 @@ // found in the LICENSE file. #include "flutter/runtime/dart_vm.h" +#include "flutter/shell/common/thread_host.h" +#include "flutter/testing/testing.h" +#include "flutter/testing/thread_test.h" #include "gtest/gtest.h" namespace blink { @@ -40,4 +43,51 @@ TEST(DartVM, CanReinitializeVMOverAndOver) { } } +using DartVMThreadTest = ::testing::ThreadTest; + +TEST_F(DartVMThreadTest, CanRunIsolatesInANewVM) { + for (size_t i = 0; i < 1000; ++i) { + // VM should not already be running. + ASSERT_FALSE(DartVM::ForProcessIfInitialized()); + auto vm = DartVM::ForProcess(kTestSettings); + ASSERT_TRUE(vm); + ASSERT_TRUE(DartVM::ForProcessIfInitialized()); + + Settings settings = {}; + + settings.task_observer_add = [](intptr_t, fml::closure) {}; + settings.task_observer_remove = [](intptr_t) {}; + + auto labels = testing::GetCurrentTestName() + std::to_string(i); + shell::ThreadHost host(labels, shell::ThreadHost::Type::UI | + shell::ThreadHost::Type::GPU | + shell::ThreadHost::Type::IO); + + TaskRunners task_runners( + labels, // task runner labels + GetCurrentTaskRunner(), // platform task runner + host.gpu_thread->GetTaskRunner(), // GPU task runner + host.ui_thread->GetTaskRunner(), // UI task runner + host.io_thread->GetTaskRunner() // IO task runner + ); + + auto weak_isolate = DartIsolate::CreateRootIsolate( + vm.get(), // vm + vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot + std::move(task_runners), // task runners + nullptr, // window + {}, // resource context + nullptr, // unref qeueue + "main.dart", // advisory uri + "main" // advisory entrypoint + ); + + auto root_isolate = weak_isolate.lock(); + ASSERT_TRUE(root_isolate); + ASSERT_EQ(root_isolate->GetPhase(), DartIsolate::Phase::LibrariesSetup); + ASSERT_TRUE(root_isolate->Shutdown()); + } +} + } // namespace blink diff --git a/testing/testing.cc b/testing/testing.cc index 835ce4fe05684..c74e64f3913bf 100644 --- a/testing/testing.cc +++ b/testing/testing.cc @@ -6,6 +6,8 @@ namespace testing { -// +std::string GetCurrentTestName() { + return UnitTest::GetInstance()->current_test_info()->name(); +} } // namespace testing diff --git a/testing/testing.h b/testing/testing.h index 2377ce5114917..b4568f0eaa66f 100644 --- a/testing/testing.h +++ b/testing/testing.h @@ -5,6 +5,8 @@ #ifndef TESTING_TESTING_H_ #define TESTING_TESTING_H_ +#include + #include "gtest/gtest.h" namespace testing { @@ -14,6 +16,8 @@ namespace testing { // error. const char* GetFixturesPath(); +std::string GetCurrentTestName(); + } // namespace testing #endif // TESTING_TESTING_H_