Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -13,7 +13,7 @@ namespace System.Threading.RateLimiting.Tests
{
internal static class Utils
{
internal static Func<Task> StopTimerAndGetTimerFunc<T>(PartitionedRateLimiter<T> limiter)
internal static async Task<Func<Task>> StopTimerAndGetTimerFunc<T>(PartitionedRateLimiter<T> limiter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type signature! 😆 It looks like this should fix the bug, but can we just add a private/internal ctor to DefaultPartitionedRateLimiter that doesn't start the timer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried out the private ctor approach. Definitely removes the possibility of test races now.

{
var innerTimer = limiter.GetType().GetField("_timer", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance);
Assert.NotNull(innerTimer);
Expand All @@ -22,6 +22,11 @@ internal static Func<Task> StopTimerAndGetTimerFunc<T>(PartitionedRateLimiter<T>
// Stop the current Timer so it doesn't fire unexpectedly
timerStopMethod.Invoke(innerTimer.GetValue(limiter), Array.Empty<object>());

// Await the timer task to make sure the Heartbeat continuation isn't going to run in the background during test code
var timerTask = limiter.GetType().GetField("_timerTask", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance);
Assert.NotNull(timerTask);
await (Task)timerTask.GetValue(limiter);

// Create a new Timer object so that disposing the PartitionedRateLimiter doesn't fail with an ODE, but this new Timer wont actually do anything
var timerCtor = innerTimer.FieldType.GetConstructor(new Type[] { typeof(TimeSpan), typeof(TimeSpan) });
Assert.NotNull(timerCtor);
Expand All @@ -31,6 +36,7 @@ internal static Func<Task> StopTimerAndGetTimerFunc<T>(PartitionedRateLimiter<T>

var timerLoopMethod = limiter.GetType().GetMethod("Heartbeat", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance);
Assert.NotNull(timerLoopMethod);

return () => (Task)timerLoopMethod.Invoke(limiter, Array.Empty<object>());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public async Task IdleLimiterIsCleanedUp()
});
});

var timerLoopMethod = Utils.StopTimerAndGetTimerFunc(limiter);
var timerLoopMethod = await Utils.StopTimerAndGetTimerFunc(limiter);

var lease = limiter.Acquire("");
Assert.True(lease.IsAcquired);
Expand Down Expand Up @@ -514,7 +514,7 @@ public async Task AllIdleLimitersCleanedUp_DisposeThrows()
}
});

var timerLoopMethod = Utils.StopTimerAndGetTimerFunc(limiter);
var timerLoopMethod = await Utils.StopTimerAndGetTimerFunc(limiter);

var lease = limiter.Acquire("1");
Assert.True(lease.IsAcquired);
Expand Down Expand Up @@ -569,7 +569,7 @@ public async Task ThrowingTryReplenishDoesNotPreventIdleLimiterBeingCleanedUp()
});
});

var timerLoopMethod = Utils.StopTimerAndGetTimerFunc(limiter);
var timerLoopMethod = await Utils.StopTimerAndGetTimerFunc(limiter);

// Add the replenishing limiter to the internal storage
limiter.Acquire("2");
Expand Down