Skip to content

Implement TimeProvider Tests#7

Open
DotNet-Ninja wants to merge 1 commit into
mainfrom
Testing
Open

Implement TimeProvider Tests#7
DotNet-Ninja wants to merge 1 commit into
mainfrom
Testing

Conversation

@DotNet-Ninja

@DotNet-Ninja DotNet-Ninja commented Jul 30, 2025

Copy link
Copy Markdown
Owner

Implement some basic tests of SystemTimeProvider

Greptile Summary

This PR implements unit tests for the SystemTimeProvider class, adding comprehensive test coverage for a previously untested time provider service that implements the ITimeProvider interface. The SystemTimeProvider is a foundational service in the Garage application that provides current time functionality through its Now property and captures immutable request instantiation time via its RequestTime property.

The changes include three main components:

  • Test Infrastructure Enhancement: Added the Shouldly NuGet package (v4.3.0) to the test project, which provides fluent assertion syntax that makes test assertions more readable and provides better error messages compared to standard xUnit assertions
  • Comprehensive Test Coverage: Created SystemTimeProviderTests.cs with three critical test scenarios that validate the service's core contracts - ensuring Now returns current time within expected bounds, verifying RequestTime is properly captured at instantiation, and confirming RequestTime remains immutable after creation
  • Code Organization: Removed the default Visual Studio template file UnitTest1.cs as part of proper test project setup

The SystemTimeProvider service appears to be used throughout the Garage application for request tracking, logging, and time-based operations. These tests follow standard arrange-act-assert patterns and use modern C# testing practices, ensuring the service maintains its reliability for time-sensitive functionality in the web application.

Important Files Changed

Files Modified in this PR
Filename Score Overview
tests/Garage.Tests/Garage.Tests.csproj 5/5 Added Shouldly NuGet package dependency for enhanced fluent test assertions
tests/Garage.Tests/Services/SystemTimeProviderTests.cs 4/5 New comprehensive unit tests for SystemTimeProvider with time-based validations
tests/Garage.Tests/UnitTest1.cs 5/5 Removed default Visual Studio template file for clean code organization

Confidence score: 4/5

  • This PR is generally safe to merge with well-structured unit tests that follow established testing best practices
  • The score reflects one minor concern about the Thread.Sleep(100) usage in the immutability test, which could potentially introduce test flakiness in very fast execution environments or CI/CD systems
  • The SystemTimeProviderTests.cs file may benefit from using more deterministic time-based testing approaches instead of Thread.Sleep

Sequence Diagram

sequenceDiagram
    participant TestRunner
    participant SystemTimeProviderTests
    participant SystemTimeProvider
    participant DateTimeOffset
    participant Thread

    Note over TestRunner, Thread: Test 1: Now_ShouldReturnCurrentTime()
    TestRunner->>SystemTimeProviderTests: Execute Now_ShouldReturnCurrentTime()
    SystemTimeProviderTests->>DateTimeOffset: Get before = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProviderTests: before timestamp
    SystemTimeProviderTests->>SystemTimeProvider: new SystemTimeProvider()
    SystemTimeProvider->>DateTimeOffset: RequestTime = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProvider: request timestamp
    SystemTimeProvider-->>SystemTimeProviderTests: timeProvider instance
    SystemTimeProviderTests->>SystemTimeProvider: timeProvider.Now
    SystemTimeProvider->>DateTimeOffset: DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProvider: current timestamp
    SystemTimeProvider-->>SystemTimeProviderTests: now timestamp
    SystemTimeProviderTests->>DateTimeOffset: Get after = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProviderTests: after timestamp
    SystemTimeProviderTests->>SystemTimeProviderTests: Assert now >= before && now <= after

    Note over TestRunner, Thread: Test 2: RequestTime_ShouldBeSetAtInstantiation()
    TestRunner->>SystemTimeProviderTests: Execute RequestTime_ShouldBeSetAtInstantiation()
    SystemTimeProviderTests->>DateTimeOffset: Get before = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProviderTests: before timestamp
    SystemTimeProviderTests->>SystemTimeProvider: new SystemTimeProvider()
    SystemTimeProvider->>DateTimeOffset: RequestTime = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProvider: request timestamp
    SystemTimeProvider-->>SystemTimeProviderTests: timeProvider instance
    SystemTimeProviderTests->>DateTimeOffset: Get after = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProviderTests: after timestamp
    SystemTimeProviderTests->>SystemTimeProvider: timeProvider.RequestTime
    SystemTimeProvider-->>SystemTimeProviderTests: requestTime
    SystemTimeProviderTests->>SystemTimeProviderTests: Assert requestTime >= before && requestTime <= after

    Note over TestRunner, Thread: Test 3: RequestTime_ShouldNotChangeAfterInstantiation()
    TestRunner->>SystemTimeProviderTests: Execute RequestTime_ShouldNotChangeAfterInstantiation()
    SystemTimeProviderTests->>SystemTimeProvider: new SystemTimeProvider()
    SystemTimeProvider->>DateTimeOffset: RequestTime = DateTimeOffset.Now
    DateTimeOffset-->>SystemTimeProvider: initial timestamp
    SystemTimeProvider-->>SystemTimeProviderTests: timeProvider instance
    SystemTimeProviderTests->>SystemTimeProvider: timeProvider.RequestTime
    SystemTimeProvider-->>SystemTimeProviderTests: initialRequestTime
    SystemTimeProviderTests->>Thread: Thread.Sleep(100)
    Thread-->>SystemTimeProviderTests: sleep complete
    SystemTimeProviderTests->>SystemTimeProvider: timeProvider.RequestTime
    SystemTimeProvider-->>SystemTimeProviderTests: laterRequestTime
    SystemTimeProviderTests->>SystemTimeProviderTests: Assert initialRequestTime == laterRequestTime
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant