blob: a91b285df391ab7b3c4b19cceac5530f03c13650 [file] [log] [blame]
[email protected]1d197ef52012-11-07 20:41:291// 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
3// found in the LICENSE file.
4
Ryan Hamiltona3ee93a72018-08-01 22:03:085#include "net/quic/test_task_runner.h"
[email protected]1d197ef52012-11-07 20:41:296
[email protected]a8582b12012-12-19 22:18:297#include <algorithm>
tzik070c8ffb2017-03-29 05:28:128#include <utility>
[email protected]1d197ef52012-11-07 20:41:299
Victor Vasiliev6bb59d22019-03-08 21:34:5110#include "net/third_party/quiche/src/quic/test_tools/mock_clock.h"
[email protected]1d197ef52012-11-07 20:41:2911#include "testing/gtest/include/gtest/gtest.h"
12
13namespace net {
[email protected]1d197ef52012-11-07 20:41:2914namespace test {
15
Ryan Hamilton85d4d6672018-08-06 18:51:2816namespace {
17
18base::TimeTicks NowInTicks(const quic::MockClock& clock) {
19 base::TimeTicks ticks;
20 return ticks + base::TimeDelta::FromMicroseconds(
21 (clock.Now() - quic::QuicTime::Zero()).ToMicroseconds());
22}
23
24} // namespace
25
Ryan Hamilton8d9ee76e2018-05-29 23:52:5226TestTaskRunner::TestTaskRunner(quic::MockClock* clock) : clock_(clock) {}
[email protected]1d197ef52012-11-07 20:41:2927
rjshaded5ced072015-12-18 19:26:0228TestTaskRunner::~TestTaskRunner() {}
[email protected]1d197ef52012-11-07 20:41:2929
Brett Wilson9c361992017-09-12 06:05:2130bool TestTaskRunner::PostDelayedTask(const base::Location& from_here,
tzik6e427842017-04-05 10:13:2131 base::OnceClosure task,
[email protected]a8582b12012-12-19 22:18:2932 base::TimeDelta delay) {
33 EXPECT_GE(delay, base::TimeDelta());
Ryan Hamilton85d4d6672018-08-06 18:51:2834 tasks_.push_back(PostedTask(from_here, std::move(task), NowInTicks(*clock_),
tzik070c8ffb2017-03-29 05:28:1235 delay, base::TestPendingTask::NESTABLE));
[email protected]a8582b12012-12-19 22:18:2936 return false;
37}
38
Zhongyi Shi8fff75b2017-11-19 21:36:3639bool TestTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
40 base::OnceClosure task,
41 base::TimeDelta delay) {
42 return PostDelayedTask(from_here, std::move(task), delay);
43}
44
peary23322df62017-05-09 03:55:4845bool TestTaskRunner::RunsTasksInCurrentSequence() const {
[email protected]1d197ef52012-11-07 20:41:2946 return true;
47}
48
[email protected]a8582b12012-12-19 22:18:2949const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const {
50 return tasks_;
[email protected]1d197ef52012-11-07 20:41:2951}
52
Zhongyi Shia6b68d112018-09-24 07:49:0353quic::QuicTime::Delta TestTaskRunner::NextPendingTaskDelay() {
54 if (tasks_.empty())
55 return quic::QuicTime::Delta::Infinite();
56
57 auto next = FindNextTask();
58 return quic::QuicTime::Delta::FromMicroseconds(
59 (next->GetTimeToRun() - NowInTicks(*clock_)).InMicroseconds());
60}
61
[email protected]1d197ef52012-11-07 20:41:2962void TestTaskRunner::RunNextTask() {
jdoerrie22a91d8b92018-10-05 08:43:2663 auto next = FindNextTask();
[email protected]1d197ef52012-11-07 20:41:2964 DCHECK(next != tasks_.end());
Ryan Hamilton8d9ee76e2018-05-29 23:52:5265 clock_->AdvanceTime(quic::QuicTime::Delta::FromMicroseconds(
Ryan Hamilton85d4d6672018-08-06 18:51:2866 (next->GetTimeToRun() - NowInTicks(*clock_)).InMicroseconds()));
tzika6f0007a2017-01-27 04:01:1167 PostedTask task = std::move(*next);
[email protected]1d197ef52012-11-07 20:41:2968 tasks_.erase(next);
tzika6f0007a2017-01-27 04:01:1169 std::move(task.task).Run();
[email protected]a8582b12012-12-19 22:18:2970}
71
Zhongyi Shia6b68d112018-09-24 07:49:0372void TestTaskRunner::FastForwardBy(quic::QuicTime::Delta delta) {
73 DCHECK_GE(delta, quic::QuicTime::Delta::Zero());
74
75 quic::QuicTime end_timestamp = clock_->Now() + delta;
76
77 while (NextPendingTaskDelay() <= end_timestamp - clock_->Now()) {
78 RunNextTask();
79 }
80
81 if (clock_->Now() != end_timestamp)
82 clock_->AdvanceTime(end_timestamp - clock_->Now());
83
84 while (NextPendingTaskDelay() <= quic::QuicTime::Delta::Zero()) {
85 RunNextTask();
86 }
87 return;
88}
89
rch9ecde09b2017-04-08 00:18:2390void TestTaskRunner::RunUntilIdle() {
91 while (!tasks_.empty())
92 RunNextTask();
93}
[email protected]a8582b12012-12-19 22:18:2994namespace {
95
96struct ShouldRunBeforeLessThan {
97 bool operator()(const PostedTask& task1, const PostedTask& task2) const {
98 return task1.ShouldRunBefore(task2);
99 }
100};
101
102} // namespace
103
104std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() {
rjshaded5ced072015-12-18 19:26:02105 return std::min_element(tasks_.begin(), tasks_.end(),
106 ShouldRunBeforeLessThan());
[email protected]1d197ef52012-11-07 20:41:29107}
108
109} // namespace test
[email protected]1d197ef52012-11-07 20:41:29110} // namespace net