Flutter Engine
The Flutter Engine
dart_lifecycle_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/common/task_runners.h"
6#include "flutter/fml/paths.h"
7#include "flutter/fml/synchronization/count_down_latch.h"
8#include "flutter/fml/synchronization/waitable_event.h"
9#include "flutter/runtime/dart_vm.h"
10#include "flutter/runtime/dart_vm_lifecycle.h"
11#include "flutter/runtime/isolate_configuration.h"
12#include "flutter/testing/fixture_test.h"
13
14namespace flutter {
15namespace testing {
16
18
19TEST_F(DartLifecycleTest, CanStartAndShutdownVM) {
20 auto settings = CreateSettingsForFixture();
21 settings.leak_vm = false;
22 settings.enable_vm_service = false;
23 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
24 {
25 auto vm_ref = DartVMRef::Create(settings);
26 ASSERT_TRUE(DartVMRef::IsInstanceRunning());
27 }
28 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
29}
30
31TEST_F(DartLifecycleTest, CanStartAndShutdownVMOverAndOver) {
32 auto settings = CreateSettingsForFixture();
33 settings.leak_vm = false;
34 settings.enable_vm_service = false;
35 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
37 for (size_t i = 0; i < 10; i++) {
38 auto vm_ref = DartVMRef::Create(settings);
39 ASSERT_TRUE(DartVMRef::IsInstanceRunning());
40 ASSERT_EQ(DartVM::GetVMLaunchCount(), count + 1);
42 }
43 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
44}
45
46static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate(
47 const Settings& settings,
48 const DartVMData& vm,
49 const fml::RefPtr<fml::TaskRunner>& task_runner,
50 std::string entrypoint) {
51 FML_CHECK(!entrypoint.empty());
52 TaskRunners runners("io.flutter.test", task_runner, task_runner, task_runner,
53 task_runner);
54
55 auto isolate_configuration =
57
58 UIDartState::Context context(runners);
59 context.advisory_script_uri = "main.dart";
60 context.advisory_script_entrypoint = entrypoint.c_str();
61 auto isolate =
63 vm.GetSettings(), // settings
64 vm.GetIsolateSnapshot(), // isolate_snapshot
65 {}, // platform configuration
66 DartIsolate::Flags{}, // flags
67 nullptr, // root isolate create callback
68 settings.isolate_create_callback, // isolate create callback
69 settings.isolate_shutdown_callback, // isolate shutdown callback,
70 entrypoint, // dart entrypoint
71 std::nullopt, // dart entrypoint library
72 {}, // dart entrypoint arguments
73 std::move(isolate_configuration), // isolate configuration
74 context // engine context
75 )
76 .lock();
77
78 if (!isolate) {
79 FML_LOG(ERROR) << "Could not launch the root isolate.";
80 return nullptr;
81 }
82
83 return isolate;
84}
85
86// TODO(chinmaygarde): This unit-test is flaky and indicates thread un-safety
87// during shutdown. https://github.com/flutter/flutter/issues/36782
88TEST_F(DartLifecycleTest, DISABLED_ShuttingDownTheVMShutsDownAllIsolates) {
89 auto settings = CreateSettingsForFixture();
90 settings.leak_vm = false;
91 // Make sure the service protocol launches
92 settings.enable_vm_service = true;
93
94 auto thread_task_runner = CreateNewThread();
95
96 for (size_t i = 0; i < 3; i++) {
97 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
98
99 const auto last_launch_count = DartVM::GetVMLaunchCount();
100
101 auto vm_ref = DartVMRef::Create(settings);
102
103 ASSERT_TRUE(DartVMRef::IsInstanceRunning());
104 ASSERT_EQ(last_launch_count + 1, DartVM::GetVMLaunchCount());
105
106 const size_t isolate_count = 5;
107
108 fml::CountDownLatch latch(isolate_count);
109 auto vm_data = vm_ref.GetVMData();
110 for (size_t i = 0; i < isolate_count; ++i) {
111 thread_task_runner->PostTask(
112 [vm_data, &settings, &latch, thread_task_runner]() {
113 ASSERT_TRUE(CreateAndRunRootIsolate(settings, *vm_data.get(),
114 thread_task_runner,
115 "testIsolateShutdown"));
116 latch.CountDown();
117 });
118 }
119
120 latch.Wait();
121 }
122 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
123}
124
125} // namespace testing
126} // namespace flutter
int count
Definition: FontMgrTest.cpp:50
static std::weak_ptr< DartIsolate > CreateRunningRootIsolate(const Settings &settings, const fml::RefPtr< const DartSnapshot > &isolate_snapshot, std::unique_ptr< PlatformConfiguration > platform_configuration, Flags flags, const fml::closure &root_isolate_create_callback, const fml::closure &isolate_create_callback, const fml::closure &isolate_shutdown_callback, std::optional< std::string > dart_entrypoint, std::optional< std::string > dart_entrypoint_library, const std::vector< std::string > &dart_entrypoint_args, std::unique_ptr< IsolateConfiguration > isolate_configuration, const UIDartState::Context &context, const DartIsolate *spawning_isolate=nullptr)
Creates an instance of a root isolate and returns a weak pointer to the same. The isolate instance ma...
Definition: dart_isolate.cc:90
Provides thread-safe access to data that is necessary to bootstrap a new Dart VM instance....
Definition: dart_vm_data.h:18
fml::RefPtr< const DartSnapshot > GetIsolateSnapshot() const
Get the isolate snapshot necessary to launch isolates in the Dart VM. The Dart VM instance in which t...
Definition: dart_vm_data.cc:67
const Settings & GetSettings() const
The settings object from which the Dart snapshots were inferred.
Definition: dart_vm_data.cc:59
static DartVMRef Create(const Settings &settings, fml::RefPtr< const DartSnapshot > vm_snapshot=nullptr, fml::RefPtr< const DartSnapshot > isolate_snapshot=nullptr)
static bool IsInstanceRunning()
static size_t GetVMLaunchCount()
The number of times the VM has been launched in the process. This call is inherently racy because the...
Definition: dart_vm.cc:281
static std::unique_ptr< IsolateConfiguration > InferFromSettings(const Settings &settings, const std::shared_ptr< AssetManager > &asset_manager=nullptr, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer the isolate configuration from the Settings object. If the VM is configured for AOT...
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_CHECK(condition)
Definition: logging.h:85
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name)
TEST_F(DisplayListTest, Defaults)
static std::shared_ptr< DartIsolate > CreateAndRunRootIsolate(const Settings &settings, const DartVMData &vm, const fml::RefPtr< fml::TaskRunner > &task_runner, std::string entrypoint)
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
Definition: ui_dart_state.h:45
std::string advisory_script_entrypoint
Definition: ui_dart_state.h:93
#define ERROR(message)
Definition: elf_loader.cc:260