Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/task_scheduler/utils.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/task_scheduler/delayed_task_manager.h" | |
| 14 #include "base/task_scheduler/priority_queue.h" | |
| 15 #include "base/task_scheduler/scheduler_task_executor.h" | |
| 16 #include "base/task_scheduler/sequence.h" | |
| 17 #include "base/task_scheduler/task.h" | |
| 18 #include "base/task_scheduler/task_tracker.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace base { | |
| 23 namespace internal { | |
| 24 namespace { | |
| 25 | |
| 26 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { | |
| 27 public: | |
| 28 void PostTaskWithSequence(std::unique_ptr<Task> task, | |
| 29 scoped_refptr<Sequence> sequence) override { | |
| 30 PostTaskWithSequenceMock(task.get(), sequence.get()); | |
| 31 } | |
| 32 | |
| 33 MOCK_METHOD2(PostTaskWithSequenceMock, | |
| 34 void(const Task* task, const Sequence* sequence)); | |
| 35 }; | |
| 36 | |
| 37 // Verifies that when PostTaskToExecutor receives a non-delayed Task that is | |
| 38 // allowed to be posted, it forwards it to a SchedulerTaskExecutor. | |
| 39 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskAllowed) { | |
| 40 std::unique_ptr<Task> task( | |
| 41 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | |
| 42 const Task* task_raw = task.get(); | |
| 43 scoped_refptr<Sequence> sequence(new Sequence); | |
| 44 testing::StrictMock<MockSchedulerTaskExecutor> executor; | |
| 45 TaskTracker task_tracker; | |
| 46 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 47 | |
| 48 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_raw, sequence.get())); | |
| 49 PostTaskToExecutor(std::move(task), sequence, &executor, &task_tracker, | |
| 50 &delayed_task_manager); | |
| 51 } | |
| 52 | |
| 53 // Verifies that when PostTaskToExecutor receives a delayed Task that is allowed | |
| 54 // to be posted, it forwards it to a DelayedTaskManager. | |
| 55 TEST(TaskSchedulerPostTaskToExecutorTest, PostDelayedTaskAllowed) { | |
| 56 scoped_refptr<Sequence> sequence(new Sequence); | |
| 57 testing::StrictMock<MockSchedulerTaskExecutor> executor; | |
| 58 TaskTracker task_tracker; | |
| 59 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 60 | |
| 61 EXPECT_TRUE(delayed_task_manager.GetDelayedRunTime().is_null()); | |
| 62 PostTaskToExecutor( | |
| 63 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), | |
| 64 TimeTicks::Now() + TimeDelta::FromSeconds(10))), | |
| 65 sequence, &executor, &task_tracker, &delayed_task_manager); | |
| 66 EXPECT_FALSE(delayed_task_manager.GetDelayedRunTime().is_null()); | |
| 67 } | |
| 68 | |
| 69 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to | |
| 70 // be posted, it doesn't forward it to a SchedulerTaskExecutor. | |
| 71 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { | |
| 72 // Use a strict mock to ensure that the test fails when there is an unexpected | |
| 73 // call to the mock method of |executor|. | |
| 74 testing::StrictMock<MockSchedulerTaskExecutor> executor; | |
| 75 TaskTracker task_tracker; | |
| 76 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 77 task_tracker.Shutdown(); | |
| 78 | |
| 79 PostTaskToExecutor(WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), | |
| 80 TaskTraits(), TimeTicks())), | |
| 81 make_scoped_refptr(new Sequence), &executor, &task_tracker, | |
| 82 &delayed_task_manager); | |
| 83 } | |
| 84 | |
| 85 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty | |
| 86 // sequence, the task is added to the sequence and the sequence is added to the | |
| 87 // priority queue. | |
| 88 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, | |
| 89 PostTaskInEmptySequence) { | |
| 90 std::unique_ptr<Task> task( | |
| 91 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | |
| 92 const Task* task_raw = task.get(); | |
| 93 scoped_refptr<Sequence> sequence(new Sequence); | |
| 94 PriorityQueue priority_queue; | |
| 95 | |
| 96 // Post |task|. | |
| 97 EXPECT_TRUE(AddTaskToSequenceAndPriorityQueue(std::move(task), sequence, | |
| 98 &priority_queue)); | |
| 99 | |
| 100 // Expect to find the sequence in the priority queue. | |
| 101 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); | |
| 102 | |
| 103 // Expect to find |task| alone in |sequence|. | |
| 104 EXPECT_EQ(task_raw, sequence->PeekTask()); | |
| 105 sequence->PopTask(); | |
| 106 EXPECT_EQ(nullptr, sequence->PeekTask()); | |
| 107 } | |
| 108 | |
| 109 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with a | |
| 110 // sequence that already contains a task, the task is added to the sequence but | |
| 111 // the sequence is not added to the priority queue. | |
| 112 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, | |
| 113 PostTaskInNonEmptySequence) { | |
| 114 std::unique_ptr<Task> task( | |
| 115 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | |
| 116 const Task* task_raw = task.get(); | |
| 117 scoped_refptr<Sequence> sequence(new Sequence); | |
| 118 PriorityQueue priority_queue; | |
| 119 | |
| 120 // Add an initial task in |sequence|. | |
| 121 sequence->PushTask(WrapUnique( | |
| 122 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks()))); | |
| 123 | |
| 124 // Post |task|. | |
| 125 EXPECT_FALSE(AddTaskToSequenceAndPriorityQueue(std::move(task), sequence, | |
| 126 &priority_queue)); | |
| 127 | |
| 128 // Expect to find the priority queue empty. | |
| 129 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); | |
| 130 | |
| 131 // Expect to find |task| in |sequence| behind the initial task. | |
| 132 EXPECT_NE(task_raw, sequence->PeekTask()); | |
| 133 sequence->PopTask(); | |
| 134 EXPECT_EQ(task_raw, sequence->PeekTask()); | |
| 135 sequence->PopTask(); | |
| 136 EXPECT_EQ(nullptr, sequence->PeekTask()); | |
| 137 } | |
| 138 | |
| 139 } // namespace | |
| 140 } // namespace internal | |
| 141 } // namespace base | |
|
gab
2016/04/22 18:53:56
Is this functionality already fully covered by the
fdoray
2016/04/22 22:18:14
PostTaskAllowed and PostTaskInEmptySequence are co
| |
| OLD | NEW |