xref: /aosp_15_r20/external/cronet/net/quic/test_task_runner.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 
7 #ifndef NET_QUIC_TEST_TASK_RUNNER_H_
8 #define NET_QUIC_TEST_TASK_RUNNER_H_
9 
10 #include <vector>
11 
12 #include "base/functional/callback.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/task/sequenced_task_runner.h"
15 #include "base/task/task_runner.h"
16 #include "base/test/test_pending_task.h"
17 #include "net/third_party/quiche/src/quiche/quic/core/quic_time.h"
18 
19 namespace quic {
20 class MockClock;
21 }  // namespace quic
22 namespace net::test {
23 
24 typedef base::TestPendingTask PostedTask;
25 
26 class TestTaskRunner : public base::SequencedTaskRunner {
27  public:
28   explicit TestTaskRunner(quic::MockClock* clock);
29 
30   TestTaskRunner(const TestTaskRunner&) = delete;
31   TestTaskRunner& operator=(const TestTaskRunner&) = delete;
32 
33   // base::TaskRunner implementation.
34   bool PostDelayedTask(const base::Location& from_here,
35                        base::OnceClosure task,
36                        base::TimeDelta delay) override;
37   bool PostNonNestableDelayedTask(const base::Location& from_here,
38                                   base::OnceClosure task,
39                                   base::TimeDelta delay) override;
40 
41   bool RunsTasksInCurrentSequence() const override;
42 
43   const std::vector<PostedTask>& GetPostedTasks() const;
44 
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 
49   // Finds the next task to run, advances the time to the correct time
50   // and then runs the task.
51   void RunNextTask();
52 
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 
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 
62  protected:
63   ~TestTaskRunner() override;
64 
65  private:
66   std::vector<PostedTask>::iterator FindNextTask();
67 
68   const raw_ptr<quic::MockClock> clock_;
69   std::vector<PostedTask> tasks_;
70 };
71 
72 }  // namespace net::test
73 
74 #endif  // NET_QUIC_TEST_TASK_RUNNER_H_
75