From 7045d1a9aedcb168cbbdd7448739fd37c0b85998 Mon Sep 17 00:00:00 2001 From: David Kean Date: Thu, 17 Mar 2016 16:15:53 -0700 Subject: [PATCH] Fix flaky ForegroundNotificationServiceTests.Test_Delay This test has been changed twice, first time was using DateTime, second was using Stopwatch. From my testing, both have a slightly different opinion than to Environment.TickCount as to how long has passed by 1 - 3 milliseconds. Environment.TickCount on my box has a resolution of 15/16 milliseconds accuracy, but Stopwatch and DateTime both found that these same 15/16 milliseconds took between 14 -> 16 milliseconds. Instead of accounting for this, I just changed this test to use the same clock as the underlying service. --- .../ForegroundNotificationServiceTests.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/EditorFeatures/Test/Threading/ForegroundNotificationServiceTests.cs b/src/EditorFeatures/Test/Threading/ForegroundNotificationServiceTests.cs index 5bdb44f1202..9a21963c75d 100644 --- a/src/EditorFeatures/Test/Threading/ForegroundNotificationServiceTests.cs +++ b/src/EditorFeatures/Test/Threading/ForegroundNotificationServiceTests.cs @@ -4,8 +4,6 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; -using System.Windows; -using System.Windows.Threading; using Microsoft.CodeAnalysis.Editor.Implementation.ForegroundNotification; using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; using Microsoft.CodeAnalysis.Shared.TestHooks; @@ -68,20 +66,25 @@ public async Task Test_Cancellation() [WpfFact] public async Task Test_Delay() { + // NOTE: Don't be tempted to use DateTime or Stopwatch to measure this + // Switched to Environment.TickCount use the same clock as the notification + // service, see: https://github.com/dotnet/roslyn/issues/7512. + var asyncToken = EmptyAsyncToken.Instance; - Stopwatch watch = Stopwatch.StartNew(); + int startMilliseconds = Environment.TickCount; + int? elapsedMilliseconds = null; _service.RegisterNotification(() => { - watch.Stop(); + elapsedMilliseconds = Environment.TickCount - startMilliseconds; + _done = true; }, 50, asyncToken, CancellationToken.None); await PumpWait(); - Assert.False(watch.IsRunning); - Assert.True(watch.ElapsedMilliseconds >= 50); + Assert.True(elapsedMilliseconds >= 50, $"Notification fired after {elapsedMilliseconds}, instead of 50."); Assert.True(_service.IsEmpty_TestOnly); } -- GitLab