| 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 |
| 13 | #include <functional> |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 |
| 17 | #include "api/task_queue/task_queue_factory.h" |
| 18 | #include "api/units/time_delta.h" |
| 19 | #include "api/units/timestamp.h" |
| 20 | #include "rtc_base/synchronization/yield_policy.h" |
| 21 | #include "rtc_base/thread.h" |
| 22 | #include "system_wrappers/include/clock.h" |
| 23 |
| 24 | namespace webrtc { |
| 25 | // Interface for controlling time progress. This allows us to execute test code |
| 26 | // in either real time or simulated time by using different implementation of |
| 27 | // this interface. |
| 28 | class TimeController { |
| 29 | public: |
| 30 | virtual ~TimeController() = default; |
| 31 | // Provides a clock instance that follows implementation defined time |
| 32 | // progress. |
| 33 | virtual Clock* GetClock() = 0; |
| 34 | // The returned factory will created task queues that runs in implementation |
| 35 | // defined time domain. |
| 36 | virtual TaskQueueFactory* GetTaskQueueFactory() = 0; |
| 37 | // Simple helper to create an owned factory that can be used as a parameter |
| 38 | // for PeerConnectionFactory. Note that this might depend on the underlying |
| 39 | // time controller and therfore must be destroyed before the time controller |
| 40 | // is destroyed. |
| 41 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueFactory(); |
| 42 |
| 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 |
| 50 | // Creates an rtc::Thread instance that ensure that it's set as the current |
| 51 | // thread. |
| 52 | virtual rtc::Thread* GetMainThread() = 0; |
| 53 | // Allow task queues and process threads created by this instance to execute |
| 54 | // for the given `duration`. |
| 55 | virtual void AdvanceTime(TimeDelta duration) = 0; |
| 56 |
| 57 | // Waits until condition() == true, polling condition() in small time |
| 58 | // intervals. |
| 59 | // Returns true if condition() was evaluated to true before `max_duration` |
| 60 | // elapsed and false otherwise. |
| 61 | bool Wait(const std::function<bool()>& condition, |
| 62 | TimeDelta max_duration = TimeDelta::Seconds(5)); |
| 63 | }; |
| 64 |
| 65 | // Interface for telling time, scheduling an event to fire at a particular time, |
| 66 | // and waiting for time to pass. |
| 67 | class ControlledAlarmClock { |
| 68 | public: |
| 69 | virtual ~ControlledAlarmClock() = default; |
| 70 |
| 71 | // Gets a clock that tells the alarm clock's notion of time. |
| 72 | virtual Clock* GetClock() = 0; |
| 73 |
| 74 | // Schedules the alarm to fire at `deadline`. |
| 75 | // An alarm clock only supports one deadline. Calls to `ScheduleAlarmAt` with |
| 76 | // an earlier deadline will reset the alarm to fire earlier.Calls to |
| 77 | // `ScheduleAlarmAt` with a later deadline are ignored. Returns true if the |
| 78 | // deadline changed, false otherwise. |
| 79 | virtual bool ScheduleAlarmAt(Timestamp deadline) = 0; |
| 80 |
| 81 | // Sets the callback that should be run when the alarm fires. |
| 82 | virtual void SetCallback(std::function<void()> callback) = 0; |
| 83 |
| 84 | // Waits for `duration` to pass, according to the alarm clock. |
| 85 | virtual void Sleep(TimeDelta duration) = 0; |
| 86 | }; |
| 87 |
| 88 | } // namespace webrtc |
| 89 | #endif // API_TEST_TIME_CONTROLLER_H_ |
| 90 |