_unitUnderTest.WaitToProceed(); timer.Stop(); Assert.IsTrue(timer.Elapsed.TotalSeconds < 1); } [Test] public void WaitToProceed_OneSecWait_Waits() { _unitUnderTest = new RateLimiter(1, TimeSpan.FromSeconds(2)); Stopwatch timer = Stopwatch.StartNew();
[Test] public void WaitToProceed_ForCoverage() { _unitUnderTest = new RateLimiter(1, TimeSpan.FromSeconds(1)); Stopwatch timer = Stopwatch.StartNew(); _unitUnderTest.WaitToProceed(TimeSpan.FromSeconds(10)); _unitUnderTest.Dispose(); timer.Stop(); Assert.IsTrue(timer.Elapsed.TotalSeconds < 1); }
private IRateLimiter GetRateLimter(Uri uri, long minCrawlDelayInMillisecs) { IRateLimiter rateLimiter; _rateLimiterLookup.TryGetValue(uri.Authority, out rateLimiter); if (rateLimiter == null && minCrawlDelayInMillisecs > 0) { rateLimiter = new RateLimiter(1, TimeSpan.FromMilliseconds(minCrawlDelayInMillisecs)); if (_rateLimiterLookup.TryAdd(uri.Authority, rateLimiter)) _logger.DebugFormat("Added new domain [{0}] with minCrawlDelayInMillisecs of [{1}] milliseconds", uri.Authority, minCrawlDelayInMillisecs); else _logger.WarnFormat("Unable to add new domain [{0}] with minCrawlDelayInMillisecs of [{1}] milliseconds", uri.Authority, minCrawlDelayInMillisecs); } return rateLimiter; }
public void AddOrUpdateDomain(Uri uri, long minCrawlDelayInMillisecs) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } if (minCrawlDelayInMillisecs < 1) { throw new ArgumentException("minCrawlDelayInMillisecs"); } var delayToUse = Math.Max(minCrawlDelayInMillisecs, _defaultMinCrawlDelayInMillisecs); if (delayToUse > 0) { var rateLimiter = new RateLimiter(1, TimeSpan.FromMilliseconds(delayToUse)); _rateLimiterLookup.AddOrUpdate(uri.Authority, rateLimiter, (key, oldValue) => rateLimiter); _logger.LogDebug($"Added/updated domain [{uri.Authority}] with minCrawlDelayInMillisecs of [{delayToUse}] milliseconds"); } }
private IRateLimiter GetRateLimter(Uri uri, long minCrawlDelayInMillisecs) { IRateLimiter rateLimiter; _rateLimiterLookup.TryGetValue(uri.Authority, out rateLimiter); if (rateLimiter == null && minCrawlDelayInMillisecs > 0) { rateLimiter = new RateLimiter(1, TimeSpan.FromMilliseconds(minCrawlDelayInMillisecs)); if (_rateLimiterLookup.TryAdd(uri.Authority, rateLimiter)) { _logger.LogDebug($"Added new domain [{uri.Authority}] with minCrawlDelayInMillisecs of [{minCrawlDelayInMillisecs}] milliseconds"); } else { _logger.LogWarning($"Unable to add new domain [{uri.Authority}] with minCrawlDelayInMillisecs of [{minCrawlDelayInMillisecs}] milliseconds"); } } return(rateLimiter); }
[ExpectedException(typeof(ArgumentOutOfRangeException))] public void WaitToProceed_NegativeMilli() { _unitUnderTest = new RateLimiter(1, TimeSpan.FromSeconds(1)); _unitUnderTest.WaitToProceed(-10); } } }
_unitUnderTest.WaitToProceed(); timer.Stop(); Assert.IsTrue(timer.Elapsed.TotalSeconds > 2);