4
4
5
5
#include " flutter/common/task_runners.h"
6
6
#include " flutter/fml/paths.h"
7
+ #include " flutter/fml/synchronization/count_down_latch.h"
7
8
#include " flutter/fml/synchronization/waitable_event.h"
8
9
#include " flutter/runtime/dart_vm.h"
9
10
#include " flutter/runtime/dart_vm_lifecycle.h"
@@ -41,8 +42,7 @@ TEST_F(DartLifecycleTest, CanStartAndShutdownVMOverAndOver) {
41
42
ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
42
43
}
43
44
44
- static void CreateAndRunRootIsolate (
45
- std::shared_ptr<DartIsolate>& isolate_result,
45
+ static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate (
46
46
const Settings& settings,
47
47
const DartVMData& vm,
48
48
fml::RefPtr<fml::TaskRunner> task_runner,
@@ -67,73 +67,74 @@ static void CreateAndRunRootIsolate(
67
67
68
68
if (!isolate) {
69
69
FML_LOG (ERROR) << " Could not create valid isolate." ;
70
- return ;
70
+ return nullptr ;
71
71
}
72
72
73
73
if (DartVM::IsRunningPrecompiledCode ()) {
74
74
if (!isolate->PrepareForRunningFromPrecompiledCode ()) {
75
75
FML_LOG (ERROR)
76
76
<< " Could not prepare to run the isolate from precompiled code." ;
77
- return ;
77
+ return nullptr ;
78
78
}
79
79
80
80
} else {
81
81
if (!isolate->PrepareForRunningFromKernels (
82
82
settings.application_kernels ())) {
83
83
FML_LOG (ERROR) << " Could not prepare isolate from application kernels." ;
84
- return ;
84
+ return nullptr ;
85
85
}
86
86
}
87
87
88
88
if (isolate->GetPhase () != DartIsolate::Phase::Ready) {
89
89
FML_LOG (ERROR) << " Isolate was not ready." ;
90
- return ;
90
+ return nullptr ;
91
91
}
92
92
93
93
if (!isolate->Run (entrypoint, settings.root_isolate_create_callback )) {
94
94
FML_LOG (ERROR) << " Could not run entrypoint: " << entrypoint << " ." ;
95
- return ;
95
+ return nullptr ;
96
96
}
97
97
98
98
if (isolate->GetPhase () != DartIsolate::Phase::Running) {
99
99
FML_LOG (ERROR) << " Isolate was not Running." ;
100
- return ;
100
+ return nullptr ;
101
101
}
102
102
103
- isolate_result = isolate;
104
- }
105
-
106
- static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate (
107
- const Settings& settings,
108
- const DartVMData& vm,
109
- fml::RefPtr<fml::TaskRunner> task_runner,
110
- std::string entrypoint) {
111
- fml::AutoResetWaitableEvent latch;
112
- std::shared_ptr<DartIsolate> isolate;
113
- fml::TaskRunner::RunNowOrPostTask (task_runner, [&]() {
114
- CreateAndRunRootIsolate (isolate, settings, vm, task_runner, entrypoint);
115
- latch.Signal ();
116
- });
117
- latch.Wait ();
118
103
return isolate;
119
104
}
120
105
121
- TEST_F (DartLifecycleTest, ShuttingDownTheVMShutsDownTheIsolate ) {
106
+ TEST_F (DartLifecycleTest, ShuttingDownTheVMShutsDownAllIsolates ) {
122
107
auto settings = CreateSettingsForFixture ();
123
108
settings.leak_vm = false ;
124
- settings.enable_observatory = false ;
125
- ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
126
- {
109
+ // Make sure the service protocol launches
110
+ settings.enable_observatory = true ;
111
+
112
+ for (size_t i = 0 ; i < 3 ; i++) {
113
+ ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
114
+
115
+ const auto last_launch_count = DartVM::GetVMLaunchCount ();
116
+
127
117
auto vm_ref = DartVMRef::Create (settings);
118
+
128
119
ASSERT_TRUE (DartVMRef::IsInstanceRunning ());
129
- ASSERT_EQ (vm_ref->GetIsolateCount (), 0u );
130
- auto isolate =
131
- CreateAndRunRootIsolate (settings, *vm_ref.GetVMData (),
132
- GetThreadTaskRunner (), " testIsolateShutdown" );
133
- ASSERT_TRUE (isolate);
134
- ASSERT_EQ (vm_ref->GetIsolateCount (), 1u );
135
- vm_ref->ShutdownAllIsolates ();
136
- ASSERT_EQ (vm_ref->GetIsolateCount (), 0u );
120
+ ASSERT_EQ (last_launch_count + 1 , DartVM::GetVMLaunchCount ());
121
+
122
+ const size_t isolate_count = 100 ;
123
+
124
+ fml::CountDownLatch latch (isolate_count);
125
+ auto vm_data = vm_ref.GetVMData ();
126
+ auto thread_task_runner = GetThreadTaskRunner ();
127
+ for (size_t i = 0 ; i < isolate_count; ++i) {
128
+ thread_task_runner->PostTask (
129
+ [vm_data, &settings, &latch, thread_task_runner]() {
130
+ ASSERT_TRUE (CreateAndRunRootIsolate (settings, *vm_data.get (),
131
+ thread_task_runner,
132
+ " testIsolateShutdown" ));
133
+ latch.CountDown ();
134
+ });
135
+ }
136
+
137
+ latch.Wait ();
137
138
}
138
139
ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
139
140
}
0 commit comments