blob: edb507becbcb09264aac586b998b94f56e139761 [file] [log] [blame]
Taylor Brandstetterb3c68102016-05-27 21:15:431/*
2 * Copyright 2016 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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_FAKE_CLOCK_H_
12#define RTC_BASE_FAKE_CLOCK_H_
Taylor Brandstetterb3c68102016-05-27 21:15:4313
Yves Gerey3e707812018-11-28 15:47:4914#include <stdint.h>
15
Sebastian Jansson5f83cf02018-05-08 12:52:2216#include "api/units/time_delta.h"
Sebastian Janssond624c392019-04-17 08:36:0317#include "api/units/timestamp.h"
Markus Handell18523c32020-07-08 15:55:5818#include "rtc_base/synchronization/mutex.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/time_utils.h"
Taylor Brandstetterb3c68102016-05-27 21:15:4321
Henrik Kjellanderec78f1c2017-06-29 05:52:5022namespace rtc {
23
24// Fake clock for use with unit tests, which does not tick on its own.
25// Starts at time 0.
26//
27// TODO(deadbeef): Unify with webrtc::SimulatedClock.
28class FakeClock : public ClockInterface {
29 public:
Sebastian Janssond624c392019-04-17 08:36:0330 FakeClock() = default;
31 FakeClock(const FakeClock&) = delete;
32 FakeClock& operator=(const FakeClock&) = delete;
33 ~FakeClock() override = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:5034
35 // ClockInterface implementation.
36 int64_t TimeNanos() const override;
37
38 // Methods that can be used by the test to control the time.
39
40 // Should only be used to set a time in the future.
Sebastian Janssond624c392019-04-17 08:36:0341 void SetTime(webrtc::Timestamp new_time);
Henrik Kjellanderec78f1c2017-06-29 05:52:5042
Sebastian Jansson5f83cf02018-05-08 12:52:2243 void AdvanceTime(webrtc::TimeDelta delta);
Sebastian Janssond624c392019-04-17 08:36:0344
45 private:
Markus Handell18523c32020-07-08 15:55:5846 mutable webrtc::Mutex lock_;
Sebastian Janssond624c392019-04-17 08:36:0347 int64_t time_ns_ RTC_GUARDED_BY(lock_) = 0;
48};
49
50class ThreadProcessingFakeClock : public ClockInterface {
51 public:
52 int64_t TimeNanos() const override { return clock_.TimeNanos(); }
53 void SetTime(webrtc::Timestamp time);
Sebastian Janssond624c392019-04-17 08:36:0354 void AdvanceTime(webrtc::TimeDelta delta);
Sebastian Jansson40889f32019-04-17 10:11:2055
Henrik Kjellanderec78f1c2017-06-29 05:52:5056 private:
Sebastian Janssond624c392019-04-17 08:36:0357 FakeClock clock_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5058};
59
60// Helper class that sets itself as the global clock in its constructor and
61// unsets it in its destructor.
Sebastian Janssond624c392019-04-17 08:36:0362class ScopedBaseFakeClock : public FakeClock {
63 public:
64 ScopedBaseFakeClock();
65 ~ScopedBaseFakeClock() override;
66
67 private:
68 ClockInterface* prev_clock_;
69};
70
71// TODO(srte): Rename this to reflect that it also does thread processing.
72class ScopedFakeClock : public ThreadProcessingFakeClock {
Henrik Kjellanderec78f1c2017-06-29 05:52:5073 public:
74 ScopedFakeClock();
75 ~ScopedFakeClock() override;
76
77 private:
78 ClockInterface* prev_clock_;
79};
80
Henrik Kjellanderec78f1c2017-06-29 05:52:5081} // namespace rtc
Taylor Brandstetterb3c68102016-05-27 21:15:4382
Steve Anton10542f22019-01-11 17:11:0083#endif // RTC_BASE_FAKE_CLOCK_H_