Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / content / child / scheduler / prioritizing_task_queue_selector_unittest.cc
blobdb1d5037720cd064e69218b9b9d5fb159518049f
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/child/scheduler/prioritizing_task_queue_selector.h"
7 #include "base/bind.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"
14 namespace content {
16 class PrioritizingTaskQueueSelectorTest : public testing::Test {
17 public:
18 PrioritizingTaskQueueSelectorTest()
19 : test_closure_(
20 base::Bind(&PrioritizingTaskQueueSelectorTest::TestFunction)) {}
21 ~PrioritizingTaskQueueSelectorTest() 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);
30 return tasks;
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();
47 return order;
50 static void TestFunction() {}
52 protected:
53 void SetUp() final {
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 PrioritizingTaskQueueSelector selector_;
68 ScopedVector<base::TaskQueue> task_queues_;
71 TEST_F(PrioritizingTaskQueueSelectorTest, 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(PrioritizingTaskQueueSelectorTest, 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, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
83 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 0, 1, 3, 4));
86 TEST_F(PrioritizingTaskQueueSelectorTest, 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(
91 0, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
92 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
93 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 1, 3, 4, 0));
96 TEST_F(PrioritizingTaskQueueSelectorTest, 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,
101 PrioritizingTaskQueueSelector::CONTROL_PRIORITY);
102 EXPECT_TRUE(selector_.IsQueueEnabled(4));
103 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
104 EXPECT_TRUE(selector_.IsQueueEnabled(2));
105 EXPECT_THAT(PopTasks(), testing::ElementsAre(4, 2, 0, 1, 3));
108 TEST_F(PrioritizingTaskQueueSelectorTest, TestDisableEnable) {
109 std::vector<base::PendingTask> tasks = GetTasks(5);
110 size_t queue_order[] = {0, 1, 2, 3, 4};
111 PushTasks(tasks, queue_order);
112 selector_.DisableQueue(2);
113 EXPECT_FALSE(selector_.IsQueueEnabled(2));
114 selector_.DisableQueue(4);
115 EXPECT_FALSE(selector_.IsQueueEnabled(4));
116 EXPECT_THAT(PopTasks(), testing::ElementsAre(0, 1, 3));
117 selector_.EnableQueue(2, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
118 EXPECT_THAT(PopTasks(), testing::ElementsAre(2));
119 selector_.EnableQueue(4, PrioritizingTaskQueueSelector::NORMAL_PRIORITY);
120 EXPECT_THAT(PopTasks(), testing::ElementsAre(4));
123 TEST_F(PrioritizingTaskQueueSelectorTest, TestEmptyQueues) {
124 size_t chosen_queue_index = 0;
125 EXPECT_FALSE(selector_.SelectWorkQueueToService(&chosen_queue_index));
127 // Test only disabled queues.
128 std::vector<base::PendingTask> tasks = GetTasks(1);
129 size_t queue_order[] = {0};
130 PushTasks(tasks, queue_order);
131 selector_.DisableQueue(0);
132 EXPECT_FALSE(selector_.IsQueueEnabled(0));
133 EXPECT_FALSE(selector_.SelectWorkQueueToService(&chosen_queue_index));
136 TEST_F(PrioritizingTaskQueueSelectorTest, TestDelay) {
137 std::vector<base::PendingTask> tasks = GetTasks(5);
138 tasks[0].delayed_run_time =
139 base::TimeTicks() + base::TimeDelta::FromMilliseconds(200);
140 tasks[3].delayed_run_time =
141 base::TimeTicks() + base::TimeDelta::FromMilliseconds(100);
142 size_t queue_order[] = {0, 1, 2, 3, 4};
143 PushTasks(tasks, queue_order);
144 EXPECT_THAT(PopTasks(), testing::ElementsAre(1, 2, 4, 3, 0));
147 TEST_F(PrioritizingTaskQueueSelectorTest, TestControlStarvesOthers) {
148 std::vector<base::PendingTask> tasks = GetTasks(4);
149 size_t queue_order[] = {0, 1, 2, 3};
150 PushTasks(tasks, queue_order);
151 selector_.SetQueuePriority(3,
152 PrioritizingTaskQueueSelector::CONTROL_PRIORITY);
153 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
154 selector_.SetQueuePriority(
155 1, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
156 for (int i = 0; i < 100; i++) {
157 size_t chosen_queue_index = 0;
158 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index));
159 EXPECT_EQ(3ul, chosen_queue_index);
160 // Don't remove task from queue to simulate all queues still being full.
164 TEST_F(PrioritizingTaskQueueSelectorTest, TestHighPriorityDoesNotStarveNormal) {
165 std::vector<base::PendingTask> tasks = GetTasks(3);
166 size_t queue_order[] = {0, 1, 2};
167 PushTasks(tasks, queue_order);
168 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
169 selector_.SetQueuePriority(
170 1, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
171 size_t counts[] = {0, 0, 0};
172 for (int i = 0; i < 100; i++) {
173 size_t chosen_queue_index = 0;
174 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index));
175 counts[chosen_queue_index]++;
176 // Don't remove task from queue to simulate all queues still being full.
178 EXPECT_GT(counts[0], 0ul); // Check high doesn't starve normal.
179 EXPECT_GT(counts[2], counts[0]); // Check high gets more chance to run.
180 EXPECT_EQ(0ul, counts[1]); // Check best effort is starved.
183 TEST_F(PrioritizingTaskQueueSelectorTest, TestBestEffortGetsStarved) {
184 std::vector<base::PendingTask> tasks = GetTasks(2);
185 size_t queue_order[] = {0, 1};
186 PushTasks(tasks, queue_order);
187 selector_.SetQueuePriority(
188 0, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
189 selector_.SetQueuePriority(1, PrioritizingTaskQueueSelector::NORMAL_PRIORITY);
190 size_t chosen_queue_index = 0;
191 for (int i = 0; i < 100; i++) {
192 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index));
193 EXPECT_EQ(1ul, chosen_queue_index);
194 // Don't remove task from queue to simulate all queues still being full.
196 selector_.SetQueuePriority(1, PrioritizingTaskQueueSelector::HIGH_PRIORITY);
197 for (int i = 0; i < 100; i++) {
198 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index));
199 EXPECT_EQ(1ul, chosen_queue_index);
200 // Don't remove task from queue to simulate all queues still being full.
202 selector_.SetQueuePriority(1,
203 PrioritizingTaskQueueSelector::CONTROL_PRIORITY);
204 for (int i = 0; i < 100; i++) {
205 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index));
206 EXPECT_EQ(1ul, chosen_queue_index);
207 // Don't remove task from queue to simulate all queues still being full.
211 } // namespace content