// Copyright 2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file * @brief Utilities for tests involving time points and durations * @author Rylie Pavlik */ #pragma once #include using unanoseconds = std::chrono::duration; class MockClock { public: int64_t now() const noexcept { return std::chrono::duration_cast(now_.time_since_epoch()).count(); } std::chrono::steady_clock::time_point now_typed() const noexcept { return now_; } void advance(unanoseconds ns) { now_ += ns; } void advance_to(int64_t timestamp_ns) { CHECK(now() <= timestamp_ns); now_ = std::chrono::steady_clock::time_point( std::chrono::steady_clock::duration(unanoseconds(timestamp_ns))); } private: std::chrono::steady_clock::time_point now_{std::chrono::steady_clock::duration(std::chrono::seconds(1000000))}; }; struct FutureEvent { std::chrono::steady_clock::time_point time_point; std::function action; };