[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 | |
Ryan Hamilton | 56b10c5d | 2018-05-11 13:40:16 | [diff] [blame] | 10 | #include "net/third_party/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 | |
| 53 | void TestTaskRunner::RunNextTask() { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 54 | std::vector<PostedTask>::iterator next = FindNextTask(); |
| 55 | DCHECK(next != tasks_.end()); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 56 | clock_->AdvanceTime(quic::QuicTime::Delta::FromMicroseconds( |
Ryan Hamilton | 85d4d667 | 2018-08-06 18:51:28 | [diff] [blame^] | 57 | (next->GetTimeToRun() - NowInTicks(*clock_)).InMicroseconds())); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 58 | PostedTask task = std::move(*next); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 59 | tasks_.erase(next); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 60 | std::move(task.task).Run(); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 61 | } |
| 62 | |
rch | 9ecde09b | 2017-04-08 00:18:23 | [diff] [blame] | 63 | void TestTaskRunner::RunUntilIdle() { |
| 64 | while (!tasks_.empty()) |
| 65 | RunNextTask(); |
| 66 | } |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 67 | namespace { |
| 68 | |
| 69 | struct ShouldRunBeforeLessThan { |
| 70 | bool operator()(const PostedTask& task1, const PostedTask& task2) const { |
| 71 | return task1.ShouldRunBefore(task2); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 78 | return std::min_element(tasks_.begin(), tasks_.end(), |
| 79 | ShouldRunBeforeLessThan()); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | } // namespace test |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 83 | } // namespace net |