Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: base/task_scheduler/delayed_task_manager_unittest.cc

Issue 1906813003: TaskScheduler: TimeDelta instead of TimeTicks in Task's constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sched_2c_owned_delegate
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/task_scheduler/priority_queue_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 GetDelayedRunTimeNoPendingDelayedTasks) { 80 GetDelayedRunTimeNoPendingDelayedTasks) {
81 TestDelayedTaskManager manager; 81 TestDelayedTaskManager manager;
82 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); 82 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
83 } 83 }
84 84
85 // Verify that a delayed task isn't posted before it is ripe for execution. 85 // Verify that a delayed task isn't posted before it is ripe for execution.
86 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { 86 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) {
87 testing::StrictMock<TestDelayedTaskManager> manager; 87 testing::StrictMock<TestDelayedTaskManager> manager;
88 88
89 std::unique_ptr<Task> task( 89 std::unique_ptr<Task> task(
90 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 90 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
91 manager.Now() + TimeDelta::FromSeconds(1))); 91 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
92 const Task* task_raw = task.get(); 92 const Task* task_raw = task.get();
93 scoped_refptr<Sequence> sequence(new Sequence); 93 scoped_refptr<Sequence> sequence(new Sequence);
94 testing::StrictMock<MockSchedulerThreadPool> thread_pool; 94 testing::StrictMock<MockSchedulerThreadPool> thread_pool;
95 95
96 // Add |task| to the DelayedTaskManager. 96 // Add |task| to the DelayedTaskManager.
97 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); 97 EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
98 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); 98 manager.AddDelayedTask(std::move(task), sequence, &thread_pool);
99 testing::Mock::VerifyAndClear(&manager); 99 testing::Mock::VerifyAndClear(&manager);
100 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); 100 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
101 101
102 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't 102 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't
103 // expect any call to the mock method of |thread_pool|. 103 // expect any call to the mock method of |thread_pool|.
104 manager.PostReadyTasks(); 104 manager.PostReadyTasks();
105 105
106 // The delayed run time shouldn't have changed. 106 // The delayed run time shouldn't have changed.
107 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); 107 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
108 } 108 }
109 109
110 // Verify that a delayed task is posted when PostReadyTasks() is called with the 110 // Verify that a delayed task is posted when PostReadyTasks() is called with the
111 // current time equal to the task's delayed run time. 111 // current time equal to the task's delayed run time.
112 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { 112 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) {
113 testing::StrictMock<TestDelayedTaskManager> manager; 113 testing::StrictMock<TestDelayedTaskManager> manager;
114 114
115 std::unique_ptr<Task> task( 115 std::unique_ptr<Task> task(
116 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 116 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
117 manager.Now() + TimeDelta::FromSeconds(1))); 117 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
118 const Task* task_raw = task.get(); 118 const Task* task_raw = task.get();
119 scoped_refptr<Sequence> sequence(new Sequence); 119 scoped_refptr<Sequence> sequence(new Sequence);
120 testing::StrictMock<MockSchedulerThreadPool> thread_pool; 120 testing::StrictMock<MockSchedulerThreadPool> thread_pool;
121 121
122 // Add |task| to the DelayedTaskManager. 122 // Add |task| to the DelayedTaskManager.
123 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); 123 EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
124 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); 124 manager.AddDelayedTask(std::move(task), sequence, &thread_pool);
125 testing::Mock::VerifyAndClear(&manager); 125 testing::Mock::VerifyAndClear(&manager);
126 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); 126 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
127 127
128 // Fast-forward time to |task_raw|'s delayed run time. 128 // Fast-forward time to |task_raw|'s delayed run time.
129 manager.SetCurrentTime(task_raw->delayed_run_time); 129 manager.SetCurrentTime(task_raw->delayed_run_time);
130 130
131 // Ask the DelayedTaskManager to post tasks that are ripe for execution. 131 // Ask the DelayedTaskManager to post tasks that are ripe for execution.
132 EXPECT_CALL(thread_pool, 132 EXPECT_CALL(thread_pool,
133 PostTaskWithSequenceNowMock(task_raw, sequence.get())); 133 PostTaskWithSequenceNowMock(task_raw, sequence.get()));
134 manager.PostReadyTasks(); 134 manager.PostReadyTasks();
135 testing::Mock::VerifyAndClear(&manager); 135 testing::Mock::VerifyAndClear(&manager);
136 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); 136 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
137 } 137 }
138 138
139 // Verify that a delayed task is posted when PostReadyTasks() is called with the 139 // Verify that a delayed task is posted when PostReadyTasks() is called with the
140 // current time greater than the task's delayed run time. 140 // current time greater than the task's delayed run time.
141 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { 141 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) {
142 testing::StrictMock<TestDelayedTaskManager> manager; 142 testing::StrictMock<TestDelayedTaskManager> manager;
143 143
144 std::unique_ptr<Task> task( 144 std::unique_ptr<Task> task(
145 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 145 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
146 manager.Now() + TimeDelta::FromSeconds(1))); 146 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
147 const Task* task_raw = task.get(); 147 const Task* task_raw = task.get();
148 scoped_refptr<Sequence> sequence(new Sequence); 148 scoped_refptr<Sequence> sequence(new Sequence);
149 testing::StrictMock<MockSchedulerThreadPool> thread_pool; 149 testing::StrictMock<MockSchedulerThreadPool> thread_pool;
150 150
151 // Add |task| to the DelayedTaskManager. 151 // Add |task| to the DelayedTaskManager.
152 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); 152 EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
153 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); 153 manager.AddDelayedTask(std::move(task), sequence, &thread_pool);
154 testing::Mock::VerifyAndClear(&manager); 154 testing::Mock::VerifyAndClear(&manager);
155 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); 155 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
156 156
(...skipping 11 matching lines...) Expand all
168 168
169 // Verify that when multiple tasks are added to a DelayedTaskManager, they are 169 // Verify that when multiple tasks are added to a DelayedTaskManager, they are
170 // posted when they become ripe for execution. 170 // posted when they become ripe for execution.
171 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { 171 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) {
172 testing::StrictMock<TestDelayedTaskManager> manager; 172 testing::StrictMock<TestDelayedTaskManager> manager;
173 173
174 scoped_refptr<Sequence> sequence(new Sequence); 174 scoped_refptr<Sequence> sequence(new Sequence);
175 testing::StrictMock<MockSchedulerThreadPool> thread_pool; 175 testing::StrictMock<MockSchedulerThreadPool> thread_pool;
176 176
177 std::unique_ptr<Task> task_a( 177 std::unique_ptr<Task> task_a(
178 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 178 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
179 manager.Now() + TimeDelta::FromSeconds(2))); 179 task_a->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2);
180 const Task* task_a_raw = task_a.get(); 180 const Task* task_a_raw = task_a.get();
181 181
182 std::unique_ptr<Task> task_b( 182 std::unique_ptr<Task> task_b(
183 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 183 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
184 manager.Now() + TimeDelta::FromSeconds(2))); 184 task_b->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2);
185 const Task* task_b_raw = task_b.get(); 185 const Task* task_b_raw = task_b.get();
186 186
187 std::unique_ptr<Task> task_c( 187 std::unique_ptr<Task> task_c(
188 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), 188 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
189 manager.Now() + TimeDelta::FromSeconds(1))); 189 task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
190 const Task* task_c_raw = task_c.get(); 190 const Task* task_c_raw = task_c.get();
191 191
192 // Add |task_a| to the DelayedTaskManager. The delayed run time should be 192 // Add |task_a| to the DelayedTaskManager. The delayed run time should be
193 // updated to |task_a|'s delayed run time. 193 // updated to |task_a|'s delayed run time.
194 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); 194 EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
195 manager.AddDelayedTask(std::move(task_a), sequence, &thread_pool); 195 manager.AddDelayedTask(std::move(task_a), sequence, &thread_pool);
196 testing::Mock::VerifyAndClear(&manager); 196 testing::Mock::VerifyAndClear(&manager);
197 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); 197 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime());
198 198
199 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't 199 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 PostTaskWithSequenceNowMock(task_a_raw, sequence.get())); 231 PostTaskWithSequenceNowMock(task_a_raw, sequence.get()));
232 EXPECT_CALL(thread_pool, 232 EXPECT_CALL(thread_pool,
233 PostTaskWithSequenceNowMock(task_b_raw, sequence.get())); 233 PostTaskWithSequenceNowMock(task_b_raw, sequence.get()));
234 manager.PostReadyTasks(); 234 manager.PostReadyTasks();
235 testing::Mock::VerifyAndClear(&thread_pool); 235 testing::Mock::VerifyAndClear(&thread_pool);
236 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); 236 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
237 } 237 }
238 238
239 } // namespace internal 239 } // namespace internal
240 } // namespace base 240 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/task_scheduler/priority_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698