| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/child/scheduler/prioritizing_task_queue_selector.h" | |
| 6 | |
| 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" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 class MockObserver | |
| 17 : public TaskQueueSelector::Observer { | |
| 18 public: | |
| 19 MockObserver() {} | |
| 20 virtual ~MockObserver() {} | |
| 21 | |
| 22 MOCK_METHOD0(OnTaskQueueEnabled, void()); | |
| 23 | |
| 24 private: | |
| 25 DISALLOW_COPY_AND_ASSIGN(MockObserver); | |
| 26 }; | |
| 27 | |
| 28 class PrioritizingTaskQueueSelectorForTest | |
| 29 : public PrioritizingTaskQueueSelector { | |
| 30 public: | |
| 31 PrioritizingTaskQueueSelectorForTest() {} | |
| 32 ~PrioritizingTaskQueueSelectorForTest() override {} | |
| 33 | |
| 34 using PrioritizingTaskQueueSelector::DisableQueueInternal; | |
| 35 }; | |
| 36 | |
| 37 class PrioritizingTaskQueueSelectorTest : public testing::Test { | |
| 38 public: | |
| 39 PrioritizingTaskQueueSelectorTest() | |
| 40 : test_closure_( | |
| 41 base::Bind(&PrioritizingTaskQueueSelectorTest::TestFunction)) {} | |
| 42 ~PrioritizingTaskQueueSelectorTest() override {} | |
| 43 | |
| 44 std::vector<base::PendingTask> GetTasks(int count) { | |
| 45 std::vector<base::PendingTask> tasks; | |
| 46 for (int i = 0; i < count; i++) { | |
| 47 base::PendingTask task = base::PendingTask(FROM_HERE, test_closure_); | |
| 48 task.sequence_num = i; | |
| 49 tasks.push_back(task); | |
| 50 } | |
| 51 return tasks; | |
| 52 } | |
| 53 | |
| 54 void PushTasks(const std::vector<base::PendingTask>& tasks, | |
| 55 const size_t queue_indices[]) { | |
| 56 for (size_t i = 0; i < tasks.size(); i++) { | |
| 57 task_queues_[queue_indices[i]]->push(tasks[i]); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 std::vector<size_t> PopTasks() { | |
| 62 std::vector<size_t> order; | |
| 63 size_t chosen_queue_index; | |
| 64 while (selector_.SelectWorkQueueToService(&chosen_queue_index)) { | |
| 65 order.push_back(chosen_queue_index); | |
| 66 task_queues_[chosen_queue_index]->pop(); | |
| 67 } | |
| 68 return order; | |
| 69 } | |
| 70 | |
| 71 static void TestFunction() {} | |
| 72 | |
| 73 protected: | |
| 74 void SetUp() final { | |
| 75 std::vector<const base::TaskQueue*> const_task_queues; | |
| 76 for (size_t i = 0; i < kTaskQueueCount; i++) { | |
| 77 scoped_ptr<base::TaskQueue> task_queue(new base::TaskQueue()); | |
| 78 const_task_queues.push_back(task_queue.get()); | |
| 79 task_queues_.push_back(task_queue.release()); | |
| 80 } | |
| 81 selector_.RegisterWorkQueues(const_task_queues); | |
| 82 for (size_t i = 0; i < kTaskQueueCount; i++) | |
| 83 EXPECT_TRUE(selector_.IsQueueEnabled(i)) << i; | |
| 84 } | |
| 85 | |
| 86 const size_t kTaskQueueCount = 5; | |
| 87 base::Closure test_closure_; | |
| 88 PrioritizingTaskQueueSelectorForTest selector_; | |
| 89 ScopedVector<base::TaskQueue> task_queues_; | |
| 90 }; | |
| 91 | |
| 92 TEST_F(PrioritizingTaskQueueSelectorTest, TestDefaultPriority) { | |
| 93 std::vector<base::PendingTask> tasks = GetTasks(5); | |
| 94 size_t queue_order[] = {4, 3, 2, 1, 0}; | |
| 95 PushTasks(tasks, queue_order); | |
| 96 EXPECT_THAT(PopTasks(), testing::ElementsAre(4, 3, 2, 1, 0)); | |
| 97 } | |
| 98 | |
| 99 TEST_F(PrioritizingTaskQueueSelectorTest, TestHighPriority) { | |
| 100 std::vector<base::PendingTask> tasks = GetTasks(5); | |
| 101 size_t queue_order[] = {0, 1, 2, 3, 4}; | |
| 102 PushTasks(tasks, queue_order); | |
| 103 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 104 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 0, 1, 3, 4)); | |
| 105 } | |
| 106 | |
| 107 TEST_F(PrioritizingTaskQueueSelectorTest, TestBestEffortPriority) { | |
| 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_.SetQueuePriority( | |
| 112 0, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY); | |
| 113 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 114 EXPECT_THAT(PopTasks(), testing::ElementsAre(2, 1, 3, 4, 0)); | |
| 115 } | |
| 116 | |
| 117 TEST_F(PrioritizingTaskQueueSelectorTest, TestControlPriority) { | |
| 118 std::vector<base::PendingTask> tasks = GetTasks(5); | |
| 119 size_t queue_order[] = {0, 1, 2, 3, 4}; | |
| 120 PushTasks(tasks, queue_order); | |
| 121 selector_.SetQueuePriority(4, | |
| 122 PrioritizingTaskQueueSelector::CONTROL_PRIORITY); | |
| 123 EXPECT_TRUE(selector_.IsQueueEnabled(4)); | |
| 124 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 125 EXPECT_TRUE(selector_.IsQueueEnabled(2)); | |
| 126 EXPECT_THAT(PopTasks(), testing::ElementsAre(4, 2, 0, 1, 3)); | |
| 127 } | |
| 128 | |
| 129 TEST_F(PrioritizingTaskQueueSelectorTest, | |
| 130 TestDisableReturnsTrueIfQueueWasEnabled) { | |
| 131 selector_.EnableQueue(1, PrioritizingTaskQueueSelector::NORMAL_PRIORITY); | |
| 132 EXPECT_TRUE(selector_.DisableQueueInternal(1)); | |
| 133 } | |
| 134 | |
| 135 TEST_F(PrioritizingTaskQueueSelectorTest, | |
| 136 TestDisableReturnsFalseIfQueueWasAlreadyDisabled) { | |
| 137 selector_.DisableQueue(1); | |
| 138 EXPECT_FALSE(selector_.DisableQueueInternal(1)); | |
| 139 } | |
| 140 | |
| 141 TEST_F(PrioritizingTaskQueueSelectorTest, TestDisableEnable) { | |
| 142 MockObserver mock_observer; | |
| 143 selector_.SetTaskQueueSelectorObserver(&mock_observer); | |
| 144 | |
| 145 std::vector<base::PendingTask> tasks = GetTasks(5); | |
| 146 size_t queue_order[] = {0, 1, 2, 3, 4}; | |
| 147 PushTasks(tasks, queue_order); | |
| 148 selector_.DisableQueue(2); | |
| 149 EXPECT_FALSE(selector_.IsQueueEnabled(2)); | |
| 150 selector_.DisableQueue(4); | |
| 151 EXPECT_FALSE(selector_.IsQueueEnabled(4)); | |
| 152 EXPECT_THAT(PopTasks(), testing::ElementsAre(0, 1, 3)); | |
| 153 | |
| 154 EXPECT_CALL(mock_observer, OnTaskQueueEnabled()).Times(2); | |
| 155 selector_.EnableQueue(2, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY); | |
| 156 EXPECT_THAT(PopTasks(), testing::ElementsAre(2)); | |
| 157 selector_.EnableQueue(4, PrioritizingTaskQueueSelector::NORMAL_PRIORITY); | |
| 158 EXPECT_THAT(PopTasks(), testing::ElementsAre(4)); | |
| 159 } | |
| 160 | |
| 161 TEST_F(PrioritizingTaskQueueSelectorTest, TestEmptyQueues) { | |
| 162 size_t chosen_queue_index = 0; | |
| 163 EXPECT_FALSE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 164 | |
| 165 // Test only disabled queues. | |
| 166 std::vector<base::PendingTask> tasks = GetTasks(1); | |
| 167 size_t queue_order[] = {0}; | |
| 168 PushTasks(tasks, queue_order); | |
| 169 selector_.DisableQueue(0); | |
| 170 EXPECT_FALSE(selector_.IsQueueEnabled(0)); | |
| 171 EXPECT_FALSE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 172 } | |
| 173 | |
| 174 TEST_F(PrioritizingTaskQueueSelectorTest, TestDelay) { | |
| 175 std::vector<base::PendingTask> tasks = GetTasks(5); | |
| 176 tasks[0].delayed_run_time = | |
| 177 base::TimeTicks() + base::TimeDelta::FromMilliseconds(200); | |
| 178 tasks[3].delayed_run_time = | |
| 179 base::TimeTicks() + base::TimeDelta::FromMilliseconds(100); | |
| 180 size_t queue_order[] = {0, 1, 2, 3, 4}; | |
| 181 PushTasks(tasks, queue_order); | |
| 182 EXPECT_THAT(PopTasks(), testing::ElementsAre(1, 2, 4, 3, 0)); | |
| 183 } | |
| 184 | |
| 185 TEST_F(PrioritizingTaskQueueSelectorTest, TestControlStarvesOthers) { | |
| 186 std::vector<base::PendingTask> tasks = GetTasks(4); | |
| 187 size_t queue_order[] = {0, 1, 2, 3}; | |
| 188 PushTasks(tasks, queue_order); | |
| 189 selector_.SetQueuePriority(3, | |
| 190 PrioritizingTaskQueueSelector::CONTROL_PRIORITY); | |
| 191 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 192 selector_.SetQueuePriority( | |
| 193 1, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY); | |
| 194 for (int i = 0; i < 100; i++) { | |
| 195 size_t chosen_queue_index = 0; | |
| 196 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 197 EXPECT_EQ(3ul, chosen_queue_index); | |
| 198 // Don't remove task from queue to simulate all queues still being full. | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 TEST_F(PrioritizingTaskQueueSelectorTest, TestHighPriorityDoesNotStarveNormal) { | |
| 203 std::vector<base::PendingTask> tasks = GetTasks(3); | |
| 204 size_t queue_order[] = {0, 1, 2}; | |
| 205 PushTasks(tasks, queue_order); | |
| 206 selector_.SetQueuePriority(2, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 207 selector_.SetQueuePriority( | |
| 208 1, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY); | |
| 209 size_t counts[] = {0, 0, 0}; | |
| 210 for (int i = 0; i < 100; i++) { | |
| 211 size_t chosen_queue_index = 0; | |
| 212 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 213 counts[chosen_queue_index]++; | |
| 214 // Don't remove task from queue to simulate all queues still being full. | |
| 215 } | |
| 216 EXPECT_GT(counts[0], 0ul); // Check high doesn't starve normal. | |
| 217 EXPECT_GT(counts[2], counts[0]); // Check high gets more chance to run. | |
| 218 EXPECT_EQ(0ul, counts[1]); // Check best effort is starved. | |
| 219 } | |
| 220 | |
| 221 TEST_F(PrioritizingTaskQueueSelectorTest, TestBestEffortGetsStarved) { | |
| 222 std::vector<base::PendingTask> tasks = GetTasks(2); | |
| 223 size_t queue_order[] = {0, 1}; | |
| 224 PushTasks(tasks, queue_order); | |
| 225 selector_.SetQueuePriority( | |
| 226 0, PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY); | |
| 227 selector_.SetQueuePriority(1, PrioritizingTaskQueueSelector::NORMAL_PRIORITY); | |
| 228 size_t chosen_queue_index = 0; | |
| 229 for (int i = 0; i < 100; i++) { | |
| 230 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 231 EXPECT_EQ(1ul, chosen_queue_index); | |
| 232 // Don't remove task from queue to simulate all queues still being full. | |
| 233 } | |
| 234 selector_.SetQueuePriority(1, PrioritizingTaskQueueSelector::HIGH_PRIORITY); | |
| 235 for (int i = 0; i < 100; i++) { | |
| 236 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 237 EXPECT_EQ(1ul, chosen_queue_index); | |
| 238 // Don't remove task from queue to simulate all queues still being full. | |
| 239 } | |
| 240 selector_.SetQueuePriority(1, | |
| 241 PrioritizingTaskQueueSelector::CONTROL_PRIORITY); | |
| 242 for (int i = 0; i < 100; i++) { | |
| 243 EXPECT_TRUE(selector_.SelectWorkQueueToService(&chosen_queue_index)); | |
| 244 EXPECT_EQ(1ul, chosen_queue_index); | |
| 245 // Don't remove task from queue to simulate all queues still being full. | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 } // namespace content | |
| OLD | NEW |