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