Don't add extra app list launcher page webviews.
[chromium-blink-merge.git] / content / child / scheduler / task_queue_manager_unittest.cc
blobb145b9eab0686f9285830abb1df85d446f07387b
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/task_queue_manager.h"
7 #include "base/threading/thread.h"
8 #include "cc/test/ordered_simple_task_runner.h"
9 #include "cc/test/test_now_source.h"
10 #include "content/child/scheduler/nestable_task_runner_for_test.h"
11 #include "content/child/scheduler/scheduler_message_loop_delegate.h"
12 #include "content/child/scheduler/task_queue_selector.h"
13 #include "testing/gmock/include/gmock/gmock.h"
15 using testing::ElementsAre;
16 using testing::_;
18 namespace content {
19 namespace {
21 class SelectorForTest : public TaskQueueSelector {
22 public:
23 ~SelectorForTest() override {}
25 virtual void AppendQueueToService(size_t queue_index) = 0;
27 virtual const std::vector<const base::TaskQueue*>& work_queues() = 0;
29 void AsValueInto(base::trace_event::TracedValue* state) const override {}
32 // Always selects queue 0.
33 class AutomaticSelectorForTest : public SelectorForTest {
34 public:
35 AutomaticSelectorForTest() {}
36 ~AutomaticSelectorForTest() override {}
38 void RegisterWorkQueues(
39 const std::vector<const base::TaskQueue*>& work_queues) override {
40 work_queues_ = work_queues;
43 bool SelectWorkQueueToService(size_t* out_queue_index) override {
44 for (size_t i = 0; i < work_queues_.size(); i++) {
45 if (!work_queues_[i]->empty()) {
46 *out_queue_index = i;
47 return true;
50 return false;
53 void AppendQueueToService(size_t queue_index) override {
54 DCHECK(false) << "Not supported";
57 const std::vector<const base::TaskQueue*>& work_queues() override {
58 return work_queues_;
61 private:
62 std::vector<const base::TaskQueue*> work_queues_;
64 DISALLOW_COPY_AND_ASSIGN(AutomaticSelectorForTest);
67 class ExplicitSelectorForTest : public SelectorForTest {
68 public:
69 ExplicitSelectorForTest() {}
70 ~ExplicitSelectorForTest() override {}
72 void RegisterWorkQueues(
73 const std::vector<const base::TaskQueue*>& work_queues) override {
74 work_queues_ = work_queues;
77 bool SelectWorkQueueToService(size_t* out_queue_index) override {
78 if (queues_to_service_.empty())
79 return false;
80 *out_queue_index = queues_to_service_.front();
81 queues_to_service_.pop_front();
82 return true;
85 void AppendQueueToService(size_t queue_index) override {
86 queues_to_service_.push_back(queue_index);
89 const std::vector<const base::TaskQueue*>& work_queues() override {
90 return work_queues_;
93 private:
94 std::deque<size_t> queues_to_service_;
95 std::vector<const base::TaskQueue*> work_queues_;
97 DISALLOW_COPY_AND_ASSIGN(ExplicitSelectorForTest);
100 class TaskQueueManagerTest : public testing::Test {
101 public:
102 void DeleteTaskQueueManager() {
103 manager_.reset();
106 protected:
107 enum class SelectorType {
108 Automatic,
109 Explicit,
112 void Initialize(size_t num_queues, SelectorType type) {
113 now_src_ = cc::TestNowSource::Create(1000);
114 test_task_runner_ =
115 make_scoped_refptr(new cc::OrderedSimpleTaskRunner(now_src_, false));
116 selector_ = make_scoped_ptr(createSelectorForTest(type));
117 manager_ = make_scoped_ptr(new TaskQueueManager(
118 num_queues, NestableTaskRunnerForTest::Create(test_task_runner_.get()),
119 selector_.get(), "test.scheduler"));
120 manager_->SetTimeSourceForTesting(now_src_);
122 EXPECT_EQ(num_queues, selector_->work_queues().size());
125 void InitializeWithRealMessageLoop(size_t num_queues, SelectorType type) {
126 message_loop_.reset(new base::MessageLoop());
127 selector_ = make_scoped_ptr(createSelectorForTest(type));
128 manager_ = make_scoped_ptr(new TaskQueueManager(
129 num_queues, SchedulerMessageLoopDelegate::Create(message_loop_.get()),
130 selector_.get(), "test.scheduler"));
131 EXPECT_EQ(num_queues, selector_->work_queues().size());
134 SelectorForTest* createSelectorForTest(SelectorType type) {
135 switch (type) {
136 case SelectorType::Automatic:
137 return new AutomaticSelectorForTest();
139 case SelectorType::Explicit:
140 return new ExplicitSelectorForTest();
143 return nullptr;
146 scoped_refptr<cc::TestNowSource> now_src_;
147 scoped_refptr<cc::OrderedSimpleTaskRunner> test_task_runner_;
148 scoped_ptr<SelectorForTest> selector_;
149 scoped_ptr<TaskQueueManager> manager_;
150 scoped_ptr<base::MessageLoop> message_loop_;
153 void PostFromNestedRunloop(base::MessageLoop* message_loop,
154 base::SingleThreadTaskRunner* runner,
155 std::vector<std::pair<base::Closure, bool>>* tasks) {
156 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop);
157 for (std::pair<base::Closure, bool>& pair : *tasks) {
158 if (pair.second) {
159 runner->PostTask(FROM_HERE, pair.first);
160 } else {
161 runner->PostNonNestableTask(FROM_HERE, pair.first);
164 message_loop->RunUntilIdle();
167 void TestTask(int value, std::vector<int>* out_result) {
168 out_result->push_back(value);
171 TEST_F(TaskQueueManagerTest, SingleQueuePosting) {
172 Initialize(1u, SelectorType::Automatic);
174 std::vector<int> run_order;
175 scoped_refptr<base::SingleThreadTaskRunner> runner =
176 manager_->TaskRunnerForQueue(0);
178 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
179 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
180 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
182 test_task_runner_->RunUntilIdle();
183 EXPECT_THAT(run_order, ElementsAre(1, 2, 3));
186 TEST_F(TaskQueueManagerTest, MultiQueuePosting) {
187 Initialize(3u, SelectorType::Explicit);
189 std::vector<int> run_order;
190 scoped_refptr<base::SingleThreadTaskRunner> runners[3] = {
191 manager_->TaskRunnerForQueue(0),
192 manager_->TaskRunnerForQueue(1),
193 manager_->TaskRunnerForQueue(2)};
195 selector_->AppendQueueToService(0);
196 selector_->AppendQueueToService(1);
197 selector_->AppendQueueToService(2);
198 selector_->AppendQueueToService(0);
199 selector_->AppendQueueToService(1);
200 selector_->AppendQueueToService(2);
202 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
203 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
204 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
205 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
206 runners[2]->PostTask(FROM_HERE, base::Bind(&TestTask, 5, &run_order));
207 runners[2]->PostTask(FROM_HERE, base::Bind(&TestTask, 6, &run_order));
209 test_task_runner_->RunUntilIdle();
210 EXPECT_THAT(run_order, ElementsAre(1, 3, 5, 2, 4, 6));
213 void NopTask() {
216 TEST_F(TaskQueueManagerTest, NowNotCalledWhenThereAreNoDelayedTasks) {
217 Initialize(3u, SelectorType::Explicit);
219 scoped_refptr<cc::TestNowSource> now_src = cc::TestNowSource::Create(1000);
220 manager_->SetTimeSourceForTesting(now_src);
222 scoped_refptr<base::SingleThreadTaskRunner> runners[3] = {
223 manager_->TaskRunnerForQueue(0),
224 manager_->TaskRunnerForQueue(1),
225 manager_->TaskRunnerForQueue(2)};
227 selector_->AppendQueueToService(0);
228 selector_->AppendQueueToService(1);
229 selector_->AppendQueueToService(2);
230 selector_->AppendQueueToService(0);
231 selector_->AppendQueueToService(1);
232 selector_->AppendQueueToService(2);
234 runners[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
235 runners[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
236 runners[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
237 runners[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
238 runners[2]->PostTask(FROM_HERE, base::Bind(&NopTask));
239 runners[2]->PostTask(FROM_HERE, base::Bind(&NopTask));
241 test_task_runner_->RunUntilIdle();
243 EXPECT_EQ(0, now_src->NumNowCalls());
246 TEST_F(TaskQueueManagerTest, NonNestableTaskPosting) {
247 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
249 std::vector<int> run_order;
250 scoped_refptr<base::SingleThreadTaskRunner> runner =
251 manager_->TaskRunnerForQueue(0);
253 runner->PostNonNestableTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
255 message_loop_->RunUntilIdle();
256 EXPECT_THAT(run_order, ElementsAre(1));
259 TEST_F(TaskQueueManagerTest, NonNestableTaskExecutesInExpectedOrder) {
260 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
262 std::vector<int> run_order;
263 scoped_refptr<base::SingleThreadTaskRunner> runner =
264 manager_->TaskRunnerForQueue(0);
266 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
267 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
268 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
269 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
270 runner->PostNonNestableTask(FROM_HERE, base::Bind(&TestTask, 5, &run_order));
272 message_loop_->RunUntilIdle();
273 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4, 5));
276 TEST_F(TaskQueueManagerTest, NonNestableTaskDoesntExecuteInNestedLoop) {
277 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
279 std::vector<int> run_order;
280 scoped_refptr<base::SingleThreadTaskRunner> runner =
281 manager_->TaskRunnerForQueue(0);
283 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
284 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
286 std::vector<std::pair<base::Closure, bool>> tasks_to_post_from_nested_loop;
287 tasks_to_post_from_nested_loop.push_back(
288 std::make_pair(base::Bind(&TestTask, 3, &run_order), false));
289 tasks_to_post_from_nested_loop.push_back(
290 std::make_pair(base::Bind(&TestTask, 4, &run_order), true));
291 tasks_to_post_from_nested_loop.push_back(
292 std::make_pair(base::Bind(&TestTask, 5, &run_order), true));
294 runner->PostTask(
295 FROM_HERE, base::Bind(&PostFromNestedRunloop, message_loop_.get(), runner,
296 base::Unretained(&tasks_to_post_from_nested_loop)));
298 message_loop_->RunUntilIdle();
299 // Note we expect task 3 to run last because it's non-nestable.
300 EXPECT_THAT(run_order, ElementsAre(1, 2, 4, 5, 3));
303 TEST_F(TaskQueueManagerTest, QueuePolling) {
304 Initialize(1u, SelectorType::Automatic);
306 std::vector<int> run_order;
307 scoped_refptr<base::SingleThreadTaskRunner> runner =
308 manager_->TaskRunnerForQueue(0);
310 EXPECT_TRUE(manager_->IsQueueEmpty(0));
311 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
312 EXPECT_FALSE(manager_->IsQueueEmpty(0));
314 test_task_runner_->RunUntilIdle();
315 EXPECT_TRUE(manager_->IsQueueEmpty(0));
318 TEST_F(TaskQueueManagerTest, DelayedTaskPosting) {
319 Initialize(1u, SelectorType::Automatic);
321 std::vector<int> run_order;
322 scoped_refptr<base::SingleThreadTaskRunner> runner =
323 manager_->TaskRunnerForQueue(0);
325 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
326 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
327 delay);
328 EXPECT_EQ(delay, test_task_runner_->DelayToNextTaskTime());
329 EXPECT_TRUE(manager_->IsQueueEmpty(0));
330 EXPECT_TRUE(run_order.empty());
332 // The task doesn't run before the delay has completed.
333 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(9));
334 EXPECT_TRUE(run_order.empty());
336 // After the delay has completed, the task runs normally.
337 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
338 EXPECT_THAT(run_order, ElementsAre(1));
341 TEST_F(TaskQueueManagerTest, DelayedTaskPosting_MultipleTasks_DecendingOrder) {
342 Initialize(1u, SelectorType::Automatic);
344 std::vector<int> run_order;
345 scoped_refptr<base::SingleThreadTaskRunner> runner =
346 manager_->TaskRunnerForQueue(0);
348 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
349 base::TimeDelta::FromMilliseconds(10));
351 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
352 base::TimeDelta::FromMilliseconds(8));
354 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
355 base::TimeDelta::FromMilliseconds(5));
357 EXPECT_EQ(base::TimeDelta::FromMilliseconds(5),
358 test_task_runner_->DelayToNextTaskTime());
360 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
361 EXPECT_THAT(run_order, ElementsAre(3));
362 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3),
363 test_task_runner_->DelayToNextTaskTime());
365 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(3));
366 EXPECT_THAT(run_order, ElementsAre(3, 2));
367 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2),
368 test_task_runner_->DelayToNextTaskTime());
370 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(2));
371 EXPECT_THAT(run_order, ElementsAre(3, 2, 1));
374 TEST_F(TaskQueueManagerTest, DelayedTaskPosting_MultipleTasks_AscendingOrder) {
375 Initialize(1u, SelectorType::Automatic);
377 std::vector<int> run_order;
378 scoped_refptr<base::SingleThreadTaskRunner> runner =
379 manager_->TaskRunnerForQueue(0);
381 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
382 base::TimeDelta::FromMilliseconds(1));
384 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
385 base::TimeDelta::FromMilliseconds(5));
387 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
388 base::TimeDelta::FromMilliseconds(10));
390 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1),
391 test_task_runner_->DelayToNextTaskTime());
393 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
394 EXPECT_THAT(run_order, ElementsAre(1));
395 EXPECT_EQ(base::TimeDelta::FromMilliseconds(4),
396 test_task_runner_->DelayToNextTaskTime());
398 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(4));
399 EXPECT_THAT(run_order, ElementsAre(1, 2));
400 EXPECT_EQ(base::TimeDelta::FromMilliseconds(5),
401 test_task_runner_->DelayToNextTaskTime());
403 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
404 EXPECT_THAT(run_order, ElementsAre(1, 2, 3));
407 TEST_F(TaskQueueManagerTest, PostDelayedTask_SharesUnderlyingDelayedTasks) {
408 Initialize(1u, SelectorType::Automatic);
410 std::vector<int> run_order;
411 scoped_refptr<base::SingleThreadTaskRunner> runner =
412 manager_->TaskRunnerForQueue(0);
414 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
415 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
416 delay);
417 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
418 delay);
419 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
420 delay);
422 EXPECT_EQ(1u, test_task_runner_->NumPendingTasks());
425 class TestObject {
426 public:
427 ~TestObject() { destructor_count_++; }
429 void Run() { FAIL() << "TestObject::Run should not be called"; }
431 static int destructor_count_;
434 int TestObject::destructor_count_ = 0;
436 TEST_F(TaskQueueManagerTest, PendingDelayedTasksRemovedOnShutdown) {
437 Initialize(1u, SelectorType::Automatic);
439 TestObject::destructor_count_ = 0;
441 scoped_refptr<base::SingleThreadTaskRunner> runner =
442 manager_->TaskRunnerForQueue(0);
444 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
445 runner->PostDelayedTask(
446 FROM_HERE, base::Bind(&TestObject::Run, base::Owned(new TestObject())),
447 delay);
448 runner->PostTask(
449 FROM_HERE, base::Bind(&TestObject::Run, base::Owned(new TestObject())));
451 manager_.reset();
453 EXPECT_EQ(2, TestObject::destructor_count_);
456 TEST_F(TaskQueueManagerTest, ManualPumping) {
457 Initialize(1u, SelectorType::Automatic);
458 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
460 std::vector<int> run_order;
461 scoped_refptr<base::SingleThreadTaskRunner> runner =
462 manager_->TaskRunnerForQueue(0);
464 // Posting a task when pumping is disabled doesn't result in work getting
465 // posted.
466 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
467 EXPECT_FALSE(test_task_runner_->HasPendingTasks());
469 // However polling still works.
470 EXPECT_FALSE(manager_->IsQueueEmpty(0));
472 // After pumping the task runs normally.
473 manager_->PumpQueue(0);
474 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
475 test_task_runner_->RunUntilIdle();
476 EXPECT_THAT(run_order, ElementsAre(1));
479 TEST_F(TaskQueueManagerTest, ManualPumpingToggle) {
480 Initialize(1u, SelectorType::Automatic);
481 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
483 std::vector<int> run_order;
484 scoped_refptr<base::SingleThreadTaskRunner> runner =
485 manager_->TaskRunnerForQueue(0);
487 // Posting a task when pumping is disabled doesn't result in work getting
488 // posted.
489 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
490 EXPECT_FALSE(test_task_runner_->HasPendingTasks());
492 // When pumping is enabled the task runs normally.
493 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AUTO);
494 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
495 test_task_runner_->RunUntilIdle();
496 EXPECT_THAT(run_order, ElementsAre(1));
499 TEST_F(TaskQueueManagerTest, DenyRunning) {
500 Initialize(1u, SelectorType::Explicit);
502 std::vector<int> run_order;
503 scoped_refptr<base::SingleThreadTaskRunner> runner =
504 manager_->TaskRunnerForQueue(0);
505 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
507 // Since we haven't appended a work queue to be selected, the task doesn't
508 // run.
509 test_task_runner_->RunUntilIdle();
510 EXPECT_TRUE(run_order.empty());
512 // Pumping the queue again with a selected work queue runs the task.
513 manager_->PumpQueue(0);
514 selector_->AppendQueueToService(0);
515 test_task_runner_->RunUntilIdle();
516 EXPECT_THAT(run_order, ElementsAre(1));
519 TEST_F(TaskQueueManagerTest, ManualPumpingWithDelayedTask) {
520 Initialize(1u, SelectorType::Automatic);
521 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
523 std::vector<int> run_order;
524 scoped_refptr<base::SingleThreadTaskRunner> runner =
525 manager_->TaskRunnerForQueue(0);
527 // Posting a delayed task when pumping will apply the delay, but won't cause
528 // work to executed afterwards.
529 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
530 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
531 delay);
533 // After pumping but before the delay period has expired, task does not run.
534 manager_->PumpQueue(0);
535 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
536 EXPECT_TRUE(run_order.empty());
538 // Once the delay has expired, pumping causes the task to run.
539 now_src_->AdvanceNow(base::TimeDelta::FromMilliseconds(5));
540 manager_->PumpQueue(0);
541 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
542 test_task_runner_->RunPendingTasks();
543 EXPECT_THAT(run_order, ElementsAre(1));
546 TEST_F(TaskQueueManagerTest, ManualPumpingWithMultipleDelayedTasks) {
547 Initialize(1u, SelectorType::Automatic);
548 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
550 std::vector<int> run_order;
551 scoped_refptr<base::SingleThreadTaskRunner> runner =
552 manager_->TaskRunnerForQueue(0);
554 // Posting a delayed task when pumping will apply the delay, but won't cause
555 // work to executed afterwards.
556 base::TimeDelta delay1(base::TimeDelta::FromMilliseconds(1));
557 base::TimeDelta delay2(base::TimeDelta::FromMilliseconds(10));
558 base::TimeDelta delay3(base::TimeDelta::FromMilliseconds(20));
559 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
560 delay1);
561 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
562 delay2);
563 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
564 delay3);
566 now_src_->AdvanceNow(base::TimeDelta::FromMilliseconds(15));
567 test_task_runner_->RunUntilIdle();
568 EXPECT_TRUE(run_order.empty());
570 // Once the delay has expired, pumping causes the task to run.
571 manager_->PumpQueue(0);
572 test_task_runner_->RunUntilIdle();
573 EXPECT_THAT(run_order, ElementsAre(1, 2));
576 TEST_F(TaskQueueManagerTest, DelayedTasksDontAutoRunWithManualPumping) {
577 Initialize(1u, SelectorType::Automatic);
578 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
580 std::vector<int> run_order;
581 scoped_refptr<base::SingleThreadTaskRunner> runner =
582 manager_->TaskRunnerForQueue(0);
584 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
585 runner->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
586 delay);
588 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(10));
589 EXPECT_TRUE(run_order.empty());
592 TEST_F(TaskQueueManagerTest, ManualPumpingWithNonEmptyWorkQueue) {
593 Initialize(1u, SelectorType::Automatic);
594 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::MANUAL);
596 std::vector<int> run_order;
597 scoped_refptr<base::SingleThreadTaskRunner> runner =
598 manager_->TaskRunnerForQueue(0);
600 // Posting two tasks and pumping twice should result in two tasks in the work
601 // queue.
602 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
603 manager_->PumpQueue(0);
604 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
605 manager_->PumpQueue(0);
607 EXPECT_EQ(2u, selector_->work_queues()[0]->size());
610 void ReentrantTestTask(scoped_refptr<base::SingleThreadTaskRunner> runner,
611 int countdown,
612 std::vector<int>* out_result) {
613 out_result->push_back(countdown);
614 if (--countdown) {
615 runner->PostTask(FROM_HERE,
616 Bind(&ReentrantTestTask, runner, countdown, out_result));
620 TEST_F(TaskQueueManagerTest, ReentrantPosting) {
621 Initialize(1u, SelectorType::Automatic);
623 std::vector<int> run_order;
624 scoped_refptr<base::SingleThreadTaskRunner> runner =
625 manager_->TaskRunnerForQueue(0);
627 runner->PostTask(FROM_HERE, Bind(&ReentrantTestTask, runner, 3, &run_order));
629 test_task_runner_->RunUntilIdle();
630 EXPECT_THAT(run_order, ElementsAre(3, 2, 1));
633 TEST_F(TaskQueueManagerTest, NoTasksAfterShutdown) {
634 Initialize(1u, SelectorType::Automatic);
636 std::vector<int> run_order;
637 scoped_refptr<base::SingleThreadTaskRunner> runner =
638 manager_->TaskRunnerForQueue(0);
640 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
641 manager_.reset();
642 selector_.reset();
643 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
645 test_task_runner_->RunUntilIdle();
646 EXPECT_TRUE(run_order.empty());
649 void PostTaskToRunner(scoped_refptr<base::SingleThreadTaskRunner> runner,
650 std::vector<int>* run_order) {
651 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, run_order));
654 TEST_F(TaskQueueManagerTest, PostFromThread) {
655 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
657 std::vector<int> run_order;
658 scoped_refptr<base::SingleThreadTaskRunner> runner =
659 manager_->TaskRunnerForQueue(0);
661 base::Thread thread("TestThread");
662 thread.Start();
663 thread.message_loop()->PostTask(
664 FROM_HERE, base::Bind(&PostTaskToRunner, runner, &run_order));
665 thread.Stop();
667 message_loop_->RunUntilIdle();
668 EXPECT_THAT(run_order, ElementsAre(1));
671 void RePostingTestTask(scoped_refptr<base::SingleThreadTaskRunner> runner,
672 int* run_count) {
673 (*run_count)++;
674 runner->PostTask(FROM_HERE, Bind(&RePostingTestTask,
675 base::Unretained(runner.get()), run_count));
678 TEST_F(TaskQueueManagerTest, DoWorkCantPostItselfMultipleTimes) {
679 Initialize(1u, SelectorType::Automatic);
680 scoped_refptr<base::SingleThreadTaskRunner> runner =
681 manager_->TaskRunnerForQueue(0);
683 int run_count = 0;
684 runner->PostTask(FROM_HERE,
685 base::Bind(&RePostingTestTask, runner, &run_count));
687 test_task_runner_->RunPendingTasks();
688 // NOTE without the executing_task_ check in MaybePostDoWorkOnMainRunner there
689 // will be two tasks here.
690 EXPECT_EQ(1u, test_task_runner_->NumPendingTasks());
691 EXPECT_EQ(1, run_count);
694 TEST_F(TaskQueueManagerTest, PostFromNestedRunloop) {
695 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
697 std::vector<int> run_order;
698 scoped_refptr<base::SingleThreadTaskRunner> runner =
699 manager_->TaskRunnerForQueue(0);
701 std::vector<std::pair<base::Closure, bool>> tasks_to_post_from_nested_loop;
702 tasks_to_post_from_nested_loop.push_back(
703 std::make_pair(base::Bind(&TestTask, 1, &run_order), true));
705 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 0, &run_order));
706 runner->PostTask(
707 FROM_HERE, base::Bind(&PostFromNestedRunloop, message_loop_.get(), runner,
708 base::Unretained(&tasks_to_post_from_nested_loop)));
709 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
711 message_loop_->RunUntilIdle();
713 EXPECT_THAT(run_order, ElementsAre(0, 2, 1));
716 TEST_F(TaskQueueManagerTest, WorkBatching) {
717 Initialize(1u, SelectorType::Automatic);
719 manager_->SetWorkBatchSize(2);
721 std::vector<int> run_order;
722 scoped_refptr<base::SingleThreadTaskRunner> runner =
723 manager_->TaskRunnerForQueue(0);
725 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
726 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
727 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
728 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
730 // Running one task in the host message loop should cause two posted tasks to
731 // get executed.
732 EXPECT_EQ(test_task_runner_->NumPendingTasks(), 1u);
733 test_task_runner_->RunPendingTasks();
734 EXPECT_THAT(run_order, ElementsAre(1, 2));
736 // The second task runs the remaining two posted tasks.
737 EXPECT_EQ(test_task_runner_->NumPendingTasks(), 1u);
738 test_task_runner_->RunPendingTasks();
739 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4));
742 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeup) {
743 Initialize(2u, SelectorType::Explicit);
744 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
746 std::vector<int> run_order;
747 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
748 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
750 selector_->AppendQueueToService(1);
751 selector_->AppendQueueToService(0);
752 selector_->AppendQueueToService(0);
754 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
755 test_task_runner_->RunUntilIdle();
756 EXPECT_TRUE(run_order.empty()); // Shouldn't run - no other task to wake TQM.
758 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
759 test_task_runner_->RunUntilIdle();
760 EXPECT_TRUE(run_order.empty()); // Still shouldn't wake TQM.
762 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
763 test_task_runner_->RunUntilIdle();
764 // Executing a task on an auto pumped queue should wake the TQM.
765 EXPECT_THAT(run_order, ElementsAre(3, 1, 2));
768 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupWhenAlreadyAwake) {
769 Initialize(2u, SelectorType::Explicit);
770 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
772 std::vector<int> run_order;
773 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
774 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
776 selector_->AppendQueueToService(1);
777 selector_->AppendQueueToService(0);
779 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
780 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
781 test_task_runner_->RunUntilIdle();
782 EXPECT_THAT(run_order, ElementsAre(2, 1)); // TQM was already awake.
785 TEST_F(TaskQueueManagerTest,
786 AutoPumpAfterWakeupTriggeredByManuallyPumpedQueue) {
787 Initialize(2u, SelectorType::Explicit);
788 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
789 manager_->SetPumpPolicy(1, TaskQueueManager::PumpPolicy::MANUAL);
791 std::vector<int> run_order;
792 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
793 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
795 selector_->AppendQueueToService(1);
796 selector_->AppendQueueToService(0);
798 runners[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
799 test_task_runner_->RunUntilIdle();
800 EXPECT_TRUE(run_order.empty()); // Shouldn't run - no other task to wake TQM.
802 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
803 test_task_runner_->RunUntilIdle();
804 // This still shouldn't wake TQM as manual queue was not pumped.
805 EXPECT_TRUE(run_order.empty());
807 manager_->PumpQueue(1);
808 test_task_runner_->RunUntilIdle();
809 // Executing a task on an auto pumped queue should wake the TQM.
810 EXPECT_THAT(run_order, ElementsAre(2, 1));
813 void TestPostingTask(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
814 base::Closure task) {
815 task_runner->PostTask(FROM_HERE, task);
818 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupFromTask) {
819 Initialize(2u, SelectorType::Explicit);
820 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
822 std::vector<int> run_order;
823 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
824 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
826 selector_->AppendQueueToService(1);
827 selector_->AppendQueueToService(1);
828 selector_->AppendQueueToService(0);
830 // Check that a task which posts a task to an auto pump after wakeup queue
831 // doesn't cause the queue to wake up.
832 base::Closure after_wakeup_task = base::Bind(&TestTask, 1, &run_order);
833 runners[1]->PostTask(
834 FROM_HERE, base::Bind(&TestPostingTask, runners[0], after_wakeup_task));
835 test_task_runner_->RunUntilIdle();
836 EXPECT_TRUE(run_order.empty());
838 // Wake up the queue.
839 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
840 test_task_runner_->RunUntilIdle();
841 EXPECT_THAT(run_order, ElementsAre(2, 1));
844 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupFromMultipleTasks) {
845 Initialize(2u, SelectorType::Explicit);
846 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
848 std::vector<int> run_order;
849 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
850 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
852 selector_->AppendQueueToService(1);
853 selector_->AppendQueueToService(1);
854 selector_->AppendQueueToService(1);
855 selector_->AppendQueueToService(0);
856 selector_->AppendQueueToService(0);
858 // Check that a task which posts a task to an auto pump after wakeup queue
859 // doesn't cause the queue to wake up.
860 base::Closure after_wakeup_task_1 = base::Bind(&TestTask, 1, &run_order);
861 base::Closure after_wakeup_task_2 = base::Bind(&TestTask, 2, &run_order);
862 runners[1]->PostTask(
863 FROM_HERE, base::Bind(&TestPostingTask, runners[0], after_wakeup_task_1));
864 runners[1]->PostTask(
865 FROM_HERE, base::Bind(&TestPostingTask, runners[0], after_wakeup_task_2));
866 test_task_runner_->RunUntilIdle();
867 EXPECT_TRUE(run_order.empty());
869 // Wake up the queue.
870 runners[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
871 test_task_runner_->RunUntilIdle();
872 EXPECT_THAT(run_order, ElementsAre(3, 1, 2));
875 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupBecomesQuiescent) {
876 Initialize(2u, SelectorType::Explicit);
877 manager_->SetPumpPolicy(0, TaskQueueManager::PumpPolicy::AFTER_WAKEUP);
879 int run_count = 0;
880 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
881 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
883 selector_->AppendQueueToService(1);
884 selector_->AppendQueueToService(0);
885 selector_->AppendQueueToService(0);
886 // Append extra service queue '0' entries to the selector otherwise test will
887 // finish even if the RePostingTestTask woke each other up.
888 selector_->AppendQueueToService(0);
889 selector_->AppendQueueToService(0);
891 // Check that if multiple tasks reposts themselves onto a pump-after-wakeup
892 // queue they don't wake each other and will eventually stop when no other
893 // tasks execute.
894 runners[0]->PostTask(FROM_HERE,
895 base::Bind(&RePostingTestTask, runners[0], &run_count));
896 runners[0]->PostTask(FROM_HERE,
897 base::Bind(&RePostingTestTask, runners[0], &run_count));
898 runners[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
899 test_task_runner_->RunUntilIdle();
900 // The reposting tasks posted to the after wakeup queue shouldn't have woken
901 // each other up.
902 EXPECT_EQ(2, run_count);
905 class MockTaskObserver : public base::MessageLoop::TaskObserver {
906 public:
907 MOCK_METHOD1(DidProcessTask, void(const base::PendingTask& task));
908 MOCK_METHOD1(WillProcessTask, void(const base::PendingTask& task));
911 TEST_F(TaskQueueManagerTest, TaskObserverAdding) {
912 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
913 MockTaskObserver observer;
915 manager_->SetWorkBatchSize(2);
916 manager_->AddTaskObserver(&observer);
918 std::vector<int> run_order;
919 scoped_refptr<base::SingleThreadTaskRunner> runner =
920 manager_->TaskRunnerForQueue(0);
922 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
923 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
925 // Two pairs of callbacks for the tasks above plus another one for the
926 // DoWork() posted by the task queue manager.
927 EXPECT_CALL(observer, WillProcessTask(_)).Times(3);
928 EXPECT_CALL(observer, DidProcessTask(_)).Times(3);
929 message_loop_->RunUntilIdle();
932 TEST_F(TaskQueueManagerTest, TaskObserverRemoving) {
933 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
934 MockTaskObserver observer;
935 manager_->SetWorkBatchSize(2);
936 manager_->AddTaskObserver(&observer);
937 manager_->RemoveTaskObserver(&observer);
939 std::vector<int> run_order;
940 scoped_refptr<base::SingleThreadTaskRunner> runner =
941 manager_->TaskRunnerForQueue(0);
943 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
945 EXPECT_CALL(observer, WillProcessTask(_)).Times(0);
946 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
948 message_loop_->RunUntilIdle();
951 void RemoveObserverTask(TaskQueueManager* manager,
952 base::MessageLoop::TaskObserver* observer) {
953 manager->RemoveTaskObserver(observer);
956 TEST_F(TaskQueueManagerTest, TaskObserverRemovingInsideTask) {
957 InitializeWithRealMessageLoop(1u, SelectorType::Automatic);
958 MockTaskObserver observer;
959 manager_->SetWorkBatchSize(3);
960 manager_->AddTaskObserver(&observer);
962 scoped_refptr<base::SingleThreadTaskRunner> runner =
963 manager_->TaskRunnerForQueue(0);
964 runner->PostTask(FROM_HERE,
965 base::Bind(&RemoveObserverTask, manager_.get(), &observer));
967 EXPECT_CALL(observer, WillProcessTask(_)).Times(1);
968 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
969 message_loop_->RunUntilIdle();
972 TEST_F(TaskQueueManagerTest, ThreadCheckAfterTermination) {
973 Initialize(1u, SelectorType::Automatic);
974 scoped_refptr<base::SingleThreadTaskRunner> runner =
975 manager_->TaskRunnerForQueue(0);
976 EXPECT_TRUE(runner->RunsTasksOnCurrentThread());
977 manager_.reset();
978 EXPECT_TRUE(runner->RunsTasksOnCurrentThread());
981 TEST_F(TaskQueueManagerTest, NextPendingDelayedTaskRunTime) {
982 scoped_refptr<cc::TestNowSource> clock(cc::TestNowSource::Create());
983 Initialize(2u, SelectorType::Explicit);
984 manager_->SetTimeSourceForTesting(clock);
986 scoped_refptr<base::SingleThreadTaskRunner> runners[2] = {
987 manager_->TaskRunnerForQueue(0), manager_->TaskRunnerForQueue(1)};
989 // With no delayed tasks.
990 EXPECT_TRUE(manager_->NextPendingDelayedTaskRunTime().is_null());
992 // With a non-delayed task.
993 runners[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
994 EXPECT_TRUE(manager_->NextPendingDelayedTaskRunTime().is_null());
996 // With a delayed task.
997 base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(50);
998 runners[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
999 EXPECT_EQ(clock->Now() + expected_delay,
1000 manager_->NextPendingDelayedTaskRunTime());
1002 // With another delayed task in the same queue with a longer delay.
1003 runners[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask),
1004 base::TimeDelta::FromMilliseconds(100));
1005 EXPECT_EQ(clock->Now() + expected_delay,
1006 manager_->NextPendingDelayedTaskRunTime());
1008 // With another delayed task in the same queue with a shorter delay.
1009 expected_delay = base::TimeDelta::FromMilliseconds(20);
1010 runners[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
1011 EXPECT_EQ(clock->Now() + expected_delay,
1012 manager_->NextPendingDelayedTaskRunTime());
1014 // With another delayed task in a different queue with a shorter delay.
1015 expected_delay = base::TimeDelta::FromMilliseconds(10);
1016 runners[1]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
1017 EXPECT_EQ(clock->Now() + expected_delay,
1018 manager_->NextPendingDelayedTaskRunTime());
1020 // Test it updates as time progresses
1021 clock->AdvanceNow(expected_delay);
1022 EXPECT_EQ(clock->Now(), manager_->NextPendingDelayedTaskRunTime());
1025 TEST_F(TaskQueueManagerTest, NextPendingDelayedTaskRunTime_MultipleQueues) {
1026 Initialize(3u, SelectorType::Automatic);
1028 scoped_refptr<base::SingleThreadTaskRunner> runners[3] = {
1029 manager_->TaskRunnerForQueue(0),
1030 manager_->TaskRunnerForQueue(1),
1031 manager_->TaskRunnerForQueue(2)};
1033 base::TimeDelta delay1 = base::TimeDelta::FromMilliseconds(50);
1034 base::TimeDelta delay2 = base::TimeDelta::FromMilliseconds(5);
1035 base::TimeDelta delay3 = base::TimeDelta::FromMilliseconds(10);
1036 runners[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay1);
1037 runners[1]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay2);
1038 runners[2]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay3);
1040 EXPECT_EQ(now_src_->Now() + delay2,
1041 manager_->NextPendingDelayedTaskRunTime());
1044 TEST_F(TaskQueueManagerTest, DeleteTaskQueueManagerInsideATask) {
1045 Initialize(1u, SelectorType::Automatic);
1047 scoped_refptr<base::SingleThreadTaskRunner> runner =
1048 manager_->TaskRunnerForQueue(0);
1049 runner->PostTask(FROM_HERE,
1050 base::Bind(&TaskQueueManagerTest::DeleteTaskQueueManager,
1051 base::Unretained(this)));
1053 // This should not crash, assuming DoWork detects the TaskQueueManager has
1054 // been deleted.
1055 test_task_runner_->RunUntilIdle();
1058 } // namespace
1059 } // namespace content