Files
Library/tests/standalone/Time/ClockSaturation.cpp
T
faumarayandClaude Opus 5.5 99a1100bb1 feat(async,time): add injected-clock TimerQueue and clock saturation helpers (M-TIME-03 B1)
- NEW Async/Timer.hpp: TimerQueue (stable (deadline, seq) min-heap, bounded
  tombstones, explicit poll_expired, callbacks outside the lock, close/destroy
  deliver TimerEvent::closed), shared-state TimerHandle safe after queue
  destruction, sleep_until/sleep_for settling exactly once and resuming via
  post_completion; documented sleep-task lifetime and cancel-under-lock rules
- Time/Clock.hpp: saturating_add, deadline_after
- Tests: TimerQueue (15), DeadlineDomains (2), ClockSaturation (1); race loops
  count failures to stay under the runner's 1,000-assertion retention (X-QA-02)

Review: W11-REVIEW-B1 (CHANGES_REQUIRED F1-F3 fixed by root: test counters,
header lifetime/cancel docs)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:36:37 +03:00

113 lines
4.7 KiB
C++

#include <Void/Time/Clock.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <cstdint>
#include <limits>
#include <utility>
using namespace std::chrono_literals;
namespace
{
using TimePoint = Void::Time::MonotonicClock::time_point;
constexpr std::int64_t kMax = std::numeric_limits<std::int64_t>::max();
constexpr std::int64_t kMin = std::numeric_limits<std::int64_t>::min();
std::int64_t ns_of(TimePoint tp)
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(
tp.time_since_epoch())
.count();
}
TimePoint at_ns(std::int64_t ns)
{
return TimePoint{std::chrono::duration_cast<TimePoint::duration>(
std::chrono::nanoseconds{ns})};
}
} // namespace
Void::Testing::Test clock_saturating_add_and_deadline_after_bounds()
{
using Void::Time::deadline_after;
using Void::Time::saturating_add;
static_assert(noexcept(saturating_add(std::declval<TimePoint>(),
std::declval<std::chrono::nanoseconds>())));
static_assert(noexcept(
deadline_after(std::declval<const Void::Time::MonotonicClock&>(),
std::declval<std::chrono::nanoseconds>())));
// In-range arithmetic, positive and negative steps.
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(0), 5ns)), std::int64_t{5});
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(10), -3ns)), std::int64_t{7});
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(-10), 0ns)), std::int64_t{-10});
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(-1), std::chrono::nanoseconds::max())),
kMax - 1);
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(0), std::chrono::nanoseconds::max())),
kMax);
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(0), std::chrono::nanoseconds::min())),
kMin);
Void::Testing::expect_eq(ns_of(saturating_add(at_ns(1), std::chrono::nanoseconds::min())),
kMin + 1);
// Positive overflow clamps to time_point::max().
Void::Testing::expect(saturating_add(at_ns(kMax - 1), 2ns) == TimePoint::max());
Void::Testing::expect(saturating_add(TimePoint::max(), 1ns) == TimePoint::max());
Void::Testing::expect(saturating_add(at_ns(1), std::chrono::nanoseconds::max()) ==
TimePoint::max());
Void::Testing::expect(saturating_add(TimePoint::max(), std::chrono::nanoseconds::max()) ==
TimePoint::max());
Void::Testing::expect(saturating_add(TimePoint::max(), 0ns) == TimePoint::max());
// Negative overflow clamps to time_point::min().
Void::Testing::expect(saturating_add(at_ns(kMin + 1), -2ns) == TimePoint::min());
Void::Testing::expect(saturating_add(TimePoint::min(), -1ns) == TimePoint::min());
Void::Testing::expect(saturating_add(at_ns(-1), std::chrono::nanoseconds::min()) ==
TimePoint::min());
Void::Testing::expect(saturating_add(TimePoint::min(), std::chrono::nanoseconds::min()) ==
TimePoint::min());
// Extremes in the other direction stay exact.
Void::Testing::expect_eq(ns_of(saturating_add(TimePoint::max(), std::chrono::nanoseconds::min())),
std::int64_t{-1});
Void::Testing::expect_eq(ns_of(saturating_add(TimePoint::min(), std::chrono::nanoseconds::max())),
std::int64_t{-1});
// deadline_after uses the injected clock's own domain, never the host clock.
Void::Time::ManualClock epoch_clock;
Void::Testing::expect_eq(ns_of(deadline_after(epoch_clock, 1s)),
std::int64_t{1'000'000'000});
Void::Testing::expect_eq(ns_of(deadline_after(epoch_clock, -5ns)), std::int64_t{-5});
Void::Testing::expect(deadline_after(epoch_clock, std::chrono::nanoseconds::max()) ==
TimePoint::max());
Void::Testing::expect(deadline_after(epoch_clock, std::chrono::nanoseconds::min()) ==
TimePoint::min());
Void::Time::ManualClock offset_clock{at_ns(100)};
Void::Testing::expect_eq(ns_of(deadline_after(offset_clock, 50ns)), std::int64_t{150});
Void::Testing::require(offset_clock.advance(25ns).has_value());
Void::Testing::expect_eq(ns_of(deadline_after(offset_clock, 50ns)), std::int64_t{175});
Void::Time::ManualClock far_clock{at_ns(kMax - 10)};
Void::Testing::expect_eq(ns_of(deadline_after(far_clock, 10ns)), kMax);
Void::Testing::expect(deadline_after(far_clock, 1s) == TimePoint::max());
Void::Testing::expect(deadline_after(far_clock, std::chrono::nanoseconds::max()) ==
TimePoint::max());
Void::Testing::expect_eq(ns_of(deadline_after(far_clock, -10ns)), kMax - 20);
// A saturated deadline is consistent with remaining_until (no wrap to 0).
Void::Testing::expect_eq(
Void::Time::remaining_until(epoch_clock,
deadline_after(epoch_clock, std::chrono::nanoseconds::max())),
std::chrono::nanoseconds::max());
Void::Testing::expect_eq(
Void::Time::remaining_until(far_clock, deadline_after(far_clock, 1h)),
std::chrono::nanoseconds{10});
}