blob: 4723716313995d6022382775ccd2e78560eb5dc8 [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>
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
23namespace 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.
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;
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|.
42 virtual void Sleep(TimeDelta duration) = 0;
43 // Execute closure in an implementation defined scope where rtc::Event::Wait
44 // might yield to execute other tasks. This allows doing blocking waits on
45 // tasks on other task queues froma a task queue without deadlocking.
46 virtual void InvokeWithControlledYield(std::function<void()> closure) = 0;
47 // Returns a YieldInterface which can be installed as a ScopedYieldPolicy.
48 virtual rtc::YieldInterface* YieldInterface() = 0;
49};
50
51// Interface for telling time, scheduling an event to fire at a particular time,
52// and waiting for time to pass.
53class ControlledAlarmClock {
54 public:
55 virtual ~ControlledAlarmClock() = default;
56
57 // Gets a clock that tells the alarm clock's notion of time.
58 virtual Clock* GetClock() = 0;
59
60 // Schedules the alarm to fire at |deadline|.
61 // An alarm clock only supports one deadline. Calls to |ScheduleAlarmAt| with
62 // an earlier deadline will reset the alarm to fire earlier.Calls to
63 // |ScheduleAlarmAt| with a later deadline are ignored. Returns true if the
64 // deadline changed, false otherwise.
65 virtual bool ScheduleAlarmAt(Timestamp deadline) = 0;
66
67 // Sets the callback that should be run when the alarm fires.
68 virtual void SetCallback(std::function<void()> callback) = 0;
69
70 // Waits for |duration| to pass, according to the alarm clock.
71 virtual void Sleep(TimeDelta duration) = 0;
72};
73
74} // namespace webrtc
75#endif // API_TEST_TIME_CONTROLLER_H_