Skip to content

Commit 0e6e271

Browse files
authored
Fix flaky min rate tests (#2703)
1 parent c01d3f6 commit 0e6e271

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

tests/Aspire.Dashboard.Tests/ChannelExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task GetBatchesAsync_MinReadInterval_WaitForNextRead()
9696
Assert.Equal(["d", "e", "f"], read2.Single());
9797

9898
var elapsed = stopwatch.Elapsed;
99-
Assert.True(elapsed >= minReadInterval, $"Elapsed time {elapsed} should be greater than min read interval {minReadInterval} on read.");
99+
CustomAssert.AssertExceedsMinInterval(elapsed, minReadInterval);
100100

101101
channel.Writer.Complete();
102102
await TaskHelpers.WaitIgnoreCancelAsync(readTask);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace Aspire.Dashboard.Tests;
7+
8+
public static class CustomAssert
9+
{
10+
public static void AssertExceedsMinInterval(TimeSpan duration, TimeSpan minInterval)
11+
{
12+
// Timers are not precise, so we allow for a small margin of error.
13+
Assert.True(duration >= minInterval.Subtract(TimeSpan.FromMilliseconds(20)), $"Elapsed time {duration} should be greater than min interval {minInterval}.");
14+
}
15+
}

tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,6 @@ public async Task Subscription_MultipleUpdates_MinExecuteIntervalApplied()
706706
Assert.Equal(2, read2);
707707

708708
var elapsed = stopwatch.Elapsed;
709-
Assert.True(elapsed >= minExecuteInterval, $"Elapsed time {elapsed} should be greater than min execute interval {minExecuteInterval} on read.");
709+
CustomAssert.AssertExceedsMinInterval(elapsed, minExecuteInterval);
710710
}
711711
}

0 commit comments

Comments
 (0)