forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_source_unittests.cc
101 lines (86 loc) · 3.52 KB
/
task_source_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
// 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.
#include <atomic>
#include <thread>
#include "flutter/fml/macros.h"
#include "flutter/fml/task_source.h"
#include "flutter/fml/time/chrono_timestamp_provider.h"
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/time/time_point.h"
#include "gtest/gtest.h"
namespace fml {
namespace testing {
TEST(TaskSourceTests, SimpleInitialization) {
TaskSource task_source = TaskSource(TaskQueueId(1));
task_source.RegisterTask(
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
ASSERT_EQ(task_source.GetNumPendingTasks(), 1u);
}
TEST(TaskSourceTests, MultipleTaskGrades) {
TaskSource task_source = TaskSource(TaskQueueId(1));
task_source.RegisterTask(
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
task_source.RegisterTask(
{2, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUserInteraction});
task_source.RegisterTask(
{3, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kDartMicroTasks});
ASSERT_EQ(task_source.GetNumPendingTasks(), 3u);
}
TEST(TaskSourceTests, SimpleOrdering) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kUnspecified});
task_source.RegisterTask({2, [&] { value = 7; },
time_stamp + fml::TimeDelta::FromMilliseconds(1),
TaskSourceGrade::kUnspecified});
task_source.Top().task.GetTask()();
task_source.PopTask(TaskSourceGrade::kUnspecified);
ASSERT_EQ(value, 1);
task_source.Top().task.GetTask()();
task_source.PopTask(TaskSourceGrade::kUnspecified);
ASSERT_EQ(value, 7);
}
TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
task_source.RegisterTask({2, [&] { value = 7; },
time_stamp + fml::TimeDelta::FromMilliseconds(1),
TaskSourceGrade::kUserInteraction});
auto top_task = task_source.Top();
top_task.task.GetTask()();
task_source.PopTask(top_task.task.GetTaskSourceGrade());
ASSERT_EQ(value, 1);
auto second_task = task_source.Top();
second_task.task.GetTask()();
task_source.PopTask(second_task.task.GetTaskSourceGrade());
ASSERT_EQ(value, 7);
}
TEST(TaskSourceTests, OrderingMultiTaskHeapsSecondaryPaused) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
task_source.RegisterTask({2, [&] { value = 7; },
time_stamp + fml::TimeDelta::FromMilliseconds(1),
TaskSourceGrade::kUserInteraction});
task_source.PauseSecondary();
auto top_task = task_source.Top();
top_task.task.GetTask()();
task_source.PopTask(top_task.task.GetTaskSourceGrade());
ASSERT_EQ(value, 7);
ASSERT_TRUE(task_source.IsEmpty());
task_source.ResumeSecondary();
auto second_task = task_source.Top();
second_task.task.GetTask()();
task_source.PopTask(second_task.task.GetTaskSourceGrade());
ASSERT_EQ(value, 1);
}
} // namespace testing
} // namespace fml