The open source OpenXR runtime
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Utilities for tests involving time points and durations
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 */
8
9#pragma once
10
11#include <chrono>
12
13using unanoseconds = std::chrono::duration<int64_t, std::nano>;
14
15class MockClock
16{
17public:
18 int64_t
19 now() const noexcept
20 {
21 return std::chrono::duration_cast<unanoseconds>(now_.time_since_epoch()).count();
22 }
23
24 std::chrono::steady_clock::time_point
25 now_typed() const noexcept
26 {
27 return now_;
28 }
29
30 void
31 advance(unanoseconds ns)
32 {
33 now_ += ns;
34 }
35
36 void
37 advance_to(int64_t timestamp_ns)
38 {
39 CHECK(now() <= timestamp_ns);
40 now_ = std::chrono::steady_clock::time_point(
41 std::chrono::steady_clock::duration(unanoseconds(timestamp_ns)));
42 }
43
44private:
45 std::chrono::steady_clock::time_point now_{std::chrono::steady_clock::duration(std::chrono::seconds(1000000))};
46};
47
48struct FutureEvent
49{
50 std::chrono::steady_clock::time_point time_point;
51 std::function<void()> action;
52};