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/delayed_task_manager.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/task_scheduler/scheduler_lock.h" |
| 14 #include "base/task_scheduler/scheduler_task_executor.h" |
| 15 #include "base/task_scheduler/sequence.h" |
| 16 #include "base/task_scheduler/task.h" |
| 17 #include "base/time/time.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace base { |
| 22 namespace internal { |
| 23 namespace { |
| 24 |
| 25 class TestDelayedTaskManager : public DelayedTaskManager { |
| 26 public: |
| 27 TestDelayedTaskManager() |
| 28 : DelayedTaskManager( |
| 29 Bind(&TestDelayedTaskManager::OnDelayedRunTimeUpdated, |
| 30 Unretained(this))) {} |
| 31 |
| 32 void SetCurrentTime(TimeTicks now) { now_ = now; } |
| 33 |
| 34 // DelayedTaskManager: |
| 35 TimeTicks Now() const override { return now_; } |
| 36 |
| 37 MOCK_METHOD0(OnDelayedRunTimeUpdated, void()); |
| 38 |
| 39 private: |
| 40 TimeTicks now_ = TimeTicks::Now(); |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); |
| 43 }; |
| 44 |
| 45 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { |
| 46 public: |
| 47 void PostTaskWithSequence(std::unique_ptr<Task> task, |
| 48 scoped_refptr<Sequence> sequence) override { |
| 49 PostTaskWithSequenceMock(task.get(), sequence.get()); |
| 50 } |
| 51 |
| 52 MOCK_METHOD2(PostTaskWithSequenceMock, void(const Task*, const Sequence*)); |
| 53 }; |
| 54 |
| 55 // Verify that GetDelayedRunTime() returns a null TimeTicks when there are |
| 56 // no pending delayed tasks. |
| 57 TEST(TaskSchedulerDelayedTaskManagerTest, |
| 58 GetDelayedRunTimeNoPendingDelayedTasks) { |
| 59 TestDelayedTaskManager manager; |
| 60 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 61 } |
| 62 |
| 63 // Verify that a delayed task isn't posted before it is ripe for execution. |
| 64 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { |
| 65 testing::StrictMock<TestDelayedTaskManager> manager; |
| 66 |
| 67 std::unique_ptr<Task> task( |
| 68 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 69 manager.Now() + TimeDelta::FromSeconds(1))); |
| 70 const Task* task_raw = task.get(); |
| 71 scoped_refptr<Sequence> sequence(new Sequence); |
| 72 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 73 |
| 74 // Add |task| to the DelayedTaskManager. |
| 75 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 76 manager.AddDelayedTask(std::move(task), sequence, &executor); |
| 77 testing::Mock::VerifyAndClear(&manager); |
| 78 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 79 |
| 80 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't |
| 81 // expect any call to the mock method of |executor|. |
| 82 manager.PostReadyTasks(); |
| 83 |
| 84 // The delayed run time shouldn't have changed. |
| 85 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 86 } |
| 87 |
| 88 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
| 89 // current time equal to the task's delayed run time. |
| 90 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { |
| 91 testing::StrictMock<TestDelayedTaskManager> manager; |
| 92 |
| 93 std::unique_ptr<Task> task( |
| 94 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 95 manager.Now() + TimeDelta::FromSeconds(1))); |
| 96 const Task* task_raw = task.get(); |
| 97 scoped_refptr<Sequence> sequence(new Sequence); |
| 98 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 99 |
| 100 // Add |task| to the DelayedTaskManager. |
| 101 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 102 manager.AddDelayedTask(std::move(task), sequence, &executor); |
| 103 testing::Mock::VerifyAndClear(&manager); |
| 104 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 105 |
| 106 // Fast-forward time to |task_raw|'s delayed run time. |
| 107 manager.SetCurrentTime(task_raw->delayed_run_time); |
| 108 |
| 109 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 110 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_raw, sequence.get())); |
| 111 manager.PostReadyTasks(); |
| 112 testing::Mock::VerifyAndClear(&manager); |
| 113 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 114 } |
| 115 |
| 116 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
| 117 // current time greater than the task's delayed run time. |
| 118 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { |
| 119 testing::StrictMock<TestDelayedTaskManager> manager; |
| 120 |
| 121 std::unique_ptr<Task> task( |
| 122 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 123 manager.Now() + TimeDelta::FromSeconds(1))); |
| 124 const Task* task_raw = task.get(); |
| 125 scoped_refptr<Sequence> sequence(new Sequence); |
| 126 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 127 |
| 128 // Add |task| to the DelayedTaskManager. |
| 129 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 130 manager.AddDelayedTask(std::move(task), sequence, &executor); |
| 131 testing::Mock::VerifyAndClear(&manager); |
| 132 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 133 |
| 134 // Fast-forward time to |task_raw|'s delayed run time. |
| 135 manager.SetCurrentTime(task_raw->delayed_run_time + |
| 136 TimeDelta::FromSeconds(10)); |
| 137 |
| 138 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 139 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_raw, sequence.get())); |
| 140 manager.PostReadyTasks(); |
| 141 testing::Mock::VerifyAndClear(&manager); |
| 142 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 143 } |
| 144 |
| 145 // Verify that when multiple tasks are added to a DelayedTaskManager, they are |
| 146 // posted when they become ripe for execution. |
| 147 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { |
| 148 testing::StrictMock<TestDelayedTaskManager> manager; |
| 149 |
| 150 scoped_refptr<Sequence> sequence(new Sequence); |
| 151 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 152 |
| 153 std::unique_ptr<Task> task_a( |
| 154 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 155 manager.Now() + TimeDelta::FromSeconds(2))); |
| 156 const Task* task_a_raw = task_a.get(); |
| 157 |
| 158 std::unique_ptr<Task> task_b( |
| 159 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 160 manager.Now() + TimeDelta::FromSeconds(2))); |
| 161 const Task* task_b_raw = task_b.get(); |
| 162 |
| 163 std::unique_ptr<Task> task_c( |
| 164 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 165 manager.Now() + TimeDelta::FromSeconds(1))); |
| 166 const Task* task_c_raw = task_c.get(); |
| 167 |
| 168 // Add |task_a| to the DelayedTaskManager. The delayed run time should be |
| 169 // updated to |task_a|'s delayed run time. |
| 170 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 171 manager.AddDelayedTask(std::move(task_a), sequence, &executor); |
| 172 testing::Mock::VerifyAndClear(&manager); |
| 173 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 174 |
| 175 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't |
| 176 // change. |
| 177 manager.AddDelayedTask(std::move(task_b), sequence, &executor); |
| 178 testing::Mock::VerifyAndClear(&manager); |
| 179 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 180 |
| 181 // Add |task_c| to the DelayedTaskManager. The delayed run time should be |
| 182 // updated to |task_c|'s delayed run time. |
| 183 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 184 manager.AddDelayedTask(std::move(task_c), sequence, &executor); |
| 185 testing::Mock::VerifyAndClear(&manager); |
| 186 EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 187 |
| 188 // Fast-forward time to |task_c_raw|'s delayed run time. |
| 189 manager.SetCurrentTime(task_c_raw->delayed_run_time); |
| 190 |
| 191 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 192 // |task_c_raw| should be posted and the delayed run time should become |
| 193 // |task_a_raw|'s delayed run time. |
| 194 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_c_raw, sequence.get())); |
| 195 manager.PostReadyTasks(); |
| 196 testing::Mock::VerifyAndClear(&executor); |
| 197 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 198 |
| 199 // Fast-forward time to |task_a_raw|'s delayed run time. |
| 200 manager.SetCurrentTime(task_a_raw->delayed_run_time); |
| 201 |
| 202 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 203 // |task_a_raw| and |task_b_raw| should be posted and the delayed run time |
| 204 // should become a null TimeTicks. |
| 205 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_a_raw, sequence.get())); |
| 206 EXPECT_CALL(executor, PostTaskWithSequenceMock(task_b_raw, sequence.get())); |
| 207 manager.PostReadyTasks(); |
| 208 testing::Mock::VerifyAndClear(&executor); |
| 209 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 210 } |
| 211 |
| 212 } // namespace |
| 213 } // namespace internal |
| 214 } // namespace base |
OLD | NEW |