test_task_runner.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Common utilities for Quic tests
  6. #ifndef NET_QUIC_TEST_TASK_RUNNER_H_
  7. #define NET_QUIC_TEST_TASK_RUNNER_H_
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "base/task/task_runner.h"
  13. #include "base/test/test_pending_task.h"
  14. #include "net/third_party/quiche/src/quiche/quic/core/quic_time.h"
  15. namespace quic {
  16. class MockClock;
  17. } // namespace quic
  18. namespace net::test {
  19. typedef base::TestPendingTask PostedTask;
  20. class TestTaskRunner : public base::SequencedTaskRunner {
  21. public:
  22. explicit TestTaskRunner(quic::MockClock* clock);
  23. TestTaskRunner(const TestTaskRunner&) = delete;
  24. TestTaskRunner& operator=(const TestTaskRunner&) = delete;
  25. // base::TaskRunner implementation.
  26. bool PostDelayedTask(const base::Location& from_here,
  27. base::OnceClosure task,
  28. base::TimeDelta delay) override;
  29. bool PostNonNestableDelayedTask(const base::Location& from_here,
  30. base::OnceClosure task,
  31. base::TimeDelta delay) override;
  32. bool RunsTasksInCurrentSequence() const override;
  33. const std::vector<PostedTask>& GetPostedTasks() const;
  34. // Returns the delay for next task to run. If there is no pending task,
  35. // return QuicTime::Delta::Infinite().
  36. quic::QuicTime::Delta NextPendingTaskDelay();
  37. // Finds the next task to run, advances the time to the correct time
  38. // and then runs the task.
  39. void RunNextTask();
  40. // Fast forwards virtual time by |delta|, causing all tasks with a remaining
  41. // delay less than or equal to |delta| to be executed. |delta| must be
  42. // non-negative.
  43. void FastForwardBy(quic::QuicTime::Delta delta);
  44. // While there are posted tasks, finds the next task to run, advances the
  45. // time to the correct time and then runs the task.
  46. void RunUntilIdle();
  47. protected:
  48. ~TestTaskRunner() override;
  49. private:
  50. std::vector<PostedTask>::iterator FindNextTask();
  51. const raw_ptr<quic::MockClock> clock_;
  52. std::vector<PostedTask> tasks_;
  53. };
  54. } // namespace net::test
  55. #endif // NET_QUIC_TEST_TASK_RUNNER_H_