public void RateLimit_ZeroAsDefault_SameDomain_DoesNotWaitsBetweenRequests()
        {
            Uri uri = new Uri("http://a.com/");
            Stopwatch timer = Stopwatch.StartNew();
            DomainRateLimiter unitUnderTest = new DomainRateLimiter(0);
            unitUnderTest.RateLimit(uri);
            unitUnderTest.RateLimit(uri);
            unitUnderTest.RateLimit(uri);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds < 50);
        }
        public void RateLimit_ZeroAsDefault_DifferentDomain_DoesNotWaitsBetweenRequests()
        {
            Uri uri1 = new Uri("http://a.com/");
            Uri uri2 = new Uri("http://b.com/");
            Uri uri3 = new Uri("http://c.com/");
            Uri uri4 = new Uri("http://d.com/");

            Stopwatch timer = Stopwatch.StartNew();
            DomainRateLimiter unitUnderTest = new DomainRateLimiter(0);
            unitUnderTest.RateLimit(uri1);
            unitUnderTest.RateLimit(uri2);
            unitUnderTest.RateLimit(uri3);
            unitUnderTest.RateLimit(uri4);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds < 50);
        }
        public void AddDomain_ParamLessThanDefault_UsesDefault()
        {
            Uri rootUri = new Uri("http://a.com/");
            Uri pageUri1 = new Uri("http://a.com/a.html");
            Uri pageUri2 = new Uri("http://a.com/b.html");

            Stopwatch timer = Stopwatch.StartNew();
            DomainRateLimiter unitUnderTest = new DomainRateLimiter(100);

            unitUnderTest.AddDomain(rootUri, 5);

            unitUnderTest.RateLimit(rootUri);
            unitUnderTest.RateLimit(pageUri1);
            unitUnderTest.RateLimit(pageUri2);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds > 190);
        }
        public void RemoveDomain_NoLongerRateLimitsThatDomain()
        {
            //Arrange
            var domainRateLimiter = new DomainRateLimiter(5);
            var domain = new Uri("http://a.com");

            domainRateLimiter.AddDomain(domain, 1000);

            //Act
            domainRateLimiter.RemoveDomain(domain);

            //Assert
            var timer = System.Diagnostics.Stopwatch.StartNew();
            domainRateLimiter.RateLimit(domain);
            domainRateLimiter.RateLimit(domain);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds < 25, "Expected it to take less than 25 millisecs but only took " + timer.ElapsedMilliseconds);
        }
        public void AddOrUpdateDomain_AddDuplicateDomain_LastUpdateWins()
        {
            var domainRateLimiter = new DomainRateLimiter(5);
            var domain = new Uri("http://a.com");
            
            domainRateLimiter.AddOrUpdateDomain(domain, 50);
            domainRateLimiter.AddOrUpdateDomain(domain, 150);//This should override the previous

            var timer = System.Diagnostics.Stopwatch.StartNew();
            domainRateLimiter.RateLimit(domain);
            domainRateLimiter.RateLimit(domain);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds >= 150, "Expected it to take more than 150 millisecs but only took " + timer.ElapsedMilliseconds);
        }
        public void AddDomain_AddDuplicateDomain_FirstAddWins()
        {
            var domainRateLimiter = new DomainRateLimiter(5);
            var domain = new Uri("http://a.com");

            domainRateLimiter.AddDomain(domain, 50);
            domainRateLimiter.AddDomain(domain, 150);//This should be ignored

            var timer = System.Diagnostics.Stopwatch.StartNew();
            domainRateLimiter.RateLimit(domain);
            domainRateLimiter.RateLimit(domain);
            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds >= 50 && timer.ElapsedMilliseconds < 150, string.Format("Expected it to take more than 50 but less than 150 but only took {0}", timer.ElapsedMilliseconds));
        }