Flutter Engine
task_runner_checker_unittest.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 #define FML_USED_ON_EMBEDDER
6 
7 #include "flutter/fml/memory/task_runner_checker.h"
8 
9 #include <thread>
10 
11 #include "flutter/fml/message_loop.h"
12 #include "flutter/fml/raster_thread_merger.h"
13 #include "flutter/fml/synchronization/count_down_latch.h"
14 #include "flutter/fml/synchronization/waitable_event.h"
15 #include "gtest/gtest.h"
16 
17 namespace fml {
18 namespace testing {
19 
20 TEST(TaskRunnerCheckerTests, RunsOnCurrentTaskRunner) {
21  TaskRunnerChecker checker;
22  EXPECT_EQ(checker.RunsOnCreationTaskRunner(), true);
23 }
24 
25 TEST(TaskRunnerCheckerTests, FailsTheCheckIfOnDifferentTaskRunner) {
26  TaskRunnerChecker checker;
27  EXPECT_EQ(checker.RunsOnCreationTaskRunner(), true);
28  fml::MessageLoop* loop = nullptr;
30  std::thread anotherThread([&]() {
33  loop->GetTaskRunner()->PostTask([&]() {
34  EXPECT_EQ(checker.RunsOnCreationTaskRunner(), false);
35  latch.Signal();
36  });
37  loop->Run();
38  });
39  latch.Wait();
40  loop->Terminate();
41  anotherThread.join();
42  EXPECT_EQ(checker.RunsOnCreationTaskRunner(), true);
43 }
44 
45 TEST(TaskRunnerCheckerTests, SameTaskRunnerRunsOnTheSameThread) {
50  EXPECT_EQ(TaskRunnerChecker::RunsOnTheSameThread(a, b), true);
51 }
52 
53 TEST(TaskRunnerCheckerTests, RunsOnDifferentThreadsReturnsFalse) {
57  std::thread anotherThread([&]() {
61  EXPECT_EQ(TaskRunnerChecker::RunsOnTheSameThread(a, b), false);
62  latch.Signal();
63  });
64  latch.Wait();
65  anotherThread.join();
66 }
67 
68 TEST(TaskRunnerCheckerTests, MergedTaskRunnersRunsOnTheSameThread) {
69  fml::MessageLoop* loop1 = nullptr;
72  std::thread thread1([&loop1, &latch1, &term1]() {
75  latch1.Signal();
76  term1.Wait();
77  });
78 
79  fml::MessageLoop* loop2 = nullptr;
82  std::thread thread2([&loop2, &latch2, &term2]() {
85  latch2.Signal();
86  term2.Wait();
87  });
88 
89  latch1.Wait();
90  latch2.Wait();
92  fml::TaskQueueId qid2 = loop2->GetTaskRunner()->GetTaskQueueId();
93  const auto raster_thread_merger_ =
94  fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
95  const int kNumFramesMerged = 5;
96 
97  raster_thread_merger_->MergeWithLease(kNumFramesMerged);
98 
99  // merged, running on the same thread
100  EXPECT_EQ(TaskRunnerChecker::RunsOnTheSameThread(qid1, qid2), true);
101 
102  for (int i = 0; i < kNumFramesMerged; i++) {
103  ASSERT_TRUE(raster_thread_merger_->IsMerged());
104  raster_thread_merger_->DecrementLease();
105  }
106 
107  ASSERT_FALSE(raster_thread_merger_->IsMerged());
108 
109  // un-merged, not running on the same thread
110  EXPECT_EQ(TaskRunnerChecker::RunsOnTheSameThread(qid1, qid2), false);
111 
112  term1.Signal();
113  term2.Signal();
114  thread1.join();
115  thread2.join();
116 }
117 
118 } // namespace testing
119 } // namespace fml
virtual TaskQueueId GetTaskQueueId()
Definition: task_runner.cc:38
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
Definition: ascii_trie.cc:9
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: message_loop.cc:56
TEST(BacktraceTest, CanGatherBacktrace)
bool RunsOnCreationTaskRunner() const
static bool RunsOnTheSameThread(TaskQueueId queue_a, TaskQueueId queue_b)