Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 2 | // 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 Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 7 | #ifndef NET_QUIC_TEST_TASK_RUNNER_H_ |
8 | #define NET_QUIC_TEST_TASK_RUNNER_H_ | ||||
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 9 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 10 | #include <vector> |
11 | |||||
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 12 | #include "base/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 14 | #include "base/task/sequenced_task_runner.h" |
15 | #include "base/task/task_runner.h" | ||||
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 16 | #include "base/test/test_pending_task.h" |
Ryan Hamilton | ea4fa19 | 2022-04-12 18:30:49 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quiche/quic/core/quic_time.h" |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 18 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 19 | namespace quic { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 20 | class MockClock; |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 21 | } // namespace quic |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 22 | namespace net::test { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 23 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 24 | typedef base::TestPendingTask PostedTask; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 25 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 26 | class TestTaskRunner : public base::SequencedTaskRunner { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 27 | public: |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 28 | explicit TestTaskRunner(quic::MockClock* clock); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 29 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 30 | TestTaskRunner(const TestTaskRunner&) = delete; |
31 | TestTaskRunner& operator=(const TestTaskRunner&) = delete; | ||||
32 | |||||
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 33 | // base::TaskRunner implementation. |
Brett Wilson | 9c36199 | 2017-09-12 06:05:21 | [diff] [blame] | 34 | bool PostDelayedTask(const base::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 35 | base::OnceClosure task, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 36 | base::TimeDelta delay) override; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 37 | bool PostNonNestableDelayedTask(const base::Location& from_here, |
38 | base::OnceClosure task, | ||||
39 | base::TimeDelta delay) override; | ||||
40 | |||||
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 41 | bool RunsTasksInCurrentSequence() const override; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 42 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 43 | const std::vector<PostedTask>& GetPostedTasks() const; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 44 | |
Zhongyi Shi | a6b68d11 | 2018-09-24 07:49:03 | [diff] [blame] | 45 | // 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 | |||||
rch | 9ecde09b | 2017-04-08 00:18:23 | [diff] [blame] | 49 | // Finds the next task to run, advances the time to the correct time |
50 | // and then runs the task. | ||||
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 51 | void RunNextTask(); |
52 | |||||
Zhongyi Shi | a6b68d11 | 2018-09-24 07:49:03 | [diff] [blame] | 53 | // 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 | |||||
rch | 9ecde09b | 2017-04-08 00:18:23 | [diff] [blame] | 58 | // 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] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 62 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 63 | ~TestTaskRunner() override; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 64 | |
65 | private: | ||||
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 66 | std::vector<PostedTask>::iterator FindNextTask(); |
67 | |||||
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 68 | const raw_ptr<quic::MockClock> clock_; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 69 | std::vector<PostedTask> tasks_; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 70 | }; |
71 | |||||
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 72 | } // namespace net::test |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 73 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 74 | #endif // NET_QUIC_TEST_TASK_RUNNER_H_ |