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> |
| 15 | |
| 16 | #include "api/task_queue/task_queue_factory.h" |
| 17 | #include "api/units/time_delta.h" |
| 18 | #include "api/units/timestamp.h" |
| 19 | #include "modules/utility/include/process_thread.h" |
| 20 | #include "rtc_base/synchronization/yield_policy.h" |
| 21 | #include "system_wrappers/include/clock.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 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 | // Creates a process thread. |
| 38 | virtual std::unique_ptr<ProcessThread> CreateProcessThread( |
| 39 | const char* thread_name) = 0; |
| 40 | // Allow task queues and process threads created by this instance to execute |
| 41 | // for the given |duration|. |
Markus Handell | 486cc55 | 2019-12-03 14:37:28 +0100 | [diff] [blame] | 42 | virtual void AdvanceTime(TimeDelta duration) = 0; |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | // Interface for telling time, scheduling an event to fire at a particular time, |
| 46 | // and waiting for time to pass. |
| 47 | class ControlledAlarmClock { |
| 48 | public: |
| 49 | virtual ~ControlledAlarmClock() = default; |
| 50 | |
| 51 | // Gets a clock that tells the alarm clock's notion of time. |
| 52 | virtual Clock* GetClock() = 0; |
| 53 | |
| 54 | // Schedules the alarm to fire at |deadline|. |
| 55 | // An alarm clock only supports one deadline. Calls to |ScheduleAlarmAt| with |
| 56 | // an earlier deadline will reset the alarm to fire earlier.Calls to |
| 57 | // |ScheduleAlarmAt| with a later deadline are ignored. Returns true if the |
| 58 | // deadline changed, false otherwise. |
| 59 | virtual bool ScheduleAlarmAt(Timestamp deadline) = 0; |
| 60 | |
| 61 | // Sets the callback that should be run when the alarm fires. |
| 62 | virtual void SetCallback(std::function<void()> callback) = 0; |
| 63 | |
| 64 | // Waits for |duration| to pass, according to the alarm clock. |
| 65 | virtual void Sleep(TimeDelta duration) = 0; |
| 66 | }; |
| 67 | |
| 68 | } // namespace webrtc |
| 69 | #endif // API_TEST_TIME_CONTROLLER_H_ |