forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_loop_unittests.cc
433 lines (398 loc) · 13.2 KB
/
message_loop_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define FML_USED_ON_EMBEDDER
#include <iostream>
#include <thread>
#include "flutter/fml/message_loop.h"
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/task_runner.h"
#include "gtest/gtest.h"
#define TIME_SENSITIVE(x) TimeSensitiveTest_##x
#if OS_WIN
#define PLATFORM_SPECIFIC_CAPTURE(...) [ __VA_ARGS__, count ]
#else
#define PLATFORM_SPECIFIC_CAPTURE(...) [__VA_ARGS__]
#endif
TEST(MessageLoop, GetCurrent) {
std::thread thread([]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
ASSERT_TRUE(fml::MessageLoop::GetCurrent().GetTaskRunner());
});
thread.join();
}
TEST(MessageLoop, DifferentThreadsHaveDifferentLoops) {
fml::MessageLoop* loop1 = nullptr;
fml::AutoResetWaitableEvent latch1;
fml::AutoResetWaitableEvent term1;
std::thread thread1([&loop1, &latch1, &term1]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop1 = &fml::MessageLoop::GetCurrent();
latch1.Signal();
term1.Wait();
});
fml::MessageLoop* loop2 = nullptr;
fml::AutoResetWaitableEvent latch2;
fml::AutoResetWaitableEvent term2;
std::thread thread2([&loop2, &latch2, &term2]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop2 = &fml::MessageLoop::GetCurrent();
latch2.Signal();
term2.Wait();
});
latch1.Wait();
latch2.Wait();
ASSERT_FALSE(loop1 == loop2);
term1.Signal();
term2.Signal();
thread1.join();
thread2.join();
}
TEST(MessageLoop, CanRunAndTerminate) {
bool started = false;
bool terminated = false;
std::thread thread([&started, &terminated]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
ASSERT_TRUE(loop.GetTaskRunner());
loop.GetTaskRunner()->PostTask([&terminated]() {
fml::MessageLoop::GetCurrent().Terminate();
terminated = true;
});
loop.Run();
started = true;
});
thread.join();
ASSERT_TRUE(started);
ASSERT_TRUE(terminated);
}
TEST(MessageLoop, NonDelayedTasksAreRunInOrder) {
const size_t count = 100;
bool started = false;
bool terminated = false;
std::thread thread([&started, &terminated, count]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
size_t current = 0;
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTask(
PLATFORM_SPECIFIC_CAPTURE(&terminated, i, ¤t)() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
fml::MessageLoop::GetCurrent().Terminate();
terminated = true;
}
});
}
loop.Run();
ASSERT_EQ(current, count);
started = true;
});
thread.join();
ASSERT_TRUE(started);
ASSERT_TRUE(terminated);
}
TEST(MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) {
const size_t count = 100;
bool started = false;
bool terminated = false;
std::thread thread([&started, &terminated, count]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
size_t current = 0;
const auto now_plus_some =
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(2);
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTaskForTime(
PLATFORM_SPECIFIC_CAPTURE(&terminated, i, ¤t)() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
fml::MessageLoop::GetCurrent().Terminate();
terminated = true;
}
},
now_plus_some);
}
loop.Run();
ASSERT_EQ(current, count);
started = true;
});
thread.join();
ASSERT_TRUE(started);
ASSERT_TRUE(terminated);
}
TEST(MessageLoop, CheckRunsTaskOnCurrentThread) {
fml::RefPtr<fml::TaskRunner> runner;
fml::AutoResetWaitableEvent latch;
std::thread thread([&runner, &latch]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
runner = loop.GetTaskRunner();
latch.Signal();
ASSERT_TRUE(loop.GetTaskRunner()->RunsTasksOnCurrentThread());
});
latch.Wait();
ASSERT_TRUE(runner);
ASSERT_FALSE(runner->RunsTasksOnCurrentThread());
thread.join();
}
TEST(MessageLoop, TIME_SENSITIVE(SingleDelayedTaskByDelta)) {
bool checked = false;
std::thread thread([&checked]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
auto begin = fml::TimePoint::Now();
loop.GetTaskRunner()->PostDelayedTask(
[begin, &checked]() {
auto delta = fml::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, 3);
ASSERT_LE(ms, 7);
checked = true;
fml::MessageLoop::GetCurrent().Terminate();
},
fml::TimeDelta::FromMilliseconds(5));
loop.Run();
});
thread.join();
ASSERT_TRUE(checked);
}
TEST(MessageLoop, TIME_SENSITIVE(SingleDelayedTaskForTime)) {
bool checked = false;
std::thread thread([&checked]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
auto begin = fml::TimePoint::Now();
loop.GetTaskRunner()->PostTaskForTime(
[begin, &checked]() {
auto delta = fml::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, 3);
ASSERT_LE(ms, 7);
checked = true;
fml::MessageLoop::GetCurrent().Terminate();
},
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(5));
loop.Run();
});
thread.join();
ASSERT_TRUE(checked);
}
TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) {
const auto count = 10;
int checked = false;
std::thread thread(PLATFORM_SPECIFIC_CAPTURE(&checked)() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = 0 + 2; target_ms < count + 2; target_ms++) {
auto begin = fml::TimePoint::Now();
loop.GetTaskRunner()->PostDelayedTask(
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
auto delta = fml::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
ASSERT_LE(ms, target_ms + 2);
checked++;
if (checked == count) {
fml::MessageLoop::GetCurrent().Terminate();
}
},
fml::TimeDelta::FromMilliseconds(target_ms));
}
loop.Run();
});
thread.join();
ASSERT_EQ(checked, count);
}
TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithDecreasingDeltas)) {
const auto count = 10;
int checked = false;
std::thread thread(PLATFORM_SPECIFIC_CAPTURE(&checked)() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = count + 2; target_ms > 0 + 2; target_ms--) {
auto begin = fml::TimePoint::Now();
loop.GetTaskRunner()->PostDelayedTask(
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
auto delta = fml::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
ASSERT_LE(ms, target_ms + 2);
checked++;
if (checked == count) {
fml::MessageLoop::GetCurrent().Terminate();
}
},
fml::TimeDelta::FromMilliseconds(target_ms));
}
loop.Run();
});
thread.join();
ASSERT_EQ(checked, count);
}
TEST(MessageLoop, TaskObserverFire) {
bool started = false;
bool terminated = false;
std::thread thread([&started, &terminated]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
const size_t count = 25;
auto& loop = fml::MessageLoop::GetCurrent();
size_t task_count = 0;
size_t obs_count = 0;
auto obs = PLATFORM_SPECIFIC_CAPTURE(&obs_count)() { obs_count++; };
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTask(
PLATFORM_SPECIFIC_CAPTURE(&terminated, i, &task_count)() {
ASSERT_EQ(task_count, i);
task_count++;
if (count == i + 1) {
fml::MessageLoop::GetCurrent().Terminate();
terminated = true;
}
});
}
loop.AddTaskObserver(0, obs);
loop.Run();
ASSERT_EQ(task_count, count);
ASSERT_EQ(obs_count, count);
started = true;
});
thread.join();
ASSERT_TRUE(started);
ASSERT_TRUE(terminated);
}
TEST(MessageLoop, CanCreateConcurrentMessageLoop) {
fml::MessageLoop loop(fml::MessageLoop::Type::kConcurrent);
auto task_runner = loop.GetTaskRunner();
const size_t kCount = 10;
fml::CountDownLatch latch(kCount);
for (size_t i = 0; i < kCount; ++i) {
task_runner->PostTask([&latch]() {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
std::cout << "Ran on thread: " << std::this_thread::get_id() << std::endl;
latch.CountDown();
});
}
latch.Wait();
}
TEST(MessageLoop, CanSwapMessageLoopsAndPreserveThreadConfiguration) {
// synchronization notes:
// 1. term1 and term2 are to wait for Swap.
// 2. task_started_1 is to wait for the task runners
// to signal that they are done.
// 3. loop_init_1 and loop_init_2 are to wait for the message loops to
// get initialized.
fml::MessageLoop* loop1 = nullptr;
fml::AutoResetWaitableEvent loop_init_1;
fml::AutoResetWaitableEvent task_started_1;
fml::AutoResetWaitableEvent term1;
std::thread thread1([&loop1, &loop_init_1, &term1, &task_started_1]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop1 = &fml::MessageLoop::GetCurrent();
// this task will be run on thread1 after Swap.
loop1->GetTaskRunner()->PostTask([&task_started_1]() {
task_started_1.Signal();
fml::MessageLoop::GetCurrent().Terminate();
});
loop_init_1.Signal();
term1.Wait();
loop1->Run();
});
loop_init_1.Wait();
fml::MessageLoop* loop2 = nullptr;
fml::AutoResetWaitableEvent loop_init_2;
fml::AutoResetWaitableEvent task_started_2;
fml::AutoResetWaitableEvent term2;
std::thread thread2(
[&loop2, &loop_init_2, &term2, &task_started_2, &loop1]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop2 = &fml::MessageLoop::GetCurrent();
// this task will be run on thread1 after Swap.
loop2->GetTaskRunner()->PostTask([&task_started_2, &loop1]() {
// ensure that we run the task on loop1 after the swap.
ASSERT_TRUE(loop1 == &fml::MessageLoop::GetCurrent());
task_started_2.Signal();
fml::MessageLoop::GetCurrent().Terminate();
});
loop_init_2.Signal();
term2.Wait();
loop2->Run();
});
loop_init_2.Wait();
// swap the loops.
loop1->SwapTaskQueues(loop2);
// thread_1 should wait for tr_term2 latch.
term1.Signal();
task_started_2.Wait();
// thread_2 should wait for tr_term2 latch.
term2.Signal();
task_started_1.Wait();
thread1.join();
thread2.join();
}
TEST(MessageLoop, TIME_SENSITIVE(DelayedTaskSwap)) {
// Task execution order:
// time (ms): 0 10 20 30 40
// thread 1: A1 A2 A3 A4 TERM
// thread 2: B1 B2 B3 TERM
// At time 15, we swap thread 1 and 2, and assert
// that tasks run on the right threads.
std::thread::id t1, t2;
fml::AutoResetWaitableEvent tid_1, tid_2;
fml::MessageLoop* loop1 = nullptr;
fml::MessageLoop* loop2 = nullptr;
std::thread thread_1([&loop1, &t1, &t2, &tid_1, &tid_2]() {
t1 = std::this_thread::get_id();
tid_1.Signal();
tid_2.Wait();
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop1 = &fml::MessageLoop::GetCurrent();
for (int t = 0; t <= 4; t++) {
loop1->GetTaskRunner()->PostDelayedTask(
[t, &t1, &t2]() {
auto cur_tid = std::this_thread::get_id();
if (t <= 1) {
ASSERT_EQ(cur_tid, t1);
} else {
ASSERT_EQ(cur_tid, t2);
}
if (t == 4) {
fml::MessageLoop::GetCurrent().Terminate();
}
},
fml::TimeDelta::FromMilliseconds(t * 10));
}
loop1->Run();
});
std::thread thread_2([&loop2, &t1, &t2, &tid_1, &tid_2]() {
t2 = std::this_thread::get_id();
tid_2.Signal();
tid_1.Wait();
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop2 = &fml::MessageLoop::GetCurrent();
for (int t = 1; t <= 4; t++) {
loop2->GetTaskRunner()->PostDelayedTask(
[t, &t1, &t2]() {
auto cur_tid = std::this_thread::get_id();
if (t <= 1) {
ASSERT_EQ(cur_tid, t2);
} else {
ASSERT_EQ(cur_tid, t1);
}
if (t == 4) {
fml::MessageLoop::GetCurrent().Terminate();
}
},
fml::TimeDelta::FromMilliseconds(t * 10));
}
loop2->Run();
});
// on main thread we swap the threads at 15 ms.
std::this_thread::sleep_for(std::chrono::milliseconds(15));
loop1->SwapTaskQueues(loop2);
thread_1.join();
thread_2.join();
}