summaryrefslogtreecommitdiffstats
path: root/net/quic/test_tools/test_task_runner.cc
blob: 385fd5dfa82fe13fc4d77fe96718267fe264edcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/test_tools/test_task_runner.h"

#include <algorithm>

#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test {

TestTaskRunner::TestTaskRunner(MockClock* clock)
    : clock_(clock) {
}

TestTaskRunner::~TestTaskRunner() {
}

bool TestTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here,
                                     const base::Closure& task,
                                     base::TimeDelta delay) {
  EXPECT_GE(delay, base::TimeDelta());
  tasks_.push_back(
      PostedTask(from_here, task, clock_->NowInTicks(), delay,
                 base::TestPendingTask::NESTABLE));
  return false;
}

bool TestTaskRunner::RunsTasksOnCurrentThread() const {
  return true;
}

const std::vector<PostedTask>& TestTaskRunner::GetPostedTasks() const {
  return tasks_;
}

void TestTaskRunner::RunNextTask() {
  // Find the next task to run, advance the time to the correct time
  // and then run the task.
  std::vector<PostedTask>::iterator next = FindNextTask();
  DCHECK(next != tasks_.end());
  clock_->AdvanceTime(QuicTime::Delta::FromMicroseconds(
      (next->GetTimeToRun() - clock_->NowInTicks()).InMicroseconds()));
  PostedTask task = *next;
  tasks_.erase(next);
  task.task.Run();
}

namespace {

struct ShouldRunBeforeLessThan {
  bool operator()(const PostedTask& task1, const PostedTask& task2) const {
    return task1.ShouldRunBefore(task2);
  }
};

}  // namespace

std::vector<PostedTask>::iterator TestTaskRunner::FindNextTask() {
  return std::min_element(
      tasks_.begin(), tasks_.end(), ShouldRunBeforeLessThan());
}

}  // namespace test
}  // namespace net