From 3b838a4dae264f857bb9ebbe1220b179603ceb8d Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang <141655842+zhiyuanliang-ms@users.noreply.github.com> Date: Fri, 16 Aug 2024 01:21:27 +0800 Subject: [PATCH] use TimeProvider (#452) --- .../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 1fc9b667..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; } - } -} diff --git a/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs b/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs index fb1f6f01..62b5f4a5 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; set; } + internal TimeProvider SystemClock { get; set; } /// /// 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 62e3ed9d..3abf1568 100644 --- a/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj +++ b/src/Microsoft.FeatureManagement/Microsoft.FeatureManagement.csproj @@ -35,10 +35,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; + } } }