| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/task_scheduler/utils.h" | 5 #include "base/task_scheduler/utils.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/task_scheduler/delayed_task_manager.h" |
| 13 #include "base/task_scheduler/priority_queue.h" | 14 #include "base/task_scheduler/priority_queue.h" |
| 14 #include "base/task_scheduler/scheduler_task_executor.h" | 15 #include "base/task_scheduler/scheduler_task_executor.h" |
| 15 #include "base/task_scheduler/sequence.h" | 16 #include "base/task_scheduler/sequence.h" |
| 16 #include "base/task_scheduler/task.h" | 17 #include "base/task_scheduler/task.h" |
| 17 #include "base/task_scheduler/task_tracker.h" | 18 #include "base/task_scheduler/task_tracker.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 namespace internal { | 23 namespace internal { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { | 26 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { |
| 26 public: | 27 public: |
| 27 void PostTaskWithSequence(std::unique_ptr<Task> task, | 28 void PostTaskWithSequence(std::unique_ptr<Task> task, |
| 28 scoped_refptr<Sequence> sequence) override { | 29 scoped_refptr<Sequence> sequence) override { |
| 29 PostTaskWithSequenceMock(task.get(), sequence.get()); | 30 PostTaskWithSequenceMock(task.get(), sequence.get()); |
| 30 } | 31 } |
| 31 | 32 |
| 32 MOCK_METHOD2(PostTaskWithSequenceMock, | 33 MOCK_METHOD2(PostTaskWithSequenceMock, |
| 33 void(const Task* task, const Sequence* sequence)); | 34 void(const Task* task, const Sequence* sequence)); |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 // Verifies that when PostTaskToExecutor receives a Task that is allowed to be | 37 // Verifies that when PostTaskToExecutor receives a non-delayed Task that is |
| 37 // posted, it forwards it to a SchedulerTaskExecutor. | 38 // allowed to be posted, it forwards it to a SchedulerTaskExecutor. |
| 38 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskAllowed) { | 39 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskAllowed) { |
| 39 std::unique_ptr<Task> task( | 40 std::unique_ptr<Task> task( |
| 40 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | 41 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); |
| 41 const Task* task_raw = task.get(); | 42 const Task* task_raw = task.get(); |
| 42 scoped_refptr<Sequence> sequence(new Sequence); | 43 scoped_refptr<Sequence> sequence(new Sequence); |
| 43 testing::StrictMock<MockSchedulerTaskExecutor> executor; | 44 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 44 TaskTracker task_tracker; | 45 TaskTracker task_tracker; |
| 46 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); |
| 45 | 47 |
| 46 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_raw, sequence.get())); | 48 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_raw, sequence.get())); |
| 47 PostTaskToExecutor(std::move(task), sequence, &executor, &task_tracker); | 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()); |
| 48 } | 67 } |
| 49 | 68 |
| 50 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to | 69 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to |
| 51 // be posted, it doesn't forward it to a SchedulerTaskExecutor. | 70 // be posted, it doesn't forward it to a SchedulerTaskExecutor. |
| 52 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { | 71 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { |
| 53 // Use a strict mock to ensure that the test fails when there is an unexpected | 72 // Use a strict mock to ensure that the test fails when there is an unexpected |
| 54 // call to the mock method of |executor|. | 73 // call to the mock method of |executor|. |
| 55 testing::StrictMock<MockSchedulerTaskExecutor> executor; | 74 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 56 TaskTracker task_tracker; | 75 TaskTracker task_tracker; |
| 76 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); |
| 57 task_tracker.Shutdown(); | 77 task_tracker.Shutdown(); |
| 58 | 78 |
| 59 PostTaskToExecutor(WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), | 79 PostTaskToExecutor(WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), |
| 60 TaskTraits(), TimeTicks())), | 80 TaskTraits(), TimeTicks())), |
| 61 make_scoped_refptr(new Sequence), &executor, | 81 make_scoped_refptr(new Sequence), &executor, &task_tracker, |
| 62 &task_tracker); | 82 &delayed_task_manager); |
| 63 } | 83 } |
| 64 | 84 |
| 65 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty | 85 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty |
| 66 // sequence, the task is added to the sequence and the sequence is added to the | 86 // sequence, the task is added to the sequence and the sequence is added to the |
| 67 // priority queue. | 87 // priority queue. |
| 68 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, | 88 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, |
| 69 PostTaskInEmptySequence) { | 89 PostTaskInEmptySequence) { |
| 70 std::unique_ptr<Task> task( | 90 std::unique_ptr<Task> task( |
| 71 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | 91 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); |
| 72 const Task* task_raw = task.get(); | 92 const Task* task_raw = task.get(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 EXPECT_NE(task_raw, sequence->PeekTask()); | 132 EXPECT_NE(task_raw, sequence->PeekTask()); |
| 113 sequence->PopTask(); | 133 sequence->PopTask(); |
| 114 EXPECT_EQ(task_raw, sequence->PeekTask()); | 134 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 115 sequence->PopTask(); | 135 sequence->PopTask(); |
| 116 EXPECT_EQ(nullptr, sequence->PeekTask()); | 136 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 117 } | 137 } |
| 118 | 138 |
| 119 } // namespace | 139 } // namespace |
| 120 } // namespace internal | 140 } // namespace internal |
| 121 } // namespace base | 141 } // namespace base |
| OLD | NEW |