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 5 #include "net/quic/test_tools/test_task_runner.h" 6 7 #include <algorithm> 8 9 #include "net/quic/test_tools/mock_clock.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace net { 13 namespace test { 14 15 TestTaskRunner::TestTaskRunner(MockClock* clock) 16 : clock_(clock) { 17 } 18 19 TestTaskRunner::~TestTaskRunner() { 20 } 21 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()); 26 tasks_.push_back( 27 PostedTask(from_here, task, clock_->NowInTicks(), delay, 28 base::TestPendingTask::NESTABLE)); 29 return false; 30 } 31 32 bool TestTaskRunner::RunsTasksOnCurrentThread() const { 33 return true; 34 } 35 36 const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const { 37 return tasks_; 38 } 39 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; 48 tasks_.erase(next); 49 task.task.Run(); 50 } 51 52 namespace { 53 54 struct ShouldRunBeforeLessThan { 55 bool operator()(const PostedTask& task1, const PostedTask& task2) const { 56 return task1.ShouldRunBefore(task2); 57 } 58 }; 59 60 } // namespace 61 62 std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() { 63 return std::min_element( 64 tasks_.begin(), tasks_.end(), ShouldRunBeforeLessThan()); 65 } 66 67 } // namespace test 68 } // namespace net 69