time_controller.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef API_TEST_TIME_CONTROLLER_H_
  11. #define API_TEST_TIME_CONTROLLER_H_
  12. #include <functional>
  13. #include <memory>
  14. #include <string>
  15. #include "api/task_queue/task_queue_factory.h"
  16. #include "api/units/time_delta.h"
  17. #include "api/units/timestamp.h"
  18. #include "modules/utility/include/process_thread.h"
  19. #include "rtc_base/synchronization/yield_policy.h"
  20. #include "rtc_base/thread.h"
  21. #include "system_wrappers/include/clock.h"
  22. namespace webrtc {
  23. // Interface for controlling time progress. This allows us to execute test code
  24. // in either real time or simulated time by using different implementation of
  25. // this interface.
  26. class TimeController {
  27. public:
  28. virtual ~TimeController() = default;
  29. // Provides a clock instance that follows implementation defined time
  30. // progress.
  31. virtual Clock* GetClock() = 0;
  32. // The returned factory will created task queues that runs in implementation
  33. // defined time domain.
  34. virtual TaskQueueFactory* GetTaskQueueFactory() = 0;
  35. // Simple helper to create an owned factory that can be used as a parameter
  36. // for PeerConnectionFactory. Note that this might depend on the underlying
  37. // time controller and therfore must be destroyed before the time controller
  38. // is destroyed.
  39. std::unique_ptr<TaskQueueFactory> CreateTaskQueueFactory();
  40. // Creates a process thread.
  41. virtual std::unique_ptr<ProcessThread> CreateProcessThread(
  42. const char* thread_name) = 0;
  43. // Creates an rtc::Thread instance. If |socket_server| is nullptr, a default
  44. // noop socket server is created.
  45. virtual std::unique_ptr<rtc::Thread> CreateThread(
  46. const std::string& name,
  47. std::unique_ptr<rtc::SocketServer> socket_server = nullptr) = 0;
  48. // Creates an rtc::Thread instance that ensure that it's set as the current
  49. // thread.
  50. virtual rtc::Thread* GetMainThread() = 0;
  51. // Allow task queues and process threads created by this instance to execute
  52. // for the given |duration|.
  53. virtual void AdvanceTime(TimeDelta duration) = 0;
  54. // Waits until condition() == true, polling condition() in small time
  55. // intervals.
  56. bool Wait(const std::function<bool()>& condition,
  57. TimeDelta max_duration = TimeDelta::Seconds(5));
  58. };
  59. // Interface for telling time, scheduling an event to fire at a particular time,
  60. // and waiting for time to pass.
  61. class ControlledAlarmClock {
  62. public:
  63. virtual ~ControlledAlarmClock() = default;
  64. // Gets a clock that tells the alarm clock's notion of time.
  65. virtual Clock* GetClock() = 0;
  66. // Schedules the alarm to fire at |deadline|.
  67. // An alarm clock only supports one deadline. Calls to |ScheduleAlarmAt| with
  68. // an earlier deadline will reset the alarm to fire earlier.Calls to
  69. // |ScheduleAlarmAt| with a later deadline are ignored. Returns true if the
  70. // deadline changed, false otherwise.
  71. virtual bool ScheduleAlarmAt(Timestamp deadline) = 0;
  72. // Sets the callback that should be run when the alarm fires.
  73. virtual void SetCallback(std::function<void()> callback) = 0;
  74. // Waits for |duration| to pass, according to the alarm clock.
  75. virtual void Sleep(TimeDelta duration) = 0;
  76. };
  77. } // namespace webrtc
  78. #endif // API_TEST_TIME_CONTROLLER_H_