#include "flutter/fml/message_loop.h"
#include <iostream>
#include <thread>
#include "flutter/fml/build_config.h"
#include "flutter/fml/concurrent_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 "flutter/fml/time/chrono_timestamp_provider.h"
#include "gtest/gtest.h"
Go to the source code of this file.
|
| TEST (MessageLoop, GetCurrent) |
|
| TEST (MessageLoop, DifferentThreadsHaveDifferentLoops) |
|
| TEST (MessageLoop, CanRunAndTerminate) |
|
| TEST (MessageLoop, NonDelayedTasksAreRunInOrder) |
|
| TEST (MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) |
|
| TEST (MessageLoop, CheckRunsTaskOnCurrentThread) |
|
| TEST (MessageLoop, TaskObserverFire) |
|
| TEST (MessageLoop, ConcurrentMessageLoopHasNonZeroWorkers) |
|
| TEST (MessageLoop, CanCreateAndShutdownConcurrentMessageLoopsOverAndOver) |
|
| TEST (MessageLoop, CanCreateConcurrentMessageLoop) |
|
◆ FML_USED_ON_EMBEDDER
#define FML_USED_ON_EMBEDDER |
◆ TEST() [1/10]
TEST |
( |
MessageLoop |
, |
|
|
CanCreateAndShutdownConcurrentMessageLoopsOverAndOver |
|
|
) |
| |
Definition at line 187 of file message_loop_unittests.cc.
187 {
188 for (
size_t i = 0;
i < 10; ++
i) {
190 ASSERT_EQ(loop->GetWorkerCount(),
i + 1);
191 }
192}
static std::shared_ptr< ConcurrentMessageLoop > Create(size_t worker_count=std::thread::hardware_concurrency())
◆ TEST() [2/10]
TEST |
( |
MessageLoop |
, |
|
|
CanCreateConcurrentMessageLoop |
|
|
) |
| |
Definition at line 194 of file message_loop_unittests.cc.
194 {
196 auto task_runner = loop->GetTaskRunner();
197 const size_t kCount = 10;
199 std::mutex thread_ids_mutex;
200 std::set<std::thread::id> thread_ids;
201 for (
size_t i = 0;
i < kCount; ++
i) {
202 task_runner->PostTask([&]() {
203 std::this_thread::sleep_for(std::chrono::seconds(1));
204 std::cout << "Ran on thread: " << std::this_thread::get_id() << std::endl;
205 {
206 std::scoped_lock lock(thread_ids_mutex);
207 thread_ids.insert(std::this_thread::get_id());
208 }
209 latch.CountDown();
210 });
211 }
212 latch.Wait();
213 ASSERT_GE(thread_ids.size(), 1u);
214}
◆ TEST() [3/10]
TEST |
( |
MessageLoop |
, |
|
|
CanRunAndTerminate |
|
|
) |
| |
Definition at line 57 of file message_loop_unittests.cc.
57 {
58 bool started = false;
59 bool terminated = false;
60 std::thread thread([&started, &terminated]() {
63 ASSERT_TRUE(loop.GetTaskRunner());
64 loop.GetTaskRunner()->PostTask([&terminated]() {
66 terminated = true;
67 });
68 loop.Run();
69 started = true;
70 });
71 thread.join();
72 ASSERT_TRUE(started);
73 ASSERT_TRUE(terminated);
74}
static void EnsureInitializedForCurrentThread()
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
◆ TEST() [4/10]
TEST |
( |
MessageLoop |
, |
|
|
CheckRunsTaskOnCurrentThread |
|
|
) |
| |
Definition at line 134 of file message_loop_unittests.cc.
134 {
137 std::thread thread([&runner, &latch]() {
140 runner = loop.GetTaskRunner();
142 ASSERT_TRUE(loop.GetTaskRunner()->RunsTasksOnCurrentThread());
143 });
145 ASSERT_TRUE(runner);
147 thread.join();
148}
virtual bool RunsTasksOnCurrentThread()
◆ TEST() [5/10]
TEST |
( |
MessageLoop |
, |
|
|
ConcurrentMessageLoopHasNonZeroWorkers |
|
|
) |
| |
◆ TEST() [6/10]
TEST |
( |
MessageLoop |
, |
|
|
DelayedTasksAtSameTimeAreRunInOrder |
|
|
) |
| |
Definition at line 103 of file message_loop_unittests.cc.
103 {
104 const size_t count = 100;
105 bool started = false;
106 bool terminated = false;
107 std::thread thread([&started, &terminated,
count]() {
110 size_t current = 0;
111 const auto now_plus_some =
113 for (
size_t i = 0;
i <
count;
i++) {
114 loop.GetTaskRunner()->PostTaskForTime(
115 [&terminated,
i, ¤t]() {
116 ASSERT_EQ(current,
i);
117 current++;
120 terminated = true;
121 }
122 },
123 now_plus_some);
124 }
125 loop.Run();
126 ASSERT_EQ(current,
count);
127 started = true;
128 });
129 thread.join();
130 ASSERT_TRUE(started);
131 ASSERT_TRUE(terminated);
132}
static constexpr TimeDelta FromMilliseconds(int64_t millis)
fml::TimePoint ChronoTicksSinceEpoch()
◆ TEST() [7/10]
TEST |
( |
MessageLoop |
, |
|
|
DifferentThreadsHaveDifferentLoops |
|
|
) |
| |
Definition at line 28 of file message_loop_unittests.cc.
28 {
32 std::thread thread1([&
loop1, &latch1, &term1]() {
37 });
38
42 std::thread thread2([&
loop2, &latch2, &term2]() {
47 });
53 thread1.join();
54 thread2.join();
55}
static void loop1(skiatest::Reporter *reporter, const char *filename)
static void loop2(skiatest::Reporter *reporter, const char *filename)
◆ TEST() [8/10]
TEST |
( |
MessageLoop |
, |
|
|
GetCurrent |
|
|
) |
| |
◆ TEST() [9/10]
TEST |
( |
MessageLoop |
, |
|
|
NonDelayedTasksAreRunInOrder |
|
|
) |
| |
Definition at line 76 of file message_loop_unittests.cc.
76 {
77 const size_t count = 100;
78 bool started = false;
79 bool terminated = false;
80 std::thread thread([&started, &terminated,
count]() {
83 size_t current = 0;
85 loop.GetTaskRunner()->PostTask([&terminated,
i, ¤t]() {
86 ASSERT_EQ(current,
i);
87 current++;
90 terminated = true;
91 }
92 });
93 }
94 loop.Run();
95 ASSERT_EQ(current,
count);
96 started = true;
97 });
98 thread.join();
99 ASSERT_TRUE(started);
100 ASSERT_TRUE(terminated);
101}
◆ TEST() [10/10]
TEST |
( |
MessageLoop |
, |
|
|
TaskObserverFire |
|
|
) |
| |
Definition at line 150 of file message_loop_unittests.cc.
150 {
151 bool started = false;
152 bool terminated = false;
153 std::thread thread([&started, &terminated]() {
155 const size_t count = 25;
157 size_t task_count = 0;
158 size_t obs_count = 0;
159 auto obs = [&obs_count]() { obs_count++; };
160 for (
size_t i = 0;
i <
count;
i++) {
161 loop.GetTaskRunner()->PostTask([&terminated,
i, &task_count]() {
162 ASSERT_EQ(task_count,
i);
163 task_count++;
166 terminated = true;
167 }
168 });
169 }
170 loop.AddTaskObserver(0, obs);
171 loop.Run();
172 ASSERT_EQ(task_count,
count);
173 ASSERT_EQ(obs_count,
count);
174 started = true;
175 });
176 thread.join();
177 ASSERT_TRUE(started);
178 ASSERT_TRUE(terminated);
179}