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.
5 #include "net/quic/test_tools/test_task_runner.h"
9 #include "net/quic/test_tools/mock_clock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 TestTaskRunner::TestTaskRunner(MockClock
* clock
)
19 TestTaskRunner::~TestTaskRunner() {
22 bool TestTaskRunner::PostDelayedTask(const tracked_objects::Location
& from_here
,
23 const base::Closure
& task
,
24 base::TimeDelta delay
) {
25 EXPECT_GE(delay
, base::TimeDelta());
27 PostedTask(from_here
, task
, clock_
->NowInTicks(), delay
,
28 base::TestPendingTask::NESTABLE
));
32 bool TestTaskRunner::RunsTasksOnCurrentThread() const {
36 const std::vector
<PostedTask
>& TestTaskRunner::GetPostedTasks() const {
40 void TestTaskRunner::RunNextTask() {
41 // Find the next task to run, advance the time to the correct time
42 // and then run the task.
43 std::vector
<PostedTask
>::iterator next
= FindNextTask();
44 DCHECK(next
!= tasks_
.end());
45 clock_
->AdvanceTime(QuicTime::Delta::FromMicroseconds(
46 (next
->GetTimeToRun() - clock_
->NowInTicks()).InMicroseconds()));
47 PostedTask task
= *next
;
54 struct ShouldRunBeforeLessThan
{
55 bool operator()(const PostedTask
& task1
, const PostedTask
& task2
) const {
56 return task1
.ShouldRunBefore(task2
);
62 std::vector
<PostedTask
>::iterator
TestTaskRunner::FindNextTask() {
63 return std::min_element(
64 tasks_
.begin(), tasks_
.end(), ShouldRunBeforeLessThan());