blob: 5309a8dabd9882f640b05c7572f6fbb310bb9486 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]1d197ef52012-11-07 20:41:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Common utilities for Quic tests
6
Ryan Hamiltona3ee93a72018-08-01 22:03:087#ifndef NET_QUIC_TEST_TASK_RUNNER_H_
8#define NET_QUIC_TEST_TASK_RUNNER_H_
[email protected]1d197ef52012-11-07 20:41:299
[email protected]1d197ef52012-11-07 20:41:2910#include <vector>
11
tzik070c8ffb2017-03-29 05:28:1212#include "base/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
Patrick Monette643cdf62021-10-15 19:13:4214#include "base/task/sequenced_task_runner.h"
15#include "base/task/task_runner.h"
[email protected]a8582b12012-12-19 22:18:2916#include "base/test/test_pending_task.h"
Ryan Hamiltonea4fa192022-04-12 18:30:4917#include "net/third_party/quiche/src/quiche/quic/core/quic_time.h"
[email protected]1d197ef52012-11-07 20:41:2918
Ryan Hamilton8d9ee76e2018-05-29 23:52:5219namespace quic {
[email protected]a8582b12012-12-19 22:18:2920class MockClock;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5221} // namespace quic
Tsuyoshi Horo4f516be2022-06-14 11:53:1322namespace net::test {
[email protected]1d197ef52012-11-07 20:41:2923
[email protected]a8582b12012-12-19 22:18:2924typedef base::TestPendingTask PostedTask;
[email protected]1d197ef52012-11-07 20:41:2925
Zhongyi Shi8fff75b2017-11-19 21:36:3626class TestTaskRunner : public base::SequencedTaskRunner {
[email protected]1d197ef52012-11-07 20:41:2927 public:
Ryan Hamilton8d9ee76e2018-05-29 23:52:5228 explicit TestTaskRunner(quic::MockClock* clock);
[email protected]1d197ef52012-11-07 20:41:2929
Peter Boström407869b2021-10-07 04:42:4830 TestTaskRunner(const TestTaskRunner&) = delete;
31 TestTaskRunner& operator=(const TestTaskRunner&) = delete;
32
[email protected]a8582b12012-12-19 22:18:2933 // base::TaskRunner implementation.
Brett Wilson9c361992017-09-12 06:05:2134 bool PostDelayedTask(const base::Location& from_here,
tzik6e427842017-04-05 10:13:2135 base::OnceClosure task,
dchengb03027d2014-10-21 12:00:2036 base::TimeDelta delay) override;
Zhongyi Shi8fff75b2017-11-19 21:36:3637 bool PostNonNestableDelayedTask(const base::Location& from_here,
38 base::OnceClosure task,
39 base::TimeDelta delay) override;
40
peary23322df62017-05-09 03:55:4841 bool RunsTasksInCurrentSequence() const override;
[email protected]1d197ef52012-11-07 20:41:2942
[email protected]a8582b12012-12-19 22:18:2943 const std::vector<PostedTask>& GetPostedTasks() const;
[email protected]1d197ef52012-11-07 20:41:2944
Zhongyi Shia6b68d112018-09-24 07:49:0345 // Returns the delay for next task to run. If there is no pending task,
46 // return QuicTime::Delta::Infinite().
47 quic::QuicTime::Delta NextPendingTaskDelay();
48
rch9ecde09b2017-04-08 00:18:2349 // Finds the next task to run, advances the time to the correct time
50 // and then runs the task.
[email protected]1d197ef52012-11-07 20:41:2951 void RunNextTask();
52
Zhongyi Shia6b68d112018-09-24 07:49:0353 // Fast forwards virtual time by |delta|, causing all tasks with a remaining
54 // delay less than or equal to |delta| to be executed. |delta| must be
55 // non-negative.
56 void FastForwardBy(quic::QuicTime::Delta delta);
57
rch9ecde09b2017-04-08 00:18:2358 // While there are posted tasks, finds the next task to run, advances the
59 // time to the correct time and then runs the task.
60 void RunUntilIdle();
61
[email protected]1d197ef52012-11-07 20:41:2962 protected:
dchengb03027d2014-10-21 12:00:2063 ~TestTaskRunner() override;
[email protected]1d197ef52012-11-07 20:41:2964
65 private:
[email protected]a8582b12012-12-19 22:18:2966 std::vector<PostedTask>::iterator FindNextTask();
67
Keishi Hattori0e45c022021-11-27 09:25:5268 const raw_ptr<quic::MockClock> clock_;
[email protected]1d197ef52012-11-07 20:41:2969 std::vector<PostedTask> tasks_;
[email protected]1d197ef52012-11-07 20:41:2970};
71
Tsuyoshi Horo4f516be2022-06-14 11:53:1372} // namespace net::test
[email protected]1d197ef52012-11-07 20:41:2973
Ryan Hamiltona3ee93a72018-08-01 22:03:0874#endif // NET_QUIC_TEST_TASK_RUNNER_H_