From 3225c0acbedcac1632686f46f4af591f9858581c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 7 Mar 2024 16:25:16 +0800 Subject: [PATCH] Fix flaky min rate tests --- .../ChannelExtensionsTests.cs | 2 +- tests/Aspire.Dashboard.Tests/CustomAssert.cs | 15 +++++++++++++++ .../TelemetryRepositoryTests/LogTests.cs | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/Aspire.Dashboard.Tests/CustomAssert.cs diff --git a/tests/Aspire.Dashboard.Tests/ChannelExtensionsTests.cs b/tests/Aspire.Dashboard.Tests/ChannelExtensionsTests.cs index 0142e878bd0..8f73c37fc4d 100644 --- a/tests/Aspire.Dashboard.Tests/ChannelExtensionsTests.cs +++ b/tests/Aspire.Dashboard.Tests/ChannelExtensionsTests.cs @@ -96,7 +96,7 @@ public async Task GetBatchesAsync_MinReadInterval_WaitForNextRead() Assert.Equal(["d", "e", "f"], read2.Single()); var elapsed = stopwatch.Elapsed; - Assert.True(elapsed >= minReadInterval, $"Elapsed time {elapsed} should be greater than min read interval {minReadInterval} on read."); + CustomAssert.AssertExceedsMinInterval(elapsed, minReadInterval); channel.Writer.Complete(); await TaskHelpers.WaitIgnoreCancelAsync(readTask); diff --git a/tests/Aspire.Dashboard.Tests/CustomAssert.cs b/tests/Aspire.Dashboard.Tests/CustomAssert.cs new file mode 100644 index 00000000000..5fe7086089e --- /dev/null +++ b/tests/Aspire.Dashboard.Tests/CustomAssert.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace Aspire.Dashboard.Tests; + +public static class CustomAssert +{ + public static void AssertExceedsMinInterval(TimeSpan duration, TimeSpan minInterval) + { + // Timers are not precise, so we allow for a small margin of error. + Assert.True(duration >= minInterval.Subtract(TimeSpan.FromMilliseconds(20)), $"Elapsed time {duration} should be greater than min interval {minInterval}."); + } +} diff --git a/tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs b/tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs index 9d6e093cf02..e72f20cad9a 100644 --- a/tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs +++ b/tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs @@ -706,6 +706,6 @@ public async Task Subscription_MultipleUpdates_MinExecuteIntervalApplied() Assert.Equal(2, read2); var elapsed = stopwatch.Elapsed; - Assert.True(elapsed >= minExecuteInterval, $"Elapsed time {elapsed} should be greater than min execute interval {minExecuteInterval} on read."); + CustomAssert.AssertExceedsMinInterval(elapsed, minExecuteInterval); } }