Skip to content

Inject TimeProvider for testable, UTC-based expiration #40

Description

@matthewdevenny

Problem

The cache uses DateTimeOffset.Now throughout for expiration logic (src/NatsDistributedCache/NatsCache.cs — TTL computation, entry creation, absolute-expiration checks, sliding-window updates). This uses local time and makes expiration behavior impossible to unit-test without real wall-clock delays.

Proposal

Inject TimeProvider (default TimeProvider.System) into NatsCache and replace every DateTimeOffset.Now with _timeProvider.GetUtcNow().

This is the .NET 8+ idiom and enables deterministic, instant expiration unit tests via FakeTimeProvider (Microsoft.Extensions.TimeProvider.Testing), removing reliance on Task.Delay in the time-expiration tests.

Acceptance criteria

  • No DateTimeOffset.Now remains in the library.
  • Time-expiration unit tests drive time with FakeTimeProvider (no real delays).
  • DI wiring resolves an optional TimeProvider, defaulting to TimeProvider.System.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions