1 // Copyright 2014 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.
7 #include "base/cancelable_callback.h"
8 #include "base/format_macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/test/test_pending_task.h"
12 #include "cc/test/ordered_simple_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // We pass EXPECT_TRUE / EXPECT_FALSE macros rather than a boolean as on some
16 // compilers EXPECT_EQ(false, XXXX) fails to compile as gtest tries to convert
17 // the false value to null causing a -Werror=conversion-null error.
18 #define RUN_AND_CHECK_RESULT( \
19 tasks_remain_expect_macro, run_func, expected_result) \
20 tasks_remain_expect_macro(task_runner_->run_func); \
21 EXPECT_EQ(expected_result, executed_tasks_); \
37 TEST(TestOrderablePendingTask
, Ordering
) {
38 TestOrderablePendingTask a
;
39 TestOrderablePendingTask b
;
40 TestOrderablePendingTask c
;
49 TestOrderablePendingTask a2
= a
;
56 class OrderedSimpleTaskRunnerTest
: public testing::Test
{
58 OrderedSimpleTaskRunnerTest()
59 : now_src_(TestNowSource::Create(base::TimeTicks())) {
60 task_runner_
= new OrderedSimpleTaskRunner(now_src_
, true);
62 ~OrderedSimpleTaskRunnerTest() override
{}
65 std::string executed_tasks_
;
66 scoped_refptr
<TestNowSource
> now_src_
;
67 scoped_refptr
<OrderedSimpleTaskRunner
> task_runner_
;
69 void PostTask(int task_num
, base::TimeDelta delay
) {
70 base::Closure test_task
= base::Bind(&OrderedSimpleTaskRunnerTest::Task
,
71 base::Unretained(this),
73 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay
);
76 void PostTaskWhichPostsInstantTask(int task_num
, base::TimeDelta delay
) {
77 base::Closure test_task
=
78 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsInstantTask
,
79 base::Unretained(this),
81 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay
);
84 void PostTaskWhichPostsDelayedTask(int task_num
,
85 base::TimeDelta delay1
,
86 base::TimeDelta delay2
) {
87 base::Closure test_task
=
88 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsDelayedTask
,
89 base::Unretained(this),
92 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay1
);
95 void PostTaskWhichCallsRun(int task_num
, base::TimeDelta delay
) {
96 base::Closure test_task
=
97 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichCallsRun
,
98 base::Unretained(this),
100 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay
);
103 void PostTaskWhichPostsTaskAgain(int task_num
, base::TimeDelta delay
) {
104 base::Closure test_task
=
105 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsAgain
,
106 base::Unretained(this),
109 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay
);
113 void Task(int task_num
) {
114 if (!executed_tasks_
.empty())
115 executed_tasks_
+= " ";
117 base::StringPrintf("%d(%" PRId64
"ms)",
119 now_src_
->Now().ToInternalValue() /
120 base::Time::kMicrosecondsPerMillisecond
);
123 void TaskWhichPostsInstantTask(int task_num
) {
125 PostTask(-task_num
, base::TimeDelta());
128 void TaskWhichPostsDelayedTask(int task_num
, base::TimeDelta delay
) {
130 PostTask(-task_num
, delay
);
133 void TaskWhichCallsRun(int task_num
) {
135 task_runner_
->RunPendingTasks();
138 void TaskWhichPostsAgain(int task_num
, base::TimeDelta delay
) {
140 PostTaskWhichPostsTaskAgain(task_num
, delay
);
143 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunnerTest
);
146 TEST_F(OrderedSimpleTaskRunnerTest
, SimpleOrderingTest
) {
147 PostTask(1, base::TimeDelta());
148 PostTask(2, base::TimeDelta());
149 PostTask(3, base::TimeDelta());
151 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
152 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
154 PostTask(4, base::TimeDelta());
155 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "4(0ms)");
156 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
159 TEST_F(OrderedSimpleTaskRunnerTest
, SimpleOrderingTestPostingTasks
) {
160 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
161 PostTaskWhichPostsInstantTask(2, base::TimeDelta());
162 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
164 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
165 RUN_AND_CHECK_RESULT(
166 EXPECT_FALSE
, RunPendingTasks(), "-1(0ms) -2(0ms) -3(0ms)");
167 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
170 TEST_F(OrderedSimpleTaskRunnerTest
, SimpleOrderingTestPostingDelayedTasks
) {
171 PostTaskWhichPostsDelayedTask(
172 1, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
173 PostTaskWhichPostsDelayedTask(
174 2, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
175 PostTaskWhichPostsDelayedTask(
176 3, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
178 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
179 RUN_AND_CHECK_RESULT(
180 EXPECT_FALSE
, RunPendingTasks(), "-1(1ms) -2(1ms) -3(1ms)");
181 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
184 TEST_F(OrderedSimpleTaskRunnerTest
,
185 SimpleOrderingTestPostingReordingDelayedTasks
) {
186 PostTaskWhichPostsDelayedTask(1,
187 base::TimeDelta::FromMilliseconds(1),
188 base::TimeDelta::FromMilliseconds(20));
189 PostTaskWhichPostsDelayedTask(2,
190 base::TimeDelta::FromMilliseconds(2),
191 base::TimeDelta::FromMilliseconds(5));
192 PostTaskWhichPostsDelayedTask(3,
193 base::TimeDelta::FromMilliseconds(3),
194 base::TimeDelta::FromMilliseconds(5));
196 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(1ms) 2(2ms) 3(3ms)");
197 RUN_AND_CHECK_RESULT(
198 EXPECT_FALSE
, RunPendingTasks(), "-2(7ms) -3(8ms) -1(21ms)");
199 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
202 TEST_F(OrderedSimpleTaskRunnerTest
,
203 SimpleOrderingTestPostingReordingDelayedTasksOverlap
) {
204 PostTaskWhichPostsDelayedTask(1,
205 base::TimeDelta::FromMilliseconds(1),
206 base::TimeDelta::FromMilliseconds(5));
207 PostTaskWhichPostsDelayedTask(2,
208 base::TimeDelta::FromMilliseconds(5),
209 base::TimeDelta::FromMilliseconds(10));
210 PostTaskWhichPostsDelayedTask(3,
211 base::TimeDelta::FromMilliseconds(10),
212 base::TimeDelta::FromMilliseconds(1));
214 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(1ms) 2(5ms)");
215 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "-1(6ms) 3(10ms)");
216 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "-3(11ms) -2(15ms)");
217 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
220 TEST_F(OrderedSimpleTaskRunnerTest
, SimpleOrderingTestPostingAndRentrantTasks
) {
221 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
222 PostTaskWhichCallsRun(2, base::TimeDelta());
223 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
225 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
226 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "-1(0ms) -3(0ms)");
227 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
230 TEST_F(OrderedSimpleTaskRunnerTest
,
231 SimpleOrderingTestPostingDelayedAndRentrantTasks
) {
232 PostTaskWhichPostsDelayedTask(
233 1, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
234 PostTaskWhichCallsRun(2, base::TimeDelta());
235 PostTaskWhichPostsDelayedTask(
236 3, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
238 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
239 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "-1(1ms) -3(1ms)");
240 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
243 TEST_F(OrderedSimpleTaskRunnerTest
, OrderingTestWithDelayedTasks
) {
244 PostTask(1, base::TimeDelta());
245 PostTask(2, base::TimeDelta::FromMilliseconds(15));
246 PostTask(3, base::TimeDelta());
247 PostTask(4, base::TimeDelta::FromMilliseconds(8));
249 RUN_AND_CHECK_RESULT(
250 EXPECT_FALSE
, RunPendingTasks(), "1(0ms) 3(0ms) 4(8ms) 2(15ms)");
251 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
254 TEST_F(OrderedSimpleTaskRunnerTest
, OrderingTestWithDelayedPostingTasks
) {
255 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
256 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(15));
257 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
258 PostTaskWhichPostsInstantTask(4, base::TimeDelta::FromMilliseconds(8));
260 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 3(0ms)");
261 RUN_AND_CHECK_RESULT(
262 EXPECT_TRUE
, RunPendingTasks(), "-1(0ms) -3(0ms) 4(8ms)");
263 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "-4(8ms) 2(15ms)");
264 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "-2(15ms)");
265 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
268 TEST_F(OrderedSimpleTaskRunnerTest
, OrderingTestWithDelayedTasksManualNow
) {
269 task_runner_
->SetAutoAdvanceNowToPendingTasks(false);
270 PostTask(1, base::TimeDelta());
271 PostTask(2, base::TimeDelta::FromMilliseconds(15));
272 PostTask(3, base::TimeDelta());
273 PostTask(4, base::TimeDelta::FromMilliseconds(8));
275 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 3(0ms)");
276 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "");
277 EXPECT_EQ(task_runner_
->DelayToNextTaskTime(),
278 base::TimeDelta::FromMilliseconds(8));
279 now_src_
->SetNow(base::TimeTicks::FromInternalValue(5000));
280 EXPECT_EQ(task_runner_
->DelayToNextTaskTime(),
281 base::TimeDelta::FromMilliseconds(3));
282 now_src_
->SetNow(base::TimeTicks::FromInternalValue(25000));
283 EXPECT_EQ(task_runner_
->DelayToNextTaskTime(), base::TimeDelta());
284 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "4(25ms) 2(25ms)");
285 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
288 TEST_F(OrderedSimpleTaskRunnerTest
, RunUntilIdle
) {
289 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
290 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilIdle(), "1(0ms) -1(0ms)");
291 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilIdle(), "");
294 TEST_F(OrderedSimpleTaskRunnerTest
, RunUntilTimeAutoNow
) {
295 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
296 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
297 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
299 task_runner_
->SetAutoAdvanceNowToPendingTasks(true);
301 base::TimeTicks run_at
= base::TimeTicks();
303 run_at
+= base::TimeDelta::FromMilliseconds(2);
304 RUN_AND_CHECK_RESULT(
305 EXPECT_TRUE
, RunUntilTime(run_at
), "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
306 EXPECT_EQ(run_at
, now_src_
->Now());
308 run_at
+= base::TimeDelta::FromMilliseconds(1);
309 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilTime(run_at
), "3(3ms) -3(3ms)");
310 EXPECT_EQ(run_at
, now_src_
->Now());
312 run_at
+= base::TimeDelta::FromMilliseconds(1);
313 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilTime(run_at
), "");
314 EXPECT_EQ(run_at
, now_src_
->Now());
317 TEST_F(OrderedSimpleTaskRunnerTest
, RunUntilTimeManualNow
) {
318 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
319 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
320 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
322 task_runner_
->SetAutoAdvanceNowToPendingTasks(false);
324 base::TimeTicks run_at
= base::TimeTicks();
326 run_at
+= base::TimeDelta::FromMilliseconds(2);
327 RUN_AND_CHECK_RESULT(
328 EXPECT_TRUE
, RunUntilTime(run_at
), "1(2ms) 2(2ms) -1(2ms) -2(2ms)");
329 EXPECT_EQ(run_at
, now_src_
->Now());
331 run_at
+= base::TimeDelta::FromMilliseconds(1);
332 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilTime(run_at
), "3(3ms) -3(3ms)");
333 EXPECT_EQ(run_at
, now_src_
->Now());
335 run_at
+= base::TimeDelta::FromMilliseconds(1);
336 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilTime(run_at
), "");
337 EXPECT_EQ(run_at
, now_src_
->Now());
340 TEST_F(OrderedSimpleTaskRunnerTest
, RunForPeriod
) {
341 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
342 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
343 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
345 RUN_AND_CHECK_RESULT(EXPECT_TRUE
,
346 RunForPeriod(base::TimeDelta::FromMilliseconds(2)),
347 "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
348 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(2),
351 RUN_AND_CHECK_RESULT(EXPECT_FALSE
,
352 RunForPeriod(base::TimeDelta::FromMilliseconds(1)),
354 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
357 RUN_AND_CHECK_RESULT(
358 EXPECT_FALSE
, RunForPeriod(base::TimeDelta::FromMilliseconds(1)), "");
359 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(4),
363 TEST_F(OrderedSimpleTaskRunnerTest
, RunTasksWhileWithCallback
) {
364 base::Callback
<bool(void)> return_true
= base::Bind(&ReturnTrue
);
365 base::Callback
<bool(void)> return_false
= base::Bind(&ReturnFalse
);
367 PostTask(1, base::TimeDelta());
369 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunTasksWhile(return_false
), "");
370 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunTasksWhile(return_true
), "1(0ms)");
373 TEST_F(OrderedSimpleTaskRunnerTest
, EmptyTaskList
) {
374 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunPendingTasks(), "");
375 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilIdle(), "");
377 ASSERT_EQ(base::TimeTicks(), now_src_
->Now());
379 RUN_AND_CHECK_RESULT(
380 EXPECT_FALSE
, RunUntilTime(base::TimeTicks::FromInternalValue(100)), "");
381 EXPECT_EQ(base::TimeTicks::FromInternalValue(100), now_src_
->Now());
383 RUN_AND_CHECK_RESULT(
384 EXPECT_FALSE
, RunForPeriod(base::TimeDelta::FromInternalValue(100)), "");
385 EXPECT_EQ(base::TimeTicks::FromInternalValue(200), now_src_
->Now());
387 base::Callback
<bool(void)> return_true
= base::Bind(&ReturnTrue
);
388 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunTasksWhile(return_true
), "");
391 TEST_F(OrderedSimpleTaskRunnerTest
, RunPendingTasksTimeout
) {
392 PostTask(1, base::TimeDelta());
393 PostTask(2, base::TimeDelta());
394 PostTask(3, base::TimeDelta());
395 PostTask(4, base::TimeDelta());
396 PostTask(5, base::TimeDelta());
397 PostTask(6, base::TimeDelta());
399 task_runner_
->SetRunTaskLimit(3);
400 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
402 task_runner_
->SetRunTaskLimit(2);
403 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "4(0ms) 5(0ms)");
405 task_runner_
->SetRunTaskLimit(0);
406 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunPendingTasks(), "");
409 TEST_F(OrderedSimpleTaskRunnerTest
, RunUntilIdleTimeout
) {
410 PostTaskWhichPostsTaskAgain(1, base::TimeDelta::FromMilliseconds(3));
412 task_runner_
->SetRunTaskLimit(3);
413 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunUntilIdle(), "1(3ms) 1(6ms) 1(9ms)");
415 task_runner_
->SetRunTaskLimit(2);
416 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunUntilIdle(), "1(12ms) 1(15ms)");
418 task_runner_
->SetRunTaskLimit(0);
419 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunUntilIdle(), "");
422 TEST_F(OrderedSimpleTaskRunnerTest
, RunUntilTimeout
) {
423 base::TimeTicks run_to
= base::TimeTicks() + base::TimeDelta::FromSeconds(1);
425 PostTask(1, base::TimeDelta::FromMilliseconds(1));
426 PostTask(2, base::TimeDelta::FromMilliseconds(2));
427 PostTask(3, base::TimeDelta::FromMilliseconds(3));
428 PostTask(4, base::TimeDelta::FromMilliseconds(4));
429 PostTask(5, base::TimeDelta::FromMilliseconds(5));
431 EXPECT_EQ(base::TimeTicks(), now_src_
->Now());
432 task_runner_
->SetRunTaskLimit(3);
433 RUN_AND_CHECK_RESULT(
434 EXPECT_TRUE
, RunUntilTime(run_to
), "1(1ms) 2(2ms) 3(3ms)");
435 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
438 task_runner_
->SetRunTaskLimit(0);
439 RUN_AND_CHECK_RESULT(EXPECT_TRUE
, RunUntilTime(run_to
), "");
441 task_runner_
->SetRunTaskLimit(100);
442 RUN_AND_CHECK_RESULT(EXPECT_FALSE
, RunUntilTime(run_to
), "4(4ms) 5(5ms)");
443 EXPECT_EQ(run_to
, now_src_
->Now());