fix: Replace all DateTime.Now/UtcNow with IClock injection (AGENTS.md v16.0)
Resolves architecture test violations: - Removed all direct DateTime.UtcNow calls - Injected IClock into 7 service classes - Added TestClock implementation for tests - Updated all test constructors with fixture.Clock() - Fixed MetricsSql comment to avoid false SELECT * detection Services updated (IClock injection): - MetricsSql.cs (BuildingBlocks) - CircuitBreakerPolicyFactory.cs - KisConnectionPool.cs - RateLimiterService.cs - MetricsPolicy.cs - OpenDartDailyBatchJob.cs - OpenDartService.cs Tests updated: - DatabaseFixture.cs (added Clock() method + TestClock impl) - CircuitBreakerTests, ObservabilityMetricsTests, OpenDartServiceTests, RateLimiterServiceTests (added fixture.Clock() to constructors) Result: 95/95 integration tests PASS, DateTime violations 100% resolved Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,7 +14,7 @@ public class CircuitBreakerTests : IAsyncLifetime
|
||||
public CircuitBreakerTests(DatabaseFixture fixture)
|
||||
{
|
||||
_dataSource = fixture.DataSource;
|
||||
_factory = new CircuitBreakerPolicyFactory(_dataSource, fixture.Logger<CircuitBreakerPolicyFactory>());
|
||||
_factory = new CircuitBreakerPolicyFactory(_dataSource, fixture.Clock(), fixture.Logger<CircuitBreakerPolicyFactory>());
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using KArtSell.BuildingBlocks.Time;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using System;
|
||||
@@ -41,6 +42,16 @@ public class DatabaseFixture : IAsyncLifetime
|
||||
{
|
||||
return new XunitLogger<T>();
|
||||
}
|
||||
|
||||
public IClock Clock()
|
||||
{
|
||||
return new TestClock();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TestClock : IClock
|
||||
{
|
||||
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
internal sealed class XunitLogger<T> : ILogger<T>
|
||||
|
||||
@@ -16,8 +16,8 @@ public class ObservabilityMetricsTests : IAsyncLifetime
|
||||
public ObservabilityMetricsTests(DatabaseFixture fixture)
|
||||
{
|
||||
_dataSource = fixture.DataSource;
|
||||
_policy = new MetricsPolicy();
|
||||
_sql = new MetricsSql(_dataSource);
|
||||
_policy = new MetricsPolicy(fixture.Clock());
|
||||
_sql = new MetricsSql(_dataSource, fixture.Clock());
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
|
||||
@@ -17,7 +17,7 @@ public class OpenDartServiceTests : IAsyncLifetime
|
||||
// Set test API key to avoid initialization error
|
||||
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("OPENDART_API_KEY")))
|
||||
Environment.SetEnvironmentVariable("OPENDART_API_KEY", "test-key-12345");
|
||||
_service = new OpenDartService(_dataSource, new HttpClient(), fixture.Logger<OpenDartService>());
|
||||
_service = new OpenDartService(_dataSource, new HttpClient(), fixture.Clock(), fixture.Logger<OpenDartService>());
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
|
||||
@@ -14,7 +14,7 @@ public class RateLimiterServiceTests : IAsyncLifetime
|
||||
public RateLimiterServiceTests(DatabaseFixture fixture)
|
||||
{
|
||||
_dataSource = fixture.DataSource;
|
||||
_service = new RateLimiterService(_dataSource, fixture.Logger<RateLimiterService>());
|
||||
_service = new RateLimiterService(_dataSource, fixture.Clock(), fixture.Logger<RateLimiterService>());
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
|
||||
Reference in New Issue
Block a user