Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        public void AddDomain_ParamGreaterThanDefault_UsesParam()
        {
            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(5);

            unitUnderTest.AddDomain(rootUri, 100);

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

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

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

            unitUnderTest.AddDomain(rootUri, 5);

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

            Assert.IsTrue(timer.ElapsedMilliseconds > 190);
        }
Ejemplo n.º 4
0
        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 < 100, "Expected it to take less than 100 millisecs but only took " + timer.ElapsedMilliseconds);
        }