blob: 121f65cea9a056676b79a99d4258484004c350d1 [file] [log] [blame]
Bjorn A Mellemc4f86542019-11-21 10:37:18 -08001/*
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 Janssonfc8279d2020-01-16 11:45:59 +010015#include <string>
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080016
17#include "api/task_queue/task_queue_factory.h"
18#include "api/units/time_delta.h"
19#include "api/units/timestamp.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080020#include "rtc_base/synchronization/yield_policy.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010021#include "rtc_base/thread.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080022#include "system_wrappers/include/clock.h"
23
24namespace webrtc {
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080025// 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.
28class 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;
Sebastian Jansson094ce2e2020-01-22 14:37:52 +010037 // 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
Artem Titov0e61fdd2021-07-25 21:50:14 +020043 // Creates an rtc::Thread instance. If `socket_server` is nullptr, a default
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010044 // noop socket server is created.
Artem Titov0ef4a242020-07-06 10:57:28 +020045 // Returned thread is not null and started.
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010046 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;
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080053 // Allow task queues and process threads created by this instance to execute
Artem Titov0e61fdd2021-07-25 21:50:14 +020054 // for the given `duration`.
Markus Handell486cc552019-12-03 14:37:28 +010055 virtual void AdvanceTime(TimeDelta duration) = 0;
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010056
Artem Titovb5a01382020-04-29 11:09:59 +020057 // Waits until condition() == true, polling condition() in small time
58 // intervals.
Artem Titov0e61fdd2021-07-25 21:50:14 +020059 // Returns true if condition() was evaluated to true before `max_duration`
Artem Titov656efbe2020-07-07 13:35:28 +020060 // elapsed and false otherwise.
Artem Titovb5a01382020-04-29 11:09:59 +020061 bool Wait(const std::function<bool()>& condition,
Danil Chapovalov0c626af2020-02-10 11:16:00 +010062 TimeDelta max_duration = TimeDelta::Seconds(5));
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080063};
64
65// Interface for telling time, scheduling an event to fire at a particular time,
66// and waiting for time to pass.
67class 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
Artem Titov0e61fdd2021-07-25 21:50:14 +020074 // Schedules the alarm to fire at `deadline`.
75 // An alarm clock only supports one deadline. Calls to `ScheduleAlarmAt` with
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080076 // an earlier deadline will reset the alarm to fire earlier.Calls to
Artem Titov0e61fdd2021-07-25 21:50:14 +020077 // `ScheduleAlarmAt` with a later deadline are ignored. Returns true if the
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080078 // 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
Artem Titov0e61fdd2021-07-25 21:50:14 +020084 // Waits for `duration` to pass, according to the alarm clock.
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080085 virtual void Sleep(TimeDelta duration) = 0;
86};
87
88} // namespace webrtc
89#endif // API_TEST_TIME_CONTROLLER_H_