OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/chromium/test_task_runner.h" | 5 #include "net/quic/chromium/test_task_runner.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "net/quic/test_tools/mock_clock.h" | 10 #include "net/quic/test_tools/mock_clock.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 bool TestTaskRunner::RunsTasksOnCurrentThread() const { | 29 bool TestTaskRunner::RunsTasksOnCurrentThread() const { |
30 return true; | 30 return true; |
31 } | 31 } |
32 | 32 |
33 const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const { | 33 const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const { |
34 return tasks_; | 34 return tasks_; |
35 } | 35 } |
36 | 36 |
37 void TestTaskRunner::RunNextTask() { | 37 void TestTaskRunner::RunNextTask() { |
38 // Find the next task to run, advance the time to the correct time | |
39 // and then run the task. | |
40 std::vector<PostedTask>::iterator next = FindNextTask(); | 38 std::vector<PostedTask>::iterator next = FindNextTask(); |
41 DCHECK(next != tasks_.end()); | 39 DCHECK(next != tasks_.end()); |
42 clock_->AdvanceTime(QuicTime::Delta::FromMicroseconds( | 40 clock_->AdvanceTime(QuicTime::Delta::FromMicroseconds( |
43 (next->GetTimeToRun() - clock_->NowInTicks()).InMicroseconds())); | 41 (next->GetTimeToRun() - clock_->NowInTicks()).InMicroseconds())); |
44 PostedTask task = std::move(*next); | 42 PostedTask task = std::move(*next); |
45 tasks_.erase(next); | 43 tasks_.erase(next); |
46 std::move(task.task).Run(); | 44 std::move(task.task).Run(); |
47 } | 45 } |
48 | 46 |
| 47 void TestTaskRunner::RunUntilIdle() { |
| 48 while (!tasks_.empty()) |
| 49 RunNextTask(); |
| 50 } |
49 namespace { | 51 namespace { |
50 | 52 |
51 struct ShouldRunBeforeLessThan { | 53 struct ShouldRunBeforeLessThan { |
52 bool operator()(const PostedTask& task1, const PostedTask& task2) const { | 54 bool operator()(const PostedTask& task1, const PostedTask& task2) const { |
53 return task1.ShouldRunBefore(task2); | 55 return task1.ShouldRunBefore(task2); |
54 } | 56 } |
55 }; | 57 }; |
56 | 58 |
57 } // namespace | 59 } // namespace |
58 | 60 |
59 std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { | 61 std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { |
60 return std::min_element(tasks_.begin(), tasks_.end(), | 62 return std::min_element(tasks_.begin(), tasks_.end(), |
61 ShouldRunBeforeLessThan()); | 63 ShouldRunBeforeLessThan()); |
62 } | 64 } |
63 | 65 |
64 } // namespace test | 66 } // namespace test |
65 } // namespace net | 67 } // namespace net |
OLD | NEW |