Create an initial chrome://supervised-user-internals page
[chromium-blink-merge.git] / cc / test / ordered_simple_task_runner_unittest.cc
blob437235ea1d3a6923af6004b2b4ae2ee9cd3bc9b0
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.
5 #include <string>
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_); \
22 executed_tasks_ = "";
24 namespace {
26 bool ReturnTrue() {
27 return true;
30 bool ReturnFalse() {
31 return false;
35 namespace cc {
37 TEST(TestOrderablePendingTask, Ordering) {
38 TestOrderablePendingTask a;
39 TestOrderablePendingTask b;
40 TestOrderablePendingTask c;
42 EXPECT_EQ(a, a);
43 EXPECT_EQ(b, b);
44 EXPECT_EQ(c, c);
45 EXPECT_LT(a, b);
46 EXPECT_LT(b, c);
47 EXPECT_LT(a, c);
49 TestOrderablePendingTask a2 = a;
50 EXPECT_EQ(a, a2);
51 EXPECT_LT(a2, b);
52 EXPECT_LT(b, c);
53 EXPECT_LT(a2, c);
56 class OrderedSimpleTaskRunnerTest : public testing::Test {
57 public:
58 OrderedSimpleTaskRunnerTest()
59 : now_src_(new base::SimpleTestTickClock()),
60 task_runner_(new OrderedSimpleTaskRunner(now_src_.get(), true)) {}
61 ~OrderedSimpleTaskRunnerTest() override {}
63 protected:
64 std::string executed_tasks_;
65 scoped_ptr<base::SimpleTestTickClock> now_src_;
66 scoped_refptr<OrderedSimpleTaskRunner> task_runner_;
68 void PostTask(int task_num, base::TimeDelta delay) {
69 base::Closure test_task = base::Bind(&OrderedSimpleTaskRunnerTest::Task,
70 base::Unretained(this),
71 task_num);
72 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
75 void PostTaskWhichPostsInstantTask(int task_num, base::TimeDelta delay) {
76 base::Closure test_task =
77 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsInstantTask,
78 base::Unretained(this),
79 task_num);
80 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
83 void PostTaskWhichPostsDelayedTask(int task_num,
84 base::TimeDelta delay1,
85 base::TimeDelta delay2) {
86 base::Closure test_task =
87 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsDelayedTask,
88 base::Unretained(this),
89 task_num,
90 delay2);
91 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay1);
94 void PostTaskWhichCallsRun(int task_num, base::TimeDelta delay) {
95 base::Closure test_task =
96 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichCallsRun,
97 base::Unretained(this),
98 task_num);
99 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
102 void PostTaskWhichPostsTaskAgain(int task_num, base::TimeDelta delay) {
103 base::Closure test_task =
104 base::Bind(&OrderedSimpleTaskRunnerTest::TaskWhichPostsAgain,
105 base::Unretained(this),
106 task_num,
107 delay);
108 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
111 private:
112 void Task(int task_num) {
113 if (!executed_tasks_.empty())
114 executed_tasks_ += " ";
115 executed_tasks_ += base::StringPrintf(
116 "%d(%" PRId64 "ms)", task_num,
117 (now_src_->NowTicks() - base::TimeTicks()).InMilliseconds());
120 void TaskWhichPostsInstantTask(int task_num) {
121 Task(task_num);
122 PostTask(-task_num, base::TimeDelta());
125 void TaskWhichPostsDelayedTask(int task_num, base::TimeDelta delay) {
126 Task(task_num);
127 PostTask(-task_num, delay);
130 void TaskWhichCallsRun(int task_num) {
131 Task(task_num);
132 task_runner_->RunPendingTasks();
135 void TaskWhichPostsAgain(int task_num, base::TimeDelta delay) {
136 Task(task_num);
137 PostTaskWhichPostsTaskAgain(task_num, delay);
140 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunnerTest);
143 TEST_F(OrderedSimpleTaskRunnerTest, SimpleOrderingTest) {
144 PostTask(1, base::TimeDelta());
145 PostTask(2, base::TimeDelta());
146 PostTask(3, base::TimeDelta());
148 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
149 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
151 PostTask(4, base::TimeDelta());
152 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "4(0ms)");
153 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
156 TEST_F(OrderedSimpleTaskRunnerTest, SimpleOrderingTestPostingTasks) {
157 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
158 PostTaskWhichPostsInstantTask(2, base::TimeDelta());
159 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
161 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
162 RUN_AND_CHECK_RESULT(
163 EXPECT_FALSE, RunPendingTasks(), "-1(0ms) -2(0ms) -3(0ms)");
164 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
167 TEST_F(OrderedSimpleTaskRunnerTest, SimpleOrderingTestPostingDelayedTasks) {
168 PostTaskWhichPostsDelayedTask(
169 1, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
170 PostTaskWhichPostsDelayedTask(
171 2, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
172 PostTaskWhichPostsDelayedTask(
173 3, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
175 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
176 RUN_AND_CHECK_RESULT(
177 EXPECT_FALSE, RunPendingTasks(), "-1(1ms) -2(1ms) -3(1ms)");
178 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
181 TEST_F(OrderedSimpleTaskRunnerTest,
182 SimpleOrderingTestPostingReordingDelayedTasks) {
183 PostTaskWhichPostsDelayedTask(1,
184 base::TimeDelta::FromMilliseconds(1),
185 base::TimeDelta::FromMilliseconds(20));
186 PostTaskWhichPostsDelayedTask(2,
187 base::TimeDelta::FromMilliseconds(2),
188 base::TimeDelta::FromMilliseconds(5));
189 PostTaskWhichPostsDelayedTask(3,
190 base::TimeDelta::FromMilliseconds(3),
191 base::TimeDelta::FromMilliseconds(5));
193 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(1ms) 2(2ms) 3(3ms)");
194 RUN_AND_CHECK_RESULT(
195 EXPECT_FALSE, RunPendingTasks(), "-2(7ms) -3(8ms) -1(21ms)");
196 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
199 TEST_F(OrderedSimpleTaskRunnerTest,
200 SimpleOrderingTestPostingReordingDelayedTasksOverlap) {
201 PostTaskWhichPostsDelayedTask(1,
202 base::TimeDelta::FromMilliseconds(1),
203 base::TimeDelta::FromMilliseconds(5));
204 PostTaskWhichPostsDelayedTask(2,
205 base::TimeDelta::FromMilliseconds(5),
206 base::TimeDelta::FromMilliseconds(10));
207 PostTaskWhichPostsDelayedTask(3,
208 base::TimeDelta::FromMilliseconds(10),
209 base::TimeDelta::FromMilliseconds(1));
211 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(1ms) 2(5ms)");
212 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "-1(6ms) 3(10ms)");
213 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "-3(11ms) -2(15ms)");
214 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
217 TEST_F(OrderedSimpleTaskRunnerTest, SimpleOrderingTestPostingAndRentrantTasks) {
218 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
219 PostTaskWhichCallsRun(2, base::TimeDelta());
220 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
222 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
223 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "-1(0ms) -3(0ms)");
224 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
227 TEST_F(OrderedSimpleTaskRunnerTest,
228 SimpleOrderingTestPostingDelayedAndRentrantTasks) {
229 PostTaskWhichPostsDelayedTask(
230 1, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
231 PostTaskWhichCallsRun(2, base::TimeDelta());
232 PostTaskWhichPostsDelayedTask(
233 3, base::TimeDelta(), base::TimeDelta::FromMilliseconds(1));
235 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
236 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "-1(1ms) -3(1ms)");
237 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
240 TEST_F(OrderedSimpleTaskRunnerTest, OrderingTestWithDelayedTasks) {
241 PostTask(1, base::TimeDelta());
242 PostTask(2, base::TimeDelta::FromMilliseconds(15));
243 PostTask(3, base::TimeDelta());
244 PostTask(4, base::TimeDelta::FromMilliseconds(8));
246 RUN_AND_CHECK_RESULT(
247 EXPECT_FALSE, RunPendingTasks(), "1(0ms) 3(0ms) 4(8ms) 2(15ms)");
248 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
251 TEST_F(OrderedSimpleTaskRunnerTest, OrderingTestWithDelayedPostingTasks) {
252 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
253 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(15));
254 PostTaskWhichPostsInstantTask(3, base::TimeDelta());
255 PostTaskWhichPostsInstantTask(4, base::TimeDelta::FromMilliseconds(8));
257 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 3(0ms)");
258 RUN_AND_CHECK_RESULT(
259 EXPECT_TRUE, RunPendingTasks(), "-1(0ms) -3(0ms) 4(8ms)");
260 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "-4(8ms) 2(15ms)");
261 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "-2(15ms)");
262 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
265 TEST_F(OrderedSimpleTaskRunnerTest, OrderingTestWithDelayedTasksManualNow) {
266 task_runner_->SetAutoAdvanceNowToPendingTasks(false);
267 PostTask(1, base::TimeDelta());
268 PostTask(2, base::TimeDelta::FromMilliseconds(15));
269 PostTask(3, base::TimeDelta());
270 PostTask(4, base::TimeDelta::FromMilliseconds(8));
272 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 3(0ms)");
273 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "");
274 EXPECT_EQ(task_runner_->DelayToNextTaskTime(),
275 base::TimeDelta::FromMilliseconds(8));
276 now_src_->Advance(base::TimeDelta::FromMicroseconds(5000));
277 EXPECT_EQ(task_runner_->DelayToNextTaskTime(),
278 base::TimeDelta::FromMilliseconds(3));
279 now_src_->Advance(base::TimeDelta::FromMicroseconds(20000));
280 EXPECT_EQ(task_runner_->DelayToNextTaskTime(), base::TimeDelta());
281 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "4(25ms) 2(25ms)");
282 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
285 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilIdle) {
286 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
287 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "1(0ms) -1(0ms)");
288 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "");
291 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeAutoNow) {
292 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
293 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
294 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
296 task_runner_->SetAutoAdvanceNowToPendingTasks(true);
298 base::TimeTicks run_at = base::TimeTicks();
300 run_at += base::TimeDelta::FromMilliseconds(2);
301 RUN_AND_CHECK_RESULT(
302 EXPECT_TRUE, RunUntilTime(run_at), "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
303 EXPECT_EQ(run_at, now_src_->NowTicks());
305 run_at += base::TimeDelta::FromMilliseconds(1);
306 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)");
307 EXPECT_EQ(run_at, now_src_->NowTicks());
309 run_at += base::TimeDelta::FromMilliseconds(1);
310 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "");
311 EXPECT_EQ(run_at, now_src_->NowTicks());
314 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeManualNow) {
315 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
316 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
317 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
319 task_runner_->SetAutoAdvanceNowToPendingTasks(false);
321 base::TimeTicks run_at = base::TimeTicks();
323 run_at += base::TimeDelta::FromMilliseconds(2);
324 RUN_AND_CHECK_RESULT(
325 EXPECT_TRUE, RunUntilTime(run_at), "1(2ms) 2(2ms) -1(2ms) -2(2ms)");
326 EXPECT_EQ(run_at, now_src_->NowTicks());
328 run_at += base::TimeDelta::FromMilliseconds(1);
329 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)");
330 EXPECT_EQ(run_at, now_src_->NowTicks());
332 run_at += base::TimeDelta::FromMilliseconds(1);
333 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "");
334 EXPECT_EQ(run_at, now_src_->NowTicks());
337 TEST_F(OrderedSimpleTaskRunnerTest, RunForPeriod) {
338 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
339 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
340 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
342 RUN_AND_CHECK_RESULT(EXPECT_TRUE,
343 RunForPeriod(base::TimeDelta::FromMilliseconds(2)),
344 "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
345 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(2),
346 now_src_->NowTicks());
348 RUN_AND_CHECK_RESULT(EXPECT_FALSE,
349 RunForPeriod(base::TimeDelta::FromMilliseconds(1)),
350 "3(3ms) -3(3ms)");
351 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
352 now_src_->NowTicks());
354 RUN_AND_CHECK_RESULT(
355 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromMilliseconds(1)), "");
356 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(4),
357 now_src_->NowTicks());
360 TEST_F(OrderedSimpleTaskRunnerTest, RunTasksWhileWithCallback) {
361 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue);
362 base::Callback<bool(void)> return_false = base::Bind(&ReturnFalse);
364 PostTask(1, base::TimeDelta());
366 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunTasksWhile(return_false), "");
367 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), "1(0ms)");
370 TEST_F(OrderedSimpleTaskRunnerTest, EmptyTaskList) {
371 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
372 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "");
374 ASSERT_EQ(base::TimeTicks(), now_src_->NowTicks());
376 RUN_AND_CHECK_RESULT(
377 EXPECT_FALSE, RunUntilTime(base::TimeTicks::FromInternalValue(100)), "");
378 EXPECT_EQ(base::TimeTicks::FromInternalValue(100), now_src_->NowTicks());
380 RUN_AND_CHECK_RESULT(
381 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromInternalValue(100)), "");
382 EXPECT_EQ(base::TimeTicks::FromInternalValue(200), now_src_->NowTicks());
384 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue);
385 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), "");
388 TEST_F(OrderedSimpleTaskRunnerTest, RunPendingTasksTimeout) {
389 PostTask(1, base::TimeDelta());
390 PostTask(2, base::TimeDelta());
391 PostTask(3, base::TimeDelta());
392 PostTask(4, base::TimeDelta());
393 PostTask(5, base::TimeDelta());
394 PostTask(6, base::TimeDelta());
396 task_runner_->SetRunTaskLimit(3);
397 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 2(0ms) 3(0ms)");
399 task_runner_->SetRunTaskLimit(2);
400 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "4(0ms) 5(0ms)");
402 task_runner_->SetRunTaskLimit(0);
403 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "");
406 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilIdleTimeout) {
407 PostTaskWhichPostsTaskAgain(1, base::TimeDelta::FromMilliseconds(3));
409 task_runner_->SetRunTaskLimit(3);
410 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilIdle(), "1(3ms) 1(6ms) 1(9ms)");
412 task_runner_->SetRunTaskLimit(2);
413 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilIdle(), "1(12ms) 1(15ms)");
415 task_runner_->SetRunTaskLimit(0);
416 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilIdle(), "");
419 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeout) {
420 base::TimeTicks run_to = base::TimeTicks() + base::TimeDelta::FromSeconds(1);
422 PostTask(1, base::TimeDelta::FromMilliseconds(1));
423 PostTask(2, base::TimeDelta::FromMilliseconds(2));
424 PostTask(3, base::TimeDelta::FromMilliseconds(3));
425 PostTask(4, base::TimeDelta::FromMilliseconds(4));
426 PostTask(5, base::TimeDelta::FromMilliseconds(5));
428 EXPECT_EQ(base::TimeTicks(), now_src_->NowTicks());
429 task_runner_->SetRunTaskLimit(3);
430 RUN_AND_CHECK_RESULT(
431 EXPECT_TRUE, RunUntilTime(run_to), "1(1ms) 2(2ms) 3(3ms)");
432 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
433 now_src_->NowTicks());
435 task_runner_->SetRunTaskLimit(0);
436 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilTime(run_to), "");
438 task_runner_->SetRunTaskLimit(100);
439 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_to), "4(4ms) 5(5ms)");
440 EXPECT_EQ(run_to, now_src_->NowTicks());
443 } // namespace cc