Add test for Timer.Elapsed DateTime (#44881)
authorStephen Toub <stoub@microsoft.com>
Wed, 18 Nov 2020 23:06:12 +0000 (18:06 -0500)
committerGitHub <noreply@github.com>
Wed, 18 Nov 2020 23:06:12 +0000 (18:06 -0500)
src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs

index 4f9437e..0904d71 100644 (file)
@@ -3,7 +3,7 @@
 
 using Xunit;
 using System.Threading;
-
+using System.Threading.Tasks;
 using TestTimer = System.Timers.Timer;
 
 namespace System.Timers.Tests
@@ -69,6 +69,31 @@ namespace System.Timers.Tests
             }
         }
 
+        [Fact]
+        public async Task ElapsedEventArgs_MatchesExpectedValues()
+        {
+            using (var timer = new TestTimer(1) { AutoReset = false })
+            {
+                DateTime start = DateTime.Now;
+                var tcs = new TaskCompletionSource<ElapsedEventArgs>(TaskCreationOptions.RunContinuationsAsynchronously);
+                timer.Elapsed += (sender, e) => tcs.SetResult(e);
+                timer.Start();
+
+                ElapsedEventArgs e = await tcs.Task;
+                Assert.False(timer.Enabled);
+
+                timer.Stop();
+                DateTime end = DateTime.Now;
+
+                const int WiggleRoomSeconds = 5;
+                Assert.Equal(DateTimeKind.Local, e.SignalTime.Kind);
+                Assert.InRange(
+                    e.SignalTime.ToUniversalTime(),
+                    start.ToUniversalTime() - TimeSpan.FromSeconds(WiggleRoomSeconds),
+                    end.ToUniversalTime() + TimeSpan.FromSeconds(WiggleRoomSeconds));
+            }
+        }
+
         [Theory]
         [InlineData(int.MaxValue)]
         [InlineData(0.5D)]