Flutter Engine
The Flutter Engine
task_source_unittests.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#include <atomic>
6#include <thread>
7
8#include "flutter/fml/macros.h"
9#include "flutter/fml/task_source.h"
10#include "flutter/fml/time/chrono_timestamp_provider.h"
11#include "flutter/fml/time/time_delta.h"
12#include "flutter/fml/time/time_point.h"
13#include "gtest/gtest.h"
14
15namespace fml {
16namespace testing {
17
18TEST(TaskSourceTests, SimpleInitialization) {
19 TaskSource task_source = TaskSource(TaskQueueId(1));
20 task_source.RegisterTask(
22 ASSERT_EQ(task_source.GetNumPendingTasks(), 1u);
23}
24
25TEST(TaskSourceTests, MultipleTaskGrades) {
26 TaskSource task_source = TaskSource(TaskQueueId(1));
27 task_source.RegisterTask(
29 task_source.RegisterTask(
31 task_source.RegisterTask(
33 ASSERT_EQ(task_source.GetNumPendingTasks(), 3u);
34}
35
36TEST(TaskSourceTests, SimpleOrdering) {
37 TaskSource task_source = TaskSource(TaskQueueId(1));
38 auto time_stamp = ChronoTicksSinceEpoch();
39 int value = 0;
40 task_source.RegisterTask(
41 {1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kUnspecified});
42 task_source.RegisterTask({2, [&] { value = 7; },
45 task_source.Top().task.GetTask()();
47 ASSERT_EQ(value, 1);
48 task_source.Top().task.GetTask()();
50 ASSERT_EQ(value, 7);
51}
52
53TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {
54 TaskSource task_source = TaskSource(TaskQueueId(1));
55 auto time_stamp = ChronoTicksSinceEpoch();
56 int value = 0;
57 task_source.RegisterTask(
58 {1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartEventLoop});
59 task_source.RegisterTask({2, [&] { value = 7; },
62 auto top_task = task_source.Top();
63 top_task.task.GetTask()();
64 task_source.PopTask(top_task.task.GetTaskSourceGrade());
65 ASSERT_EQ(value, 1);
66
67 auto second_task = task_source.Top();
68 second_task.task.GetTask()();
69 task_source.PopTask(second_task.task.GetTaskSourceGrade());
70 ASSERT_EQ(value, 7);
71}
72
73TEST(TaskSourceTests, OrderingMultiTaskHeapsSecondaryPaused) {
74 TaskSource task_source = TaskSource(TaskQueueId(1));
75 auto time_stamp = ChronoTicksSinceEpoch();
76 int value = 0;
77 task_source.RegisterTask(
78 {1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartEventLoop});
79 task_source.RegisterTask({2, [&] { value = 7; },
82
83 task_source.PauseSecondary();
84
85 auto top_task = task_source.Top();
86 top_task.task.GetTask()();
87 task_source.PopTask(top_task.task.GetTaskSourceGrade());
88 ASSERT_EQ(value, 7);
89
90 ASSERT_TRUE(task_source.IsEmpty());
91
92 task_source.ResumeSecondary();
93
94 auto second_task = task_source.Top();
95 second_task.task.GetTask()();
96 task_source.PopTask(second_task.task.GetTaskSourceGrade());
97 ASSERT_EQ(value, 1);
98}
99
100} // namespace testing
101} // namespace fml
const fml::closure & GetTask() const
Definition: delayed_task.cc:24
bool IsEmpty() const
Returns true if GetNumPendingTasks is zero.
Definition: task_source.cc:59
void RegisterTask(const DelayedTask &task)
Definition: task_source.cc:23
void ResumeSecondary()
Resume providing tasks from secondary task heap.
Definition: task_source.cc:98
void PauseSecondary()
Pause providing tasks from secondary task heap.
Definition: task_source.cc:94
void PopTask(TaskSourceGrade grade)
Pops the task heap corresponding to the TaskSourceGrade.
Definition: task_source.cc:37
size_t GetNumPendingTasks() const
Definition: task_source.cc:51
TopTask Top() const
Definition: task_source.cc:63
static constexpr TimeDelta FromMilliseconds(int64_t millis)
Definition: time_delta.h:46
uint8_t value
TEST(BacktraceTest, CanGatherBacktrace)
Definition: ascii_trie.cc:9
fml::TimePoint ChronoTicksSinceEpoch()
@ kUnspecified
The absence of a specialized TaskSourceGrade.
const DelayedTask & task
Definition: task_source.h:39