Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Functions
message_loop_unittests.cc File Reference
#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.

Macros

#define FML_USED_ON_EMBEDDER
 

Functions

 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)
 

Macro Definition Documentation

◆ FML_USED_ON_EMBEDDER

#define FML_USED_ON_EMBEDDER

Definition at line 5 of file message_loop_unittests.cc.

Function Documentation

◆ 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) {
189 auto loop = fml::ConcurrentMessageLoop::Create(i + 1);
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;
198 fml::CountDownLatch latch(kCount);
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]() {
62 auto& loop = fml::MessageLoop::GetCurrent();
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]() {
139 auto& loop = fml::MessageLoop::GetCurrent();
140 runner = loop.GetTaskRunner();
141 latch.Signal();
142 ASSERT_TRUE(loop.GetTaskRunner()->RunsTasksOnCurrentThread());
143 });
144 latch.Wait();
145 ASSERT_TRUE(runner);
146 ASSERT_FALSE(runner->RunsTasksOnCurrentThread());
147 thread.join();
148}

◆ TEST() [5/10]

TEST ( MessageLoop  ,
ConcurrentMessageLoopHasNonZeroWorkers   
)

Definition at line 181 of file message_loop_unittests.cc.

181 {
183 0u /* explicitly specify zero workers */);
184 ASSERT_GT(loop->GetWorkerCount(), 0u);
185}

◆ 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]() {
109 auto& loop = fml::MessageLoop::GetCurrent();
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, &current]() {
116 ASSERT_EQ(current, i);
117 current++;
118 if (count == i + 1) {
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}
int count
static constexpr TimeDelta FromMilliseconds(int64_t millis)
Definition time_delta.h:46
fml::TimePoint ChronoTicksSinceEpoch()

◆ TEST() [7/10]

TEST ( MessageLoop  ,
DifferentThreadsHaveDifferentLoops   
)

Definition at line 28 of file message_loop_unittests.cc.

28 {
29 fml::MessageLoop* loop1 = nullptr;
32 std::thread thread1([&loop1, &latch1, &term1]() {
35 latch1.Signal();
36 term1.Wait();
37 });
38
39 fml::MessageLoop* loop2 = nullptr;
42 std::thread thread2([&loop2, &latch2, &term2]() {
45 latch2.Signal();
46 term2.Wait();
47 });
48 latch1.Wait();
49 latch2.Wait();
50 ASSERT_FALSE(loop1 == loop2);
51 term1.Signal();
52 term2.Signal();
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   
)

Definition at line 20 of file message_loop_unittests.cc.

20 {
21 std::thread thread([]() {
23 ASSERT_TRUE(fml::MessageLoop::GetCurrent().GetTaskRunner());
24 });
25 thread.join();
26}

◆ 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]() {
82 auto& loop = fml::MessageLoop::GetCurrent();
83 size_t current = 0;
84 for (size_t i = 0; i < count; i++) {
85 loop.GetTaskRunner()->PostTask([&terminated, i, &current]() {
86 ASSERT_EQ(current, i);
87 current++;
88 if (count == i + 1) {
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;
156 auto& loop = fml::MessageLoop::GetCurrent();
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++;
164 if (count == i + 1) {
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}