Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CancelOnTimerWithLargeRetryDelay test flakiness due to Stopwatch imprecision #384

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ServicePartitionClientTests

private static readonly TimeSpan DefaultRetryDelay = TimeSpan.FromMilliseconds(50);

// Due to bugs in BIOS or Hardware Abstraction Layer, Stopwatch sometimes reports slightly smaller value, as
// documented here: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=net-8.0
// We need to account for that possibility.
private static readonly long StopwatchPrecisionMs = 25;

/// <summary>
/// Tests handling of cancellation by the passed cancellation token.
/// </summary>
Expand Down Expand Up @@ -67,8 +72,9 @@ public async Task CancelOnTimer()
clientRetryTimeout,
retryCount,
retryDelay);
sw.Stop();

sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds, "Should be longer than the ClientRetryTimeout.");
sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds - StopwatchPrecisionMs, "Should be longer than the ClientRetryTimeout.");
result.ExceptionFromInvoke.Should().BeAssignableTo(typeof(OperationCanceledException), $"Should indicate a canceled operation. {result.ExceptionFromInvoke}");
result.CallCount.Should().BeLessThan(retryCount, "Should cancel before token is signaled.");
result.CancellationTokenSource.Token.IsCancellationRequested.Should().Be(false, "Cancellation should have occured due to the timer.");
Expand All @@ -92,8 +98,9 @@ public async Task CancelAfterCall()
retryCount,
retryDelay);
result.CancellationTokenSource.Cancel();
sw.Stop();

sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds - 10, "Should be longer than the ClientRetryTimeout.");
sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds - StopwatchPrecisionMs, "Should be longer than the ClientRetryTimeout.");
result.ExceptionFromInvoke.Should().BeAssignableTo(typeof(OperationCanceledException), $"Should indicate a canceled operation. {result.ExceptionFromInvoke}");
result.CallCount.Should().BeLessThan(retryCount, "Should cancel before token is signaled.");
}
Expand All @@ -115,8 +122,9 @@ public async Task CancelOnTimerWithLargeRetryDelay()
clientRetryTimeout,
retryCount,
retryDelay);
sw.Stop();

sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds, "Should be longer than the ClientRetryTimeout.");
sw.ElapsedMilliseconds.Should().BeGreaterThan((long)clientRetryTimeout.TotalMilliseconds - StopwatchPrecisionMs, "Should be longer than the ClientRetryTimeout.");
sw.ElapsedMilliseconds.Should().BeLessThan((long)retryDelay.TotalMilliseconds, "Should return before the retry delay.");
result.ExceptionFromInvoke.Should().BeAssignableTo(typeof(OperationCanceledException), "Should indicate a canceled operation.");
result.CallCount.Should().BeLessThan(retryCount, "Should cancel before token is signaled.");
Expand Down