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

Side by Side Diff: base/test/sequenced_task_runner_test_template.h

Issue 1800743003: base: Remove some unnecessary const scoped_refptr<>&. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // SequencedTaskRunnerTest defines tests that implementations of 5 // SequencedTaskRunnerTest defines tests that implementations of
6 // SequencedTaskRunner should pass in order to be conformant. 6 // SequencedTaskRunner should pass in order to be conformant.
7 // See task_runner_test_template.h for a description of how to use the 7 // See task_runner_test_template.h for a description of how to use the
8 // constructs in this file; these work the same. 8 // constructs in this file; these work the same.
9 9
10 #ifndef BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ 10 #ifndef BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_
(...skipping 23 matching lines...) Expand all
34 int i; 34 int i;
35 Type type; 35 Type type;
36 }; 36 };
37 37
38 // Utility class used in the tests below. 38 // Utility class used in the tests below.
39 class SequencedTaskTracker : public RefCountedThreadSafe<SequencedTaskTracker> { 39 class SequencedTaskTracker : public RefCountedThreadSafe<SequencedTaskTracker> {
40 public: 40 public:
41 SequencedTaskTracker(); 41 SequencedTaskTracker();
42 42
43 // Posts the non-nestable task |task|, and records its post event. 43 // Posts the non-nestable task |task|, and records its post event.
44 void PostWrappedNonNestableTask( 44 void PostWrappedNonNestableTask(SequencedTaskRunner* task_runner,
45 const scoped_refptr<SequencedTaskRunner>& task_runner, 45 const Closure& task);
46 const Closure& task);
47 46
48 // Posts the nestable task |task|, and records its post event. 47 // Posts the nestable task |task|, and records its post event.
49 void PostWrappedNestableTask( 48 void PostWrappedNestableTask(SequencedTaskRunner* task_runner,
50 const scoped_refptr<SequencedTaskRunner>& task_runner, 49 const Closure& task);
51 const Closure& task);
52 50
53 // Posts the delayed non-nestable task |task|, and records its post event. 51 // Posts the delayed non-nestable task |task|, and records its post event.
54 void PostWrappedDelayedNonNestableTask( 52 void PostWrappedDelayedNonNestableTask(SequencedTaskRunner* task_runner,
55 const scoped_refptr<SequencedTaskRunner>& task_runner, 53 const Closure& task,
56 const Closure& task, 54 TimeDelta delay);
57 TimeDelta delay);
58 55
59 // Posts |task_count| non-nestable tasks. 56 // Posts |task_count| non-nestable tasks.
60 void PostNonNestableTasks( 57 void PostNonNestableTasks(SequencedTaskRunner* task_runner, int task_count);
61 const scoped_refptr<SequencedTaskRunner>& task_runner,
62 int task_count);
63 58
64 const std::vector<TaskEvent>& GetTaskEvents() const; 59 const std::vector<TaskEvent>& GetTaskEvents() const;
65 60
66 // Returns after the tracker observes a total of |count| task completions. 61 // Returns after the tracker observes a total of |count| task completions.
67 void WaitForCompletedTasks(int count); 62 void WaitForCompletedTasks(int count);
68 63
69 private: 64 private:
70 friend class RefCountedThreadSafe<SequencedTaskTracker>; 65 friend class RefCountedThreadSafe<SequencedTaskTracker>;
71 66
72 ~SequencedTaskTracker(); 67 ~SequencedTaskTracker();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 // overlapping. I.e. that each task starts only after the previously-posted 129 // overlapping. I.e. that each task starts only after the previously-posted
135 // one has finished. 130 // one has finished.
136 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNonNestable) { 131 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNonNestable) {
137 const int kTaskCount = 1000; 132 const int kTaskCount = 1000;
138 133
139 this->delegate_.StartTaskRunner(); 134 this->delegate_.StartTaskRunner();
140 const scoped_refptr<SequencedTaskRunner> task_runner = 135 const scoped_refptr<SequencedTaskRunner> task_runner =
141 this->delegate_.GetTaskRunner(); 136 this->delegate_.GetTaskRunner();
142 137
143 this->task_tracker_->PostWrappedNonNestableTask( 138 this->task_tracker_->PostWrappedNonNestableTask(
144 task_runner, Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1))); 139 task_runner.get(),
140 Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1)));
145 for (int i = 1; i < kTaskCount; ++i) { 141 for (int i = 1; i < kTaskCount; ++i) {
146 this->task_tracker_->PostWrappedNonNestableTask(task_runner, Closure()); 142 this->task_tracker_->PostWrappedNonNestableTask(task_runner.get(),
143 Closure());
147 } 144 }
148 145
149 this->delegate_.StopTaskRunner(); 146 this->delegate_.StopTaskRunner();
150 147
151 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 148 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
152 kTaskCount)); 149 kTaskCount));
153 } 150 }
154 151
155 // This test posts N nestable tasks in sequence. It has the same expectations 152 // This test posts N nestable tasks in sequence. It has the same expectations
156 // as SequentialNonNestable because even though the tasks are nestable, they 153 // as SequentialNonNestable because even though the tasks are nestable, they
157 // will not be run nestedly in this case. 154 // will not be run nestedly in this case.
158 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNestable) { 155 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNestable) {
159 const int kTaskCount = 1000; 156 const int kTaskCount = 1000;
160 157
161 this->delegate_.StartTaskRunner(); 158 this->delegate_.StartTaskRunner();
162 const scoped_refptr<SequencedTaskRunner> task_runner = 159 const scoped_refptr<SequencedTaskRunner> task_runner =
163 this->delegate_.GetTaskRunner(); 160 this->delegate_.GetTaskRunner();
164 161
165 this->task_tracker_->PostWrappedNestableTask( 162 this->task_tracker_->PostWrappedNestableTask(
166 task_runner, 163 task_runner.get(),
167 Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1))); 164 Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1)));
168 for (int i = 1; i < kTaskCount; ++i) { 165 for (int i = 1; i < kTaskCount; ++i) {
169 this->task_tracker_->PostWrappedNestableTask(task_runner, Closure()); 166 this->task_tracker_->PostWrappedNestableTask(task_runner.get(), Closure());
170 } 167 }
171 168
172 this->delegate_.StopTaskRunner(); 169 this->delegate_.StopTaskRunner();
173 170
174 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 171 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
175 kTaskCount)); 172 kTaskCount));
176 } 173 }
177 174
178 // This test posts non-nestable tasks in order of increasing delay, and checks 175 // This test posts non-nestable tasks in order of increasing delay, and checks
179 // that that the tasks are run in FIFO order and that there is no execution 176 // that that the tasks are run in FIFO order and that there is no execution
180 // overlap whatsoever between any two tasks. 177 // overlap whatsoever between any two tasks.
181 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialDelayedNonNestable) { 178 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialDelayedNonNestable) {
182 const int kTaskCount = 20; 179 const int kTaskCount = 20;
183 const int kDelayIncrementMs = 50; 180 const int kDelayIncrementMs = 50;
184 181
185 this->delegate_.StartTaskRunner(); 182 this->delegate_.StartTaskRunner();
186 const scoped_refptr<SequencedTaskRunner> task_runner = 183 const scoped_refptr<SequencedTaskRunner> task_runner =
187 this->delegate_.GetTaskRunner(); 184 this->delegate_.GetTaskRunner();
188 185
189 for (int i = 0; i < kTaskCount; ++i) { 186 for (int i = 0; i < kTaskCount; ++i) {
190 this->task_tracker_->PostWrappedDelayedNonNestableTask( 187 this->task_tracker_->PostWrappedDelayedNonNestableTask(
191 task_runner, 188 task_runner.get(), Closure(),
192 Closure(),
193 TimeDelta::FromMilliseconds(kDelayIncrementMs * i)); 189 TimeDelta::FromMilliseconds(kDelayIncrementMs * i));
194 } 190 }
195 191
196 this->task_tracker_->WaitForCompletedTasks(kTaskCount); 192 this->task_tracker_->WaitForCompletedTasks(kTaskCount);
197 this->delegate_.StopTaskRunner(); 193 this->delegate_.StopTaskRunner();
198 194
199 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 195 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
200 kTaskCount)); 196 kTaskCount));
201 } 197 }
202 198
203 // This test posts a fast, non-nestable task from within each of a number of 199 // This test posts a fast, non-nestable task from within each of a number of
204 // slow, non-nestable tasks and checks that they all run in the sequence they 200 // slow, non-nestable tasks and checks that they all run in the sequence they
205 // were posted in and that there is no execution overlap whatsoever. 201 // were posted in and that there is no execution overlap whatsoever.
206 TYPED_TEST_P(SequencedTaskRunnerTest, NonNestablePostFromNonNestableTask) { 202 TYPED_TEST_P(SequencedTaskRunnerTest, NonNestablePostFromNonNestableTask) {
207 const int kParentCount = 10; 203 const int kParentCount = 10;
208 const int kChildrenPerParent = 10; 204 const int kChildrenPerParent = 10;
209 205
210 this->delegate_.StartTaskRunner(); 206 this->delegate_.StartTaskRunner();
211 const scoped_refptr<SequencedTaskRunner> task_runner = 207 const scoped_refptr<SequencedTaskRunner> task_runner =
212 this->delegate_.GetTaskRunner(); 208 this->delegate_.GetTaskRunner();
213 209
214 for (int i = 0; i < kParentCount; ++i) { 210 for (int i = 0; i < kParentCount; ++i) {
215 Closure task = Bind( 211 Closure task = Bind(
216 &internal::SequencedTaskTracker::PostNonNestableTasks, 212 &internal::SequencedTaskTracker::PostNonNestableTasks,
217 this->task_tracker_, 213 this->task_tracker_,
218 task_runner, 214 task_runner,
219 kChildrenPerParent); 215 kChildrenPerParent);
220 this->task_tracker_->PostWrappedNonNestableTask(task_runner, task); 216 this->task_tracker_->PostWrappedNonNestableTask(task_runner.get(), task);
221 } 217 }
222 218
223 this->delegate_.StopTaskRunner(); 219 this->delegate_.StopTaskRunner();
224 220
225 EXPECT_TRUE(CheckNonNestableInvariants( 221 EXPECT_TRUE(CheckNonNestableInvariants(
226 this->task_tracker_->GetTaskEvents(), 222 this->task_tracker_->GetTaskEvents(),
227 kParentCount * (kChildrenPerParent + 1))); 223 kParentCount * (kChildrenPerParent + 1)));
228 } 224 }
229 225
230 // This test posts two tasks with the same delay, and checks that the tasks are 226 // This test posts two tasks with the same delay, and checks that the tasks are
231 // run in the order in which they were posted. 227 // run in the order in which they were posted.
232 // 228 //
233 // NOTE: This is actually an approximate test since the API only takes a 229 // NOTE: This is actually an approximate test since the API only takes a
234 // "delay" parameter, so we are not exactly simulating two tasks that get 230 // "delay" parameter, so we are not exactly simulating two tasks that get
235 // posted at the exact same time. It would be nice if the API allowed us to 231 // posted at the exact same time. It would be nice if the API allowed us to
236 // specify the desired run time. 232 // specify the desired run time.
237 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTasksSameDelay) { 233 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTasksSameDelay) {
238 const int kTaskCount = 2; 234 const int kTaskCount = 2;
239 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); 235 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
240 236
241 this->delegate_.StartTaskRunner(); 237 this->delegate_.StartTaskRunner();
242 const scoped_refptr<SequencedTaskRunner> task_runner = 238 const scoped_refptr<SequencedTaskRunner> task_runner =
243 this->delegate_.GetTaskRunner(); 239 this->delegate_.GetTaskRunner();
244 240
245 this->task_tracker_->PostWrappedDelayedNonNestableTask( 241 this->task_tracker_->PostWrappedDelayedNonNestableTask(task_runner.get(),
246 task_runner, Closure(), kDelay); 242 Closure(), kDelay);
247 this->task_tracker_->PostWrappedDelayedNonNestableTask( 243 this->task_tracker_->PostWrappedDelayedNonNestableTask(task_runner.get(),
248 task_runner, Closure(), kDelay); 244 Closure(), kDelay);
249 this->task_tracker_->WaitForCompletedTasks(kTaskCount); 245 this->task_tracker_->WaitForCompletedTasks(kTaskCount);
250 this->delegate_.StopTaskRunner(); 246 this->delegate_.StopTaskRunner();
251 247
252 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 248 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
253 kTaskCount)); 249 kTaskCount));
254 } 250 }
255 251
256 // This test posts a normal task and a delayed task, and checks that the 252 // This test posts a normal task and a delayed task, and checks that the
257 // delayed task runs after the normal task even if the normal task takes 253 // delayed task runs after the normal task even if the normal task takes
258 // a long time to run. 254 // a long time to run.
259 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterLongTask) { 255 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterLongTask) {
260 const int kTaskCount = 2; 256 const int kTaskCount = 2;
261 257
262 this->delegate_.StartTaskRunner(); 258 this->delegate_.StartTaskRunner();
263 const scoped_refptr<SequencedTaskRunner> task_runner = 259 const scoped_refptr<SequencedTaskRunner> task_runner =
264 this->delegate_.GetTaskRunner(); 260 this->delegate_.GetTaskRunner();
265 261
266 this->task_tracker_->PostWrappedNonNestableTask( 262 this->task_tracker_->PostWrappedNonNestableTask(
267 task_runner, base::Bind(&PlatformThread::Sleep, 263 task_runner.get(),
268 TimeDelta::FromMilliseconds(50))); 264 base::Bind(&PlatformThread::Sleep, TimeDelta::FromMilliseconds(50)));
269 this->task_tracker_->PostWrappedDelayedNonNestableTask( 265 this->task_tracker_->PostWrappedDelayedNonNestableTask(
270 task_runner, Closure(), TimeDelta::FromMilliseconds(10)); 266 task_runner.get(), Closure(), TimeDelta::FromMilliseconds(10));
271 this->task_tracker_->WaitForCompletedTasks(kTaskCount); 267 this->task_tracker_->WaitForCompletedTasks(kTaskCount);
272 this->delegate_.StopTaskRunner(); 268 this->delegate_.StopTaskRunner();
273 269
274 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 270 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
275 kTaskCount)); 271 kTaskCount));
276 } 272 }
277 273
278 // Test that a pile of normal tasks and a delayed task run in the 274 // Test that a pile of normal tasks and a delayed task run in the
279 // time-to-run order. 275 // time-to-run order.
280 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterManyLongTasks) { 276 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterManyLongTasks) {
281 const int kTaskCount = 11; 277 const int kTaskCount = 11;
282 278
283 this->delegate_.StartTaskRunner(); 279 this->delegate_.StartTaskRunner();
284 const scoped_refptr<SequencedTaskRunner> task_runner = 280 const scoped_refptr<SequencedTaskRunner> task_runner =
285 this->delegate_.GetTaskRunner(); 281 this->delegate_.GetTaskRunner();
286 282
287 for (int i = 0; i < kTaskCount - 1; i++) { 283 for (int i = 0; i < kTaskCount - 1; i++) {
288 this->task_tracker_->PostWrappedNonNestableTask( 284 this->task_tracker_->PostWrappedNonNestableTask(
289 task_runner, base::Bind(&PlatformThread::Sleep, 285 task_runner.get(),
290 TimeDelta::FromMilliseconds(50))); 286 base::Bind(&PlatformThread::Sleep, TimeDelta::FromMilliseconds(50)));
291 } 287 }
292 this->task_tracker_->PostWrappedDelayedNonNestableTask( 288 this->task_tracker_->PostWrappedDelayedNonNestableTask(
293 task_runner, Closure(), TimeDelta::FromMilliseconds(10)); 289 task_runner.get(), Closure(), TimeDelta::FromMilliseconds(10));
294 this->task_tracker_->WaitForCompletedTasks(kTaskCount); 290 this->task_tracker_->WaitForCompletedTasks(kTaskCount);
295 this->delegate_.StopTaskRunner(); 291 this->delegate_.StopTaskRunner();
296 292
297 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 293 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
298 kTaskCount)); 294 kTaskCount));
299 } 295 }
300 296
301 297
302 // TODO(francoisk777@gmail.com) Add a test, similiar to the above, which runs 298 // TODO(francoisk777@gmail.com) Add a test, similiar to the above, which runs
303 // some tasked nestedly (which should be implemented in the test 299 // some tasked nestedly (which should be implemented in the test
(...skipping 22 matching lines...) Expand all
326 // the specified time. 322 // the specified time.
327 TYPED_TEST_P(SequencedTaskRunnerDelayedTest, DelayedTaskBasic) { 323 TYPED_TEST_P(SequencedTaskRunnerDelayedTest, DelayedTaskBasic) {
328 const int kTaskCount = 1; 324 const int kTaskCount = 1;
329 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); 325 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
330 326
331 this->delegate_.StartTaskRunner(); 327 this->delegate_.StartTaskRunner();
332 const scoped_refptr<SequencedTaskRunner> task_runner = 328 const scoped_refptr<SequencedTaskRunner> task_runner =
333 this->delegate_.GetTaskRunner(); 329 this->delegate_.GetTaskRunner();
334 330
335 Time time_before_run = Time::Now(); 331 Time time_before_run = Time::Now();
336 this->task_tracker_->PostWrappedDelayedNonNestableTask(task_runner, Closure(), 332 this->task_tracker_->PostWrappedDelayedNonNestableTask(task_runner.get(),
337 kDelay); 333 Closure(), kDelay);
338 this->task_tracker_->WaitForCompletedTasks(kTaskCount); 334 this->task_tracker_->WaitForCompletedTasks(kTaskCount);
339 this->delegate_.StopTaskRunner(); 335 this->delegate_.StopTaskRunner();
340 Time time_after_run = Time::Now(); 336 Time time_after_run = Time::Now();
341 337
342 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), 338 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(),
343 kTaskCount)); 339 kTaskCount));
344 EXPECT_LE(kDelay, time_after_run - time_before_run); 340 EXPECT_LE(kDelay, time_after_run - time_before_run);
345 } 341 }
346 342
347 // SequencedTaskRunnerDelayedTest tests that the |delay| parameter of 343 // SequencedTaskRunnerDelayedTest tests that the |delay| parameter of
348 // is used to actually wait for |delay| ms before executing the task. 344 // is used to actually wait for |delay| ms before executing the task.
349 // This is not mandatory for a SequencedTaskRunner to be compliant. 345 // This is not mandatory for a SequencedTaskRunner to be compliant.
350 REGISTER_TYPED_TEST_CASE_P(SequencedTaskRunnerDelayedTest, DelayedTaskBasic); 346 REGISTER_TYPED_TEST_CASE_P(SequencedTaskRunnerDelayedTest, DelayedTaskBasic);
351 347
352 } // namespace base 348 } // namespace base
353 349
354 #endif // BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ 350 #endif // BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698