Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 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 | |
| 13 | #include <functional> |
| 14 | #include <memory> |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 15 | #include <string> |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 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 "modules/utility/include/process_thread.h" |
| 21 | #include "rtc_base/synchronization/yield_policy.h" |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 22 | #include "rtc_base/thread.h" |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
| 24 | |
| 25 | namespace webrtc { |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 26 | // Interface for controlling time progress. This allows us to execute test code |
| 27 | // in either real time or simulated time by using different implementation of |
| 28 | // this interface. |
| 29 | class TimeController { |
| 30 | public: |
| 31 | virtual ~TimeController() = default; |
| 32 | // Provides a clock instance that follows implementation defined time |
| 33 | // progress. |
| 34 | virtual Clock* GetClock() = 0; |
| 35 | // The returned factory will created task queues that runs in implementation |
| 36 | // defined time domain. |
| 37 | virtual TaskQueueFactory* GetTaskQueueFactory() = 0; |
Sebastian Jansson | 094ce2e | 2020-01-22 14:37:52 +0100 | [diff] [blame^] | 38 | // Simple helper to create an owned factory that can be used as a parameter |
| 39 | // for PeerConnectionFactory. Note that this might depend on the underlying |
| 40 | // time controller and therfore must be destroyed before the time controller |
| 41 | // is destroyed. |
| 42 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueFactory(); |
| 43 | |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 44 | // Creates a process thread. |
| 45 | virtual std::unique_ptr<ProcessThread> CreateProcessThread( |
| 46 | const char* thread_name) = 0; |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 47 | // Creates an rtc::Thread instance. If |socket_server| is nullptr, a default |
| 48 | // noop socket server is created. |
| 49 | virtual std::unique_ptr<rtc::Thread> CreateThread( |
| 50 | const std::string& name, |
| 51 | std::unique_ptr<rtc::SocketServer> socket_server = nullptr) = 0; |
| 52 | |
| 53 | // Creates an rtc::Thread instance that ensure that it's set as the current |
| 54 | // thread. |
| 55 | virtual rtc::Thread* GetMainThread() = 0; |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 56 | // Allow task queues and process threads created by this instance to execute |
| 57 | // for the given |duration|. |
Markus Handell | 486cc55 | 2019-12-03 14:37:28 +0100 | [diff] [blame] | 58 | virtual void AdvanceTime(TimeDelta duration) = 0; |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 59 | |
| 60 | // Waits until done() == true, polling done() in small time intervals. |
| 61 | bool Wait(const std::function<bool()>& done, |
| 62 | TimeDelta max_duration = TimeDelta::seconds(5)); |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 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_ |