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