blob: 6c47e91575f91daeb605045459980ad5444dee92 [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"
20#include "modules/utility/include/process_thread.h"
21#include "rtc_base/synchronization/yield_policy.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010022#include "rtc_base/thread.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080023#include "system_wrappers/include/clock.h"
24
25namespace webrtc {
26
27// Interface for controlling time progress. This allows us to execute test code
28// in either real time or simulated time by using different implementation of
29// this interface.
30class TimeController {
31 public:
32 virtual ~TimeController() = default;
33 // Provides a clock instance that follows implementation defined time
34 // progress.
35 virtual Clock* GetClock() = 0;
36 // The returned factory will created task queues that runs in implementation
37 // defined time domain.
38 virtual TaskQueueFactory* GetTaskQueueFactory() = 0;
39 // Creates a process thread.
40 virtual std::unique_ptr<ProcessThread> CreateProcessThread(
41 const char* thread_name) = 0;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010042 // Creates an rtc::Thread instance. If |socket_server| is nullptr, a default
43 // noop socket server is created.
44 virtual std::unique_ptr<rtc::Thread> CreateThread(
45 const std::string& name,
46 std::unique_ptr<rtc::SocketServer> socket_server = nullptr) = 0;
47
48 // Creates an rtc::Thread instance that ensure that it's set as the current
49 // thread.
50 virtual rtc::Thread* GetMainThread() = 0;
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080051 // Allow task queues and process threads created by this instance to execute
52 // for the given |duration|.
Markus Handell486cc552019-12-03 14:37:28 +010053 virtual void AdvanceTime(TimeDelta duration) = 0;
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080054};
55
56// Interface for telling time, scheduling an event to fire at a particular time,
57// and waiting for time to pass.
58class ControlledAlarmClock {
59 public:
60 virtual ~ControlledAlarmClock() = default;
61
62 // Gets a clock that tells the alarm clock's notion of time.
63 virtual Clock* GetClock() = 0;
64
65 // Schedules the alarm to fire at |deadline|.
66 // An alarm clock only supports one deadline. Calls to |ScheduleAlarmAt| with
67 // an earlier deadline will reset the alarm to fire earlier.Calls to
68 // |ScheduleAlarmAt| with a later deadline are ignored. Returns true if the
69 // deadline changed, false otherwise.
70 virtual bool ScheduleAlarmAt(Timestamp deadline) = 0;
71
72 // Sets the callback that should be run when the alarm fires.
73 virtual void SetCallback(std::function<void()> callback) = 0;
74
75 // Waits for |duration| to pass, according to the alarm clock.
76 virtual void Sleep(TimeDelta duration) = 0;
77};
78
79} // namespace webrtc
80#endif // API_TEST_TIME_CONTROLLER_H_