From 11604b0c79bb953c8f51b5c804c4e191136eb566 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Sun, 19 May 2024 12:53:39 +0800 Subject: [PATCH] use TimeProvider --- .../FeatureFilters/ISystemClock.cs | 20 ------------------- .../FeatureFilters/TimeWindowFilter.cs | 6 +++--- .../Microsoft.FeatureManagement.csproj | 3 ++- .../Tests.FeatureManagement/OnDemandClock.cs | 10 +++++++--- 4 files changed, 12 insertions(+), 27 deletions(-) delete mode 100644 src/Microsoft.FeatureManagement/FeatureFilters/ISystemClock.cs diff --git a/src/Microsoft.FeatureManagement/FeatureFilters/ISystemClock.cs b/src/Microsoft.FeatureManagement/FeatureFilters/ISystemClock.cs deleted file mode 100644 index 50c6743d..00000000 --- a/src/Microsoft.FeatureManagement/FeatureFilters/ISystemClock.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. -// - -using System; - -namespace Microsoft.FeatureManagement.FeatureFilters -{ - /// - /// Abstracts the system clock to facilitate testing. - /// .NET8 offers an abstract class TimeProvider. After we stop supporting .NET version less than .NET8, this ISystemClock should retire. - /// - internal interface ISystemClock - { - /// - /// Retrieves the current system time in UTC. - /// - public DateTimeOffset UtcNow { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs b/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs index 121bb4cf..9ae47e1b 100644 --- a/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs +++ b/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs @@ -39,7 +39,7 @@ public TimeWindowFilter(ILoggerFactory loggerFactory = null) /// /// This property allows the time window filter in our test suite to use simulated time. /// - internal ISystemClock SystemClock { get; init; } + internal TimeProvider SystemClock { get; init; } /// /// Binds configuration representing filter parameters to . @@ -74,7 +74,7 @@ public Task EvaluateAsync(FeatureFilterEvaluationContext context) // Check if prebound settings available, otherwise bind from parameters. TimeWindowFilterSettings settings = (TimeWindowFilterSettings)context.Settings ?? (TimeWindowFilterSettings)BindParameters(context.Parameters); - DateTimeOffset now = SystemClock?.UtcNow ?? DateTimeOffset.UtcNow; + DateTimeOffset now = SystemClock?.GetUtcNow() ?? DateTimeOffset.UtcNow; if (!settings.Start.HasValue && !settings.End.HasValue) { @@ -129,7 +129,7 @@ public Task EvaluateAsync(FeatureFilterEvaluationContext context) private DateTimeOffset? ReloadClosestStart(TimeWindowFilterSettings settings) { - DateTimeOffset now = SystemClock?.UtcNow ?? DateTimeOffset.UtcNow; + DateTimeOffset now = SystemClock?.GetUtcNow() ?? DateTimeOffset.UtcNow; DateTimeOffset? closestStart = RecurrenceEvaluator.CalculateClosestStart(now, settings); diff --git a/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj b/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj index dd67ba56..266134c7 100644 --- a/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj +++ b/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj @@ -33,10 +33,11 @@ - + + diff --git a/tests/Tests.FeatureManagement/OnDemandClock.cs b/tests/Tests.FeatureManagement/OnDemandClock.cs index c639a3e3..59c8c0d8 100644 --- a/tests/Tests.FeatureManagement/OnDemandClock.cs +++ b/tests/Tests.FeatureManagement/OnDemandClock.cs @@ -1,10 +1,14 @@ -using Microsoft.FeatureManagement.FeatureFilters; -using System; +using System; namespace Tests.FeatureManagement { - class OnDemandClock : ISystemClock + class OnDemandClock : TimeProvider { public DateTimeOffset UtcNow { get; set; } + + public override DateTimeOffset GetUtcNow() + { + return UtcNow; + } } }