[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 | |
rch | 1e36936 | 2017-04-03 19:44:51 | [diff] [blame] | 5 | #include "net/quic/chromium/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 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 10 | #include "net/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 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 16 | TestTaskRunner::TestTaskRunner(MockClock* clock) : clock_(clock) {} |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 17 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 18 | TestTaskRunner::~TestTaskRunner() {} |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 19 | |
Brett Wilson | 9c36199 | 2017-09-12 06:05:21 | [diff] [blame^] | 20 | bool TestTaskRunner::PostDelayedTask(const base::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 21 | base::OnceClosure task, |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 22 | base::TimeDelta delay) { |
| 23 | EXPECT_GE(delay, base::TimeDelta()); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 24 | tasks_.push_back(PostedTask(from_here, std::move(task), clock_->NowInTicks(), |
| 25 | delay, base::TestPendingTask::NESTABLE)); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 26 | return false; |
| 27 | } |
| 28 | |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 29 | bool TestTaskRunner::RunsTasksInCurrentSequence() const { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 30 | return true; |
| 31 | } |
| 32 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 33 | const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const { |
| 34 | return tasks_; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void TestTaskRunner::RunNextTask() { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 38 | std::vector<PostedTask>::iterator next = FindNextTask(); |
| 39 | DCHECK(next != tasks_.end()); |
[email protected] | 2a960e0 | 2012-11-11 14:48:10 | [diff] [blame] | 40 | clock_->AdvanceTime(QuicTime::Delta::FromMicroseconds( |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 41 | (next->GetTimeToRun() - clock_->NowInTicks()).InMicroseconds())); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 42 | PostedTask task = std::move(*next); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 43 | tasks_.erase(next); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 44 | std::move(task.task).Run(); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 45 | } |
| 46 | |
rch | 9ecde09b | 2017-04-08 00:18:23 | [diff] [blame] | 47 | void TestTaskRunner::RunUntilIdle() { |
| 48 | while (!tasks_.empty()) |
| 49 | RunNextTask(); |
| 50 | } |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | struct ShouldRunBeforeLessThan { |
| 54 | bool operator()(const PostedTask& task1, const PostedTask& task2) const { |
| 55 | return task1.ShouldRunBefore(task2); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 62 | return std::min_element(tasks_.begin(), tasks_.end(), |
| 63 | ShouldRunBeforeLessThan()); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace test |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 67 | } // namespace net |