Skip to content

Commit 2cb3e59

Browse files
add more UTs
1 parent 3b9c267 commit 2cb3e59

File tree

3 files changed

+925
-18
lines changed

3 files changed

+925
-18
lines changed

src/Microsoft.FeatureManagement/FeatureFilters/Recurrence/RecurrenceEvaluator.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ static class RecurrenceEvaluator
4949
const string Numbered = "Numbered";
5050
const string NoEnd = "NoEnd";
5151

52-
const int WeeklyUnitIntervalDuration = 7; // in days
53-
const int MonthlyUnitIntervalDuration = 28; // in days
54-
const int YearlyUnitIntervalDuration = 365; // in days
52+
const int WeekDayNumber = 7;
53+
const int MinMonthDayNumber = 28;
54+
const int MinYearDayNumber = 365;
5555

5656
public static bool MatchRecurrence(DateTimeOffset time, TimeWindowFilterSettings settings)
5757
{
@@ -619,13 +619,13 @@ private static bool TryValidateWeeklyRecurrencePattern(TimeWindowFilterSettings
619619

620620
RecurrencePattern pattern = settings.Recurrence.Pattern;
621621

622-
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * WeeklyUnitIntervalDuration);
622+
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * WeekDayNumber);
623623

624624
TimeSpan timeWindowDuration = settings.End.Value - settings.Start.Value;
625625

626626
//
627627
// Time window duration must be shorter than how frequently it occurs
628-
if (settings.End.Value - settings.Start.Value > intervalDuration)
628+
if (timeWindowDuration > intervalDuration)
629629
{
630630
paramName = $"{nameof(settings.End)}";
631631

@@ -684,7 +684,7 @@ private static bool TryValidateAbsoluteMonthlyRecurrencePattern(TimeWindowFilter
684684

685685
RecurrencePattern pattern = settings.Recurrence.Pattern;
686686

687-
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MonthlyUnitIntervalDuration);
687+
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MinMonthDayNumber);
688688

689689
//
690690
// Time window duration must be shorter than how frequently it occurs
@@ -730,7 +730,7 @@ private static bool TryValidateRelativeMonthlyRecurrencePattern(TimeWindowFilter
730730

731731
RecurrencePattern pattern = settings.Recurrence.Pattern;
732732

733-
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MonthlyUnitIntervalDuration);
733+
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MinMonthDayNumber);
734734

735735
//
736736
// Time window duration must be shorter than how frequently it occurs
@@ -782,7 +782,7 @@ private static bool TryValidateAbsoluteYearlyRecurrencePattern(TimeWindowFilterS
782782

783783
RecurrencePattern pattern = settings.Recurrence.Pattern;
784784

785-
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * YearlyUnitIntervalDuration);
785+
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MinYearDayNumber);
786786

787787
//
788788
// Time window duration must be shorter than how frequently it occurs
@@ -833,7 +833,7 @@ private static bool TryValidateRelativeYearlyRecurrencePattern(TimeWindowFilterS
833833

834834
RecurrencePattern pattern = settings.Recurrence.Pattern;
835835

836-
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * YearlyUnitIntervalDuration);
836+
TimeSpan intervalDuration = TimeSpan.FromDays(pattern.Interval * MinYearDayNumber);
837837

838838
//
839839
// Time window duration must be shorter than how frequently it occurs

tests/Tests.FeatureManagement/FeatureManagement.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ public async Task TimeWindow()
256256
string feature2 = "feature2";
257257
string feature3 = "feature3";
258258
string feature4 = "feature4";
259+
string feature5 = "feature5";
260+
string feature6 = "feature6";
259261

260262
Environment.SetEnvironmentVariable($"FeatureManagement:{feature1}:EnabledFor:0:Name", "TimeWindow");
261263
Environment.SetEnvironmentVariable($"FeatureManagement:{feature1}:EnabledFor:0:Parameters:End", DateTimeOffset.UtcNow.AddDays(1).ToString("r"));
@@ -269,6 +271,19 @@ public async Task TimeWindow()
269271
Environment.SetEnvironmentVariable($"FeatureManagement:{feature4}:EnabledFor:0:Name", "TimeWindow");
270272
Environment.SetEnvironmentVariable($"FeatureManagement:{feature4}:EnabledFor:0:Parameters:Start", DateTimeOffset.UtcNow.AddDays(1).ToString("r"));
271273

274+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature5}:EnabledFor:0:Name", "TimeWindow");
275+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature5}:EnabledFor:0:Parameters:Start", DateTimeOffset.UtcNow.AddDays(-2).ToString("r"));
276+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature5}:EnabledFor:0:Parameters:End", DateTimeOffset.UtcNow.AddDays(-1).ToString("r"));
277+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature5}:EnabledFor:0:Parameters:Recurrence:Pattern:Type", "Daily");
278+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature5}:EnabledFor:0:Parameters:Recurrence:Range:Type", "NoEnd");
279+
280+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Name", "TimeWindow");
281+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Parameters:Start", DateTimeOffset.UtcNow.AddDays(-2).ToString("r"));
282+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Parameters:End", DateTimeOffset.UtcNow.AddDays(-1).ToString("r"));
283+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Parameters:Recurrence:Pattern:Type", "Daily");
284+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Parameters:Recurrence:Pattern:Interval", "3");
285+
Environment.SetEnvironmentVariable($"FeatureManagement:{feature6}:EnabledFor:0:Parameters:Recurrence:Range:Type", "NoEnd");
286+
272287
IConfiguration config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
273288

274289
var serviceCollection = new ServiceCollection();
@@ -285,6 +300,8 @@ public async Task TimeWindow()
285300
Assert.False(await featureManager.IsEnabledAsync(feature2));
286301
Assert.True(await featureManager.IsEnabledAsync(feature3));
287302
Assert.False(await featureManager.IsEnabledAsync(feature4));
303+
Assert.True(await featureManager.IsEnabledAsync(feature5));
304+
Assert.False(await featureManager.IsEnabledAsync(feature6));
288305
}
289306

290307
[Fact]

0 commit comments

Comments
 (0)