Skip to content

Commit

Permalink
Attempted to replicate timezone issue
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigHawker committed Apr 16, 2024
1 parent c927de4 commit c5e137c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 42 deletions.
23 changes: 9 additions & 14 deletions MFiles.VAF.Extensions.Tests/Configuration/FrequencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using MFiles.VAF.Extensions.ScheduledExecution;
using MFiles.VAF.Extensions.Tests.ScheduledExecution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -43,17 +44,13 @@ public static IEnumerable<object[]> SplitTriggerType_Data()
{
yield return new object[]
{
new DateTime(2022, 10, 06, 20, 01, 00, DateTimeKind.Utc),
new DateTime(2022, 10, 06, 20, 30, 00, DateTimeKind.Utc)
new DateTimeOffset(2022, 10, 06, 20, 01, 00, TimeSpan.Zero),
new DateTimeOffset(2022, 10, 06, 20, 30, 00, TimeSpan.Zero)
};
yield return new object[]
{
TimeZoneInfo.ConvertTimeBySystemTimeZoneId
(
new DateTime(2022, 10, 26, 20, 31, 00, DateTimeKind.Local),
"GMT Standard Time"
),
new DateTime(2022, 10, 26, 20, 00, 00, DateTimeKind.Utc)
new DateTimeOffset(2022, 10, 06, 20, 01, 00, TimeSpan.FromHours(1)),
new DateTimeOffset(2022, 10, 06, 20, 30, 00, TimeSpan.FromHours(1))
};
}
public static IEnumerable<object[]> DaylightSaving_ClocksGoBackwards_Data()
Expand Down Expand Up @@ -111,7 +108,7 @@ DateTimeOffset expected

[DynamicData(nameof(SplitTriggerType_Data), DynamicDataSourceType.Method)]
[TestMethod]
public void SplitTriggerType(DateTime now, DateTime expected)
public void SplitTriggerType(DateTimeOffset now, DateTimeOffset expected)
{
var frequency = Newtonsoft.Json.JsonConvert.DeserializeObject<Frequency>(@"{
""Triggers"": [
Expand Down Expand Up @@ -210,11 +207,9 @@ public void SplitTriggerType(DateTime now, DateTime expected)
""TriggerTimeType"": ""UTC""
}
");
{
var nextRun = frequency.GetNextExecution(now);
Assert.IsNotNull(nextRun.Value);
Assert.AreEqual(expected, nextRun.Value.ToUniversalTime());
}
var nextRun = frequency.GetNextExecution(now);
Assert.IsNotNull(nextRun.Value);
Assert.AreEqual(expected, nextRun.Value.ToUniversalTime());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,53 @@ public void ScheduleTriggersAreNotNullByDefault()
Assert.IsNotNull(new Schedule().Triggers);
}

[TestMethod]
// https://community.m-files.com/groups-1602070992/developers/f/developer-forum/9824/trouble-with-daily-scheduling-repeating
public void ForumPost9824()
{
using (var x = new LocalTimeZoneInfoMocker("FLE Standard Time"))
{
var schedule = new Schedule()
{
Enabled = true,
Triggers = new List<Trigger>()
{
new DailyTrigger()
{
TriggerTimes = new List<TimeSpan>()
{
TimeSpan.FromHours(1)
}
}
},
RunOnVaultStartup = false
};

Assert.AreEqual
(
// Lets say we run just after the time above.
new DateTime(2024, 01, 02, 01, 00, 00, DateTimeKind.Local),
// We expect to run the next day at 1am local time.
schedule.GetNextExecution(new DateTime(2024, 01, 01, 01, 00, 01, DateTimeKind.Local))
);

Assert.AreEqual
(
// Lets say we run just after the time above but without an explicit datetimekind.
new DateTime(2024, 01, 02, 01, 00, 00),
// We expect to run the next day at 1am local time.
schedule.GetNextExecution(new DateTime(2024, 01, 01, 01, 00, 01))
);
}
}

[TestMethod]
[DynamicData(nameof(GetNextExecutionData_UTC), DynamicDataSourceType.Method)]
public void GetNextExecution
public void GetNextExecution_UTC
(
IEnumerable<TriggerBase> triggers,
DateTime? after,
DateTime? expected
DateTimeOffset? after,
DateTimeOffset? expected
)
{
Assert.AreEqual
Expand All @@ -42,7 +82,8 @@ public void GetNextExecution
Triggers = triggers
.Select(t => new Trigger(t))
.Where(t => t != null)
.ToList()
.ToList(),
TriggerTimeType = TriggerTimeType.Utc,
}.GetNextExecution(after)
);
}
Expand Down Expand Up @@ -141,8 +182,8 @@ public void GetNextExecution_GMT
public void GetNextExecution_NotEnabled
(
IEnumerable<TriggerBase> triggers,
DateTime? after,
DateTime? expected
DateTimeOffset? after,
DateTimeOffset? expected
)
{
// We use the same data as the GetNextExecution, but
Expand Down Expand Up @@ -177,8 +218,8 @@ public static IEnumerable<object[]> GetNextExecutionData_UTC()
}.ToList()
}
},
new DateTime(2021, 03, 17, 01, 00, 00, DateTimeKind.Utc), // Wednesday @ 1am
new DateTime(2021, 03, 17, 17, 00, 00, DateTimeKind.Utc), // Wednesday @ 5pm
new DateTimeOffset(2021, 03, 17, 01, 00, 00, TimeSpan.Zero), // Wednesday @ 1am
new DateTimeOffset(2021, 03, 17, 17, 00, 00, TimeSpan.Zero), // Wednesday @ 5pm
};

// Multiple triggers returns earliest.
Expand All @@ -199,16 +240,16 @@ public static IEnumerable<object[]> GetNextExecutionData_UTC()
}.ToList()
}
},
new DateTime(2021, 03, 17, 01, 00, 00, DateTimeKind.Utc), // Wednesday @ 1am
new DateTime(2021, 03, 17, 12, 00, 00, DateTimeKind.Utc), // Wednesday @ 5pm
new DateTimeOffset(2021, 03, 17, 01, 00, 00, TimeSpan.Zero), // Wednesday @ 1am
new DateTimeOffset(2021, 03, 17, 12, 00, 00, TimeSpan.Zero), // Wednesday @ 5pm
};

// No triggers = null.
yield return new object[]
{
new TriggerBase[0],
new DateTime(2021, 03, 17, 01, 00, 00, DateTimeKind.Utc), // Wednesday @ 1am
(DateTime?)null
new DateTimeOffset(2021, 03, 17, 01, 00, 00, TimeSpan.Zero), // Wednesday @ 1am
(DateTimeOffset?)null
};

// Trigger at exact current time returns now.
Expand All @@ -223,8 +264,8 @@ public static IEnumerable<object[]> GetNextExecutionData_UTC()
}.ToList()
}
},
new DateTime(2021, 03, 17, 17, 00, 00, DateTimeKind.Utc), // Wednesday @ 1am
new DateTime(2021, 03, 17, 17, 00, 00, DateTimeKind.Utc)
new DateTimeOffset(2021, 03, 17, 17, 00, 00, TimeSpan.Zero), // Wednesday @ 1am
new DateTimeOffset(2021, 03, 17, 17, 00, 00, TimeSpan.Zero)
};
}

Expand Down Expand Up @@ -278,8 +319,8 @@ public static IEnumerable<object[]> GetNextExecutionData_GMT()
}.ToList()
}
},
new DateTimeOffset(2022, 10, 30, 02, 00, 00, 0, TimeSpan.Zero), // This is 0100 BST
new DateTimeOffset(2022, 10, 30, 02, 30, 00, 0, TimeSpan.Zero), // So it should run at 0030UTC / 0130 BST
new DateTimeOffset(2022, 10, 30, 00, 00, 00, 0, TimeSpan.FromHours(1)), // This is 0100 BST
new DateTimeOffset(2022, 10, 30, 02, 30, 00, 0, TimeSpan.Zero), // So it should run at 0230 GMT
};

// Just before clocks go backwards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void GetNextDayOfWeek
var result = WeeklyTrigger.GetNextDayOfWeek(after, dayOfWeek)?.ToArray();
Assert.IsNotNull(result);
Assert.AreEqual(expected.Length, result.Length);
for(var i=0; i<result.Length; i++)

for (var i = 0; i < result.Length; i++)
{
Assert.AreEqual(expected[i], result[i]);
}
Expand Down
27 changes: 17 additions & 10 deletions MFiles.VAF.Extensions.Tests/Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ namespace MFiles.VAF.Extensions.Tests
[TestClass]
public class Initialization
{
private class MSTestContextTarget
: global::NLog.Targets.TargetWithLayout
{
protected TestContext TestContext { get; }
public MSTestContextTarget(TestContext testContext)
{
this.TestContext = testContext;
}
protected override void Write(NLog.LogEventInfo logEvent)
{
this.TestContext.WriteLine(this.RenderLogEvent(Layout, logEvent));
}
}
[AssemblyInitialize]
public static void MyTestInitialize(TestContext testContext)
{
Expand All @@ -23,16 +36,10 @@ public static void MyTestInitialize(TestContext testContext)
(
global::NLog.LogLevel.Trace,
global::NLog.LogLevel.Fatal,
new SensitivityAwareAsyncTargetWrapper
(
new global::NLog.Targets.ColoredConsoleTarget()
{
AutoFlush = true,
Layout = layout
},
LogSensitivity.Minimum,
new SensitivityFlag[0]
)
new MSTestContextTarget(testContext)
{
Layout = layout
}
);
global::NLog.LogManager.ReconfigExistingLoggers();

Expand Down
26 changes: 26 additions & 0 deletions MFiles.VAF.Extensions.Tests/LocalTimeZoneInfoMocker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Reflection;

namespace MFiles.VAF.Extensions.Tests
{
public class LocalTimeZoneInfoMocker : IDisposable
{
public LocalTimeZoneInfoMocker(string timeZoneId)
: this(TimeZoneInfo.FindSystemTimeZoneById(timeZoneId))
{
}
public LocalTimeZoneInfoMocker(TimeZoneInfo mockTimeZoneInfo)
{
var info = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
var cachedData = info.GetValue(null);
var field = cachedData.GetType().GetField("m_localTimeZone",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance);
field.SetValue(cachedData, mockTimeZoneInfo);
}

public void Dispose()
{
TimeZoneInfo.ClearCachedData();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal static IEnumerable<DateTimeOffset> GetNextDayOfWeek(DateTimeOffset afte
}

// Get the next one of this day.
yield return after.Date.AddDays(daysToAdd);
yield return new DateTimeOffset(after.Date.AddDays(daysToAdd), after.Offset);
}

/// <inheritdoc />
Expand Down

0 comments on commit c5e137c

Please sign in to comment.