From: Stephen Toub Date: Wed, 18 Nov 2020 23:06:12 +0000 (-0500) Subject: Add test for Timer.Elapsed DateTime (#44881) X-Git-Tag: submit/tizen/20210909.063632~4540 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=722fb86f9023341f2ec30edb830daf3491fcc981;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add test for Timer.Elapsed DateTime (#44881) --- diff --git a/src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs b/src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs index 4f9437e..0904d71 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs @@ -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(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)]