| 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/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/task_scheduler/delayed_task_manager.h" |
| 14 #include "base/task_scheduler/priority_queue.h" | 15 #include "base/task_scheduler/priority_queue.h" |
| 15 #include "base/task_scheduler/scheduler_task_executor.h" | 16 #include "base/task_scheduler/scheduler_task_executor.h" |
| 16 #include "base/task_scheduler/sequence.h" | 17 #include "base/task_scheduler/sequence.h" |
| 17 #include "base/task_scheduler/task.h" | 18 #include "base/task_scheduler/task.h" |
| 18 #include "base/task_scheduler/task_tracker.h" | 19 #include "base/task_scheduler/task_tracker.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 | 24 |
| 24 bool operator==(const Closure& lhs, const Closure& rhs) { | 25 bool operator==(const Closure& lhs, const Closure& rhs) { |
| 25 return lhs.Equals(rhs); | 26 return lhs.Equals(rhs); |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace internal { | 29 namespace internal { |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { | 32 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { |
| 32 public: | 33 public: |
| 33 void PostTaskWithSequence(std::unique_ptr<Task> task, | 34 void PostTaskWithSequence(std::unique_ptr<Task> task, |
| 34 scoped_refptr<Sequence> sequence) override { | 35 scoped_refptr<Sequence> sequence) override { |
| 35 PostTaskWithSequenceMock(task->task, task->traits, sequence.get()); | 36 PostTaskWithSequenceMock(task->task, task->traits, sequence.get()); |
| 36 } | 37 } |
| 37 | 38 |
| 38 MOCK_METHOD3(PostTaskWithSequenceMock, | 39 MOCK_METHOD3(PostTaskWithSequenceMock, |
| 39 void(const Closure&, const TaskTraits&, Sequence* sequence)); | 40 void(const Closure&, const TaskTraits&, Sequence* sequence)); |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 // Verifies that when PostTaskToExecutor receives a Task that is allowed to be | 43 // Verifies that when PostTaskToExecutor receives a non-delayed Task that is |
| 43 // posted, it forwards it to a SchedulerTaskExecutor. | 44 // allowed to be posted, it forwards it to a SchedulerTaskExecutor. |
| 44 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskAllowed) { | 45 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskAllowed) { |
| 45 const Closure closure(Bind(&DoNothing)); | 46 const Closure closure(Bind(&DoNothing)); |
| 46 const TaskTraits traits; | 47 const TaskTraits traits; |
| 47 scoped_refptr<Sequence> sequence(new Sequence); | 48 scoped_refptr<Sequence> sequence(new Sequence); |
| 48 testing::StrictMock<MockSchedulerTaskExecutor> executor; | 49 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 49 TaskTracker task_tracker; | 50 TaskTracker task_tracker; |
| 51 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); |
| 50 | 52 |
| 51 EXPECT_CALL(executor, | 53 EXPECT_CALL(executor, |
| 52 PostTaskWithSequenceMock(closure, traits, sequence.get())); | 54 PostTaskWithSequenceMock(closure, traits, sequence.get())); |
| 53 PostTaskToExecutor(FROM_HERE, closure, traits, TimeDelta(), sequence, | 55 PostTaskToExecutor(FROM_HERE, closure, traits, TimeDelta(), sequence, |
| 54 &executor, &task_tracker); | 56 &executor, &task_tracker, &delayed_task_manager); |
| 57 } |
| 58 |
| 59 // Verifies that when PostTaskToExecutor receives a delayed Task that is allowed |
| 60 // to be posted, it forwards it to a DelayedTaskManager. |
| 61 TEST(TaskSchedulerPostTaskToExecutorTest, PostDelayedTaskAllowed) { |
| 62 const Closure closure(Bind(&DoNothing)); |
| 63 const TaskTraits traits; |
| 64 scoped_refptr<Sequence> sequence(new Sequence); |
| 65 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 66 TaskTracker task_tracker; |
| 67 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); |
| 68 |
| 69 EXPECT_TRUE(delayed_task_manager.GetDelayedRunTime().is_null()); |
| 70 PostTaskToExecutor(FROM_HERE, closure, traits, TimeDelta::FromSeconds(10), |
| 71 sequence, &executor, &task_tracker, &delayed_task_manager); |
| 72 EXPECT_FALSE(delayed_task_manager.GetDelayedRunTime().is_null()); |
| 55 } | 73 } |
| 56 | 74 |
| 57 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to | 75 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to |
| 58 // be posted, it doesn't forward it to a SchedulerTaskExecutor. | 76 // be posted, it doesn't forward it to a SchedulerTaskExecutor. |
| 59 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { | 77 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { |
| 60 // Use a strict mock to ensure that the test fails when there is an unexpected | 78 // Use a strict mock to ensure that the test fails when there is an unexpected |
| 61 // call to the mock method of |executor|. | 79 // call to the mock method of |executor|. |
| 62 testing::StrictMock<MockSchedulerTaskExecutor> executor; | 80 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 63 TaskTracker task_tracker; | 81 TaskTracker task_tracker; |
| 82 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); |
| 64 task_tracker.Shutdown(); | 83 task_tracker.Shutdown(); |
| 65 | 84 |
| 66 PostTaskToExecutor( | 85 PostTaskToExecutor( |
| 67 FROM_HERE, Bind(&DoNothing), | 86 FROM_HERE, Bind(&DoNothing), |
| 68 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | 87 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 69 TimeDelta(), make_scoped_refptr(new Sequence), &executor, &task_tracker); | 88 TimeDelta(), make_scoped_refptr(new Sequence), &executor, &task_tracker, |
| 89 &delayed_task_manager); |
| 70 } | 90 } |
| 71 | 91 |
| 72 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty | 92 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty |
| 73 // sequence, the task is added to the sequence and the sequence is added to the | 93 // sequence, the task is added to the sequence and the sequence is added to the |
| 74 // priority queue. | 94 // priority queue. |
| 75 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, | 95 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, |
| 76 PostTaskInEmptySequence) { | 96 PostTaskInEmptySequence) { |
| 77 std::unique_ptr<Task> task( | 97 std::unique_ptr<Task> task( |
| 78 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); | 98 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks())); |
| 79 const Task* task_raw = task.get(); | 99 const Task* task_raw = task.get(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 EXPECT_NE(task_raw, sequence->PeekTask()); | 139 EXPECT_NE(task_raw, sequence->PeekTask()); |
| 120 sequence->PopTask(); | 140 sequence->PopTask(); |
| 121 EXPECT_EQ(task_raw, sequence->PeekTask()); | 141 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 122 sequence->PopTask(); | 142 sequence->PopTask(); |
| 123 EXPECT_EQ(nullptr, sequence->PeekTask()); | 143 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 124 } | 144 } |
| 125 | 145 |
| 126 } // namespace | 146 } // namespace |
| 127 } // namespace internal | 147 } // namespace internal |
| 128 } // namespace base | 148 } // namespace base |
| OLD | NEW |