time_controller.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Returned thread is not null and started.
  46. virtual std::unique_ptr<rtc::Thread> CreateThread(
  47. const std::string& name,
  48. std::unique_ptr<rtc::SocketServer> socket_server = nullptr) = 0;
  49. // Creates an rtc::Thread instance that ensure that it's set as the current
  50. // thread.
  51. virtual rtc::Thread* GetMainThread() = 0;
  52. // Allow task queues and process threads created by this instance to execute
  53. // for the given |duration|.
  54. virtual void AdvanceTime(TimeDelta duration) = 0;
  55. // Waits until condition() == true, polling condition() in small time
  56. // intervals.
  57. // Returns true if condition() was evaluated to true before |max_duration|
  58. // elapsed and false otherwise.
  59. bool Wait(const std::function<bool()>& condition,
  60. TimeDelta max_duration = TimeDelta::Seconds(5));
  61. };
  62. // Interface for telling time, scheduling an event to fire at a particular time,
  63. // and waiting for time to pass.
  64. class ControlledAlarmClock {
  65. public:
  66. virtual ~ControlledAlarmClock() = default;
  67. // Gets a clock that tells the alarm clock's notion of time.
  68. virtual Clock* GetClock() = 0;
  69. // Schedules the alarm to fire at |deadline|.
  70. // An alarm clock only supports one deadline. Calls to |ScheduleAlarmAt| with
  71. // an earlier deadline will reset the alarm to fire earlier.Calls to
  72. // |ScheduleAlarmAt| with a later deadline are ignored. Returns true if the
  73. // deadline changed, false otherwise.
  74. virtual bool ScheduleAlarmAt(Timestamp deadline) = 0;
  75. // Sets the callback that should be run when the alarm fires.
  76. virtual void SetCallback(std::function<void()> callback) = 0;
  77. // Waits for |duration| to pass, according to the alarm clock.
  78. virtual void Sleep(TimeDelta duration) = 0;
  79. };
  80. } // namespace webrtc
  81. #endif // API_TEST_TIME_CONTROLLER_H_