1 // Copyright 2014 The Chromium 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.
5 #include "content/renderer/scheduler/renderer_task_queue_selector.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/pending_task.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class RendererTaskQueueSelectorTest
: public testing::Test
{
18 RendererTaskQueueSelectorTest()
20 base::Bind(&RendererTaskQueueSelectorTest::TestFunction
)) {}
21 ~RendererTaskQueueSelectorTest() override
{}
23 std::vector
<base::PendingTask
> GetTasks(int count
) {
24 std::vector
<base::PendingTask
> tasks
;
25 for (int i
= 0; i
< count
; i
++) {
26 base::PendingTask task
= base::PendingTask(FROM_HERE
, test_closure_
);
27 task
.sequence_num
= i
;
28 tasks
.push_back(task
);
33 void PushTasks(const std::vector
<base::PendingTask
>& tasks
,
34 const size_t queue_indices
[]) {
35 for (size_t i
= 0; i
< tasks
.size(); i
++) {
36 task_queues_
[queue_indices
[i
]]->push(tasks
[i
]);
40 std::vector
<size_t> PopTasks() {
41 std::vector
<size_t> order
;
42 size_t chosen_queue_index
;
43 while (selector_
.SelectWorkQueueToService(&chosen_queue_index
)) {
44 order
.push_back(chosen_queue_index
);
45 task_queues_
[chosen_queue_index
]->pop();
50 static void TestFunction() {}
54 std::vector
<const base::TaskQueue
*> const_task_queues
;
55 for (size_t i
= 0; i
< kTaskQueueCount
; i
++) {
56 scoped_ptr
<base::TaskQueue
> task_queue(new base::TaskQueue());
57 const_task_queues
.push_back(task_queue
.get());
58 task_queues_
.push_back(task_queue
.release());
60 selector_
.RegisterWorkQueues(const_task_queues
);
61 for (size_t i
= 0; i
< kTaskQueueCount
; i
++)
62 EXPECT_TRUE(selector_
.IsQueueEnabled(i
)) << i
;
65 const size_t kTaskQueueCount
= 5;
66 base::Closure test_closure_
;
67 RendererTaskQueueSelector selector_
;
68 ScopedVector
<base::TaskQueue
> task_queues_
;
71 TEST_F(RendererTaskQueueSelectorTest
, TestDefaultPriority
) {
72 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
73 size_t queue_order
[] = {4, 3, 2, 1, 0};
74 PushTasks(tasks
, queue_order
);
75 EXPECT_THAT(PopTasks(), testing::ElementsAre(4, 3, 2, 1, 0));
78 TEST_F(RendererTaskQueueSelectorTest
, TestHighPriority
) {
79 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
80 size_t queue_order
[] = {0, 1, 2, 3, 4};
81 PushTasks(tasks
, queue_order
);
82 selector_
.SetQueuePriority(2, RendererTaskQueueSelector::HIGH_PRIORITY
);
83 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 0, 1, 3, 4));
86 TEST_F(RendererTaskQueueSelectorTest
, TestBestEffortPriority
) {
87 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
88 size_t queue_order
[] = {0, 1, 2, 3, 4};
89 PushTasks(tasks
, queue_order
);
90 selector_
.SetQueuePriority(0,
91 RendererTaskQueueSelector::BEST_EFFORT_PRIORITY
);
92 selector_
.SetQueuePriority(2, RendererTaskQueueSelector::HIGH_PRIORITY
);
93 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 1, 3, 4, 0));
96 TEST_F(RendererTaskQueueSelectorTest
, TestControlPriority
) {
97 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
98 size_t queue_order
[] = {0, 1, 2, 3, 4};
99 PushTasks(tasks
, queue_order
);
100 selector_
.SetQueuePriority(4, RendererTaskQueueSelector::CONTROL_PRIORITY
);
101 EXPECT_TRUE(selector_
.IsQueueEnabled(4));
102 selector_
.SetQueuePriority(2, RendererTaskQueueSelector::HIGH_PRIORITY
);
103 EXPECT_TRUE(selector_
.IsQueueEnabled(2));
104 EXPECT_THAT(PopTasks(), testing::ElementsAre(4, 2, 0, 1, 3));
107 TEST_F(RendererTaskQueueSelectorTest
, TestDisableEnable
) {
108 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
109 size_t queue_order
[] = {0, 1, 2, 3, 4};
110 PushTasks(tasks
, queue_order
);
111 selector_
.DisableQueue(2);
112 EXPECT_FALSE(selector_
.IsQueueEnabled(2));
113 selector_
.DisableQueue(4);
114 EXPECT_FALSE(selector_
.IsQueueEnabled(4));
115 EXPECT_THAT(PopTasks(), testing::ElementsAre(0, 1, 3));
116 selector_
.EnableQueue(2, RendererTaskQueueSelector::BEST_EFFORT_PRIORITY
);
117 EXPECT_THAT(PopTasks(), testing::ElementsAre(2));
118 selector_
.EnableQueue(4, RendererTaskQueueSelector::NORMAL_PRIORITY
);
119 EXPECT_THAT(PopTasks(), testing::ElementsAre(4));
122 TEST_F(RendererTaskQueueSelectorTest
, TestEmptyQueues
) {
123 size_t chosen_queue_index
= 0;
124 EXPECT_FALSE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
126 // Test only disabled queues.
127 std::vector
<base::PendingTask
> tasks
= GetTasks(1);
128 size_t queue_order
[] = {0};
129 PushTasks(tasks
, queue_order
);
130 selector_
.DisableQueue(0);
131 EXPECT_FALSE(selector_
.IsQueueEnabled(0));
132 EXPECT_FALSE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
135 TEST_F(RendererTaskQueueSelectorTest
, TestDelay
) {
136 std::vector
<base::PendingTask
> tasks
= GetTasks(5);
137 tasks
[0].delayed_run_time
=
138 base::TimeTicks() + base::TimeDelta::FromMilliseconds(200);
139 tasks
[3].delayed_run_time
=
140 base::TimeTicks() + base::TimeDelta::FromMilliseconds(100);
141 size_t queue_order
[] = {0, 1, 2, 3, 4};
142 PushTasks(tasks
, queue_order
);
143 EXPECT_THAT(PopTasks(), testing::ElementsAre(1, 2, 4, 3, 0));
146 TEST_F(RendererTaskQueueSelectorTest
, TestControlStarvesOthers
) {
147 std::vector
<base::PendingTask
> tasks
= GetTasks(4);
148 size_t queue_order
[] = {0, 1, 2, 3};
149 PushTasks(tasks
, queue_order
);
150 selector_
.SetQueuePriority(3, RendererTaskQueueSelector::CONTROL_PRIORITY
);
151 selector_
.SetQueuePriority(2, RendererTaskQueueSelector::HIGH_PRIORITY
);
152 selector_
.SetQueuePriority(1,
153 RendererTaskQueueSelector::BEST_EFFORT_PRIORITY
);
154 for (int i
= 0; i
< 100; i
++) {
155 size_t chosen_queue_index
= 0;
156 EXPECT_TRUE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
157 EXPECT_EQ(3ul, chosen_queue_index
);
158 // Don't remove task from queue to simulate all queues still being full.
162 TEST_F(RendererTaskQueueSelectorTest
, TestHighPriorityDoesNotStarveNormal
) {
163 std::vector
<base::PendingTask
> tasks
= GetTasks(3);
164 size_t queue_order
[] = {0, 1, 2};
165 PushTasks(tasks
, queue_order
);
166 selector_
.SetQueuePriority(2, RendererTaskQueueSelector::HIGH_PRIORITY
);
167 selector_
.SetQueuePriority(1,
168 RendererTaskQueueSelector::BEST_EFFORT_PRIORITY
);
169 size_t counts
[] = {0, 0, 0};
170 for (int i
= 0; i
< 100; i
++) {
171 size_t chosen_queue_index
= 0;
172 EXPECT_TRUE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
173 counts
[chosen_queue_index
]++;
174 // Don't remove task from queue to simulate all queues still being full.
176 EXPECT_GT(counts
[0], 0ul); // Check high doesn't starve normal.
177 EXPECT_GT(counts
[2], counts
[0]); // Check high gets more chance to run.
178 EXPECT_EQ(0ul, counts
[1]); // Check best effort is starved.
181 TEST_F(RendererTaskQueueSelectorTest
, TestBestEffortGetsStarved
) {
182 std::vector
<base::PendingTask
> tasks
= GetTasks(2);
183 size_t queue_order
[] = {0, 1};
184 PushTasks(tasks
, queue_order
);
185 selector_
.SetQueuePriority(0,
186 RendererTaskQueueSelector::BEST_EFFORT_PRIORITY
);
187 selector_
.SetQueuePriority(1, RendererTaskQueueSelector::NORMAL_PRIORITY
);
188 size_t chosen_queue_index
= 0;
189 for (int i
= 0; i
< 100; i
++) {
190 EXPECT_TRUE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
191 EXPECT_EQ(1ul, chosen_queue_index
);
192 // Don't remove task from queue to simulate all queues still being full.
194 selector_
.SetQueuePriority(1, RendererTaskQueueSelector::HIGH_PRIORITY
);
195 for (int i
= 0; i
< 100; i
++) {
196 EXPECT_TRUE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
197 EXPECT_EQ(1ul, chosen_queue_index
);
198 // Don't remove task from queue to simulate all queues still being full.
200 selector_
.SetQueuePriority(1, RendererTaskQueueSelector::CONTROL_PRIORITY
);
201 for (int i
= 0; i
< 100; i
++) {
202 EXPECT_TRUE(selector_
.SelectWorkQueueToService(&chosen_queue_index
));
203 EXPECT_EQ(1ul, chosen_queue_index
);
204 // Don't remove task from queue to simulate all queues still being full.
208 } // namespace content