public async Task Holds_For_All_URIs_With_Same_Domain() { var window = TimeSpan.FromMilliseconds(1000); var nowProvider = new NowProvider(new DateTime(2020, 01, 01)); var limiter = new RollingWindowRateLimiter(window, 1, nowProvider); var firstURI = new Uri("http://domain.com/something"); var secondURI = new Uri("http://domain.com/something-else"); var stopwatch = new Stopwatch(); await limiter.HoldIfRequired(firstURI); stopwatch.Start(); await limiter.HoldIfRequired(secondURI); Assert.IsTrue(stopwatch.ElapsedMilliseconds >= 1000); }
public async Task Holds_Until_Oldest_Passed_Window_If_Max_Reached(int windowSeconds, int max, int millStep) { var startDate = new DateTime(2020, 01, 01); var window = TimeSpan.FromSeconds(windowSeconds); var nowProvider = new TestNowProvider(startDate); var limiter = new RollingWindowRateLimiter(window, max, nowProvider); for (var i = 0; i < max; i++) { nowProvider.Update(startDate.AddMilliseconds(i * millStep)); await limiter.HoldIfRequired(new Uri("http://domain.com/" + i)); } }
public async Task Does_Not_Hold_If_Domain_New() { var window = TimeSpan.MaxValue; var nowProvider = new NowProvider(new DateTime(2020, 01, 01)); var limiter = new RollingWindowRateLimiter(window, 1, nowProvider); var uri = new Uri("http://domain.com/something"); var waitTask = limiter.HoldIfRequired(uri); var stopwatch = new Stopwatch(); stopwatch.Start(); while (!waitTask.IsCompleted) { if (stopwatch.ElapsedMilliseconds > 1000) { Assert.Fail("Took too long"); } } await waitTask; }