[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 5 | #include "net/quic/test_task_runner.h" |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 6 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 7 | #include <algorithm> |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 9 | |
Victor Vasiliev | 6bb59d2 | 2019-03-08 21:34:51 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace net { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 14 | namespace test { |
| 15 | |
Ryan Hamilton | 85d4d667 | 2018-08-06 18:51:28 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | base::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 Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 26 | TestTaskRunner::TestTaskRunner(quic::MockClock* clock) : clock_(clock) {} |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 27 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 28 | TestTaskRunner::~TestTaskRunner() {} |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 29 | |
Brett Wilson | 9c36199 | 2017-09-12 06:05:21 | [diff] [blame] | 30 | bool TestTaskRunner::PostDelayedTask(const base::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 31 | base::OnceClosure task, |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 32 | base::TimeDelta delay) { |
| 33 | EXPECT_GE(delay, base::TimeDelta()); |
Ryan Hamilton | 85d4d667 | 2018-08-06 18:51:28 | [diff] [blame] | 34 | tasks_.push_back(PostedTask(from_here, std::move(task), NowInTicks(*clock_), |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 35 | delay, base::TestPendingTask::NESTABLE)); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 39 | bool 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 | |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 45 | bool TestTaskRunner::RunsTasksInCurrentSequence() const { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 49 | const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const { |
| 50 | return tasks_; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 51 | } |
| 52 | |
Zhongyi Shi | a6b68d11 | 2018-09-24 07:49:03 | [diff] [blame] | 53 | quic::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] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 62 | void TestTaskRunner::RunNextTask() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 63 | auto next = FindNextTask(); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 64 | DCHECK(next != tasks_.end()); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 65 | clock_->AdvanceTime(quic::QuicTime::Delta::FromMicroseconds( |
Ryan Hamilton | 85d4d667 | 2018-08-06 18:51:28 | [diff] [blame] | 66 | (next->GetTimeToRun() - NowInTicks(*clock_)).InMicroseconds())); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 67 | PostedTask task = std::move(*next); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 68 | tasks_.erase(next); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 69 | std::move(task.task).Run(); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 70 | } |
| 71 | |
Zhongyi Shi | a6b68d11 | 2018-09-24 07:49:03 | [diff] [blame] | 72 | void 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 | |
rch | 9ecde09b | 2017-04-08 00:18:23 | [diff] [blame] | 90 | void TestTaskRunner::RunUntilIdle() { |
| 91 | while (!tasks_.empty()) |
| 92 | RunNextTask(); |
| 93 | } |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 94 | namespace { |
| 95 | |
| 96 | struct ShouldRunBeforeLessThan { |
| 97 | bool operator()(const PostedTask& task1, const PostedTask& task2) const { |
| 98 | return task1.ShouldRunBefore(task2); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 105 | return std::min_element(tasks_.begin(), tasks_.end(), |
| 106 | ShouldRunBeforeLessThan()); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace test |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 110 | } // namespace net |