Exemple #1
0
        public void AsManyAsBurstTokensAreAvailableRightAway(int burst)
        {
            // arrange
            var clock   = new FakeSystemClock();
            var limiter = new Limiter(new Limit(10), burst, clock);

            // act
            var allowed = new List <bool>();

            foreach (var index in Enumerable.Range(1, burst))
            {
                allowed.Add(limiter.Allow());
            }
            var notAllowed = limiter.Allow();

            // assert
            allowed.ShouldAllBe(item => item == true);
            notAllowed.ShouldBeFalse();
        }
Exemple #2
0
        public void FirstTokenIsAvailable()
        {
            // arrange
            var clock   = new FakeSystemClock();
            var limiter = new Limiter(new Limit(10), 1, clock);

            // act
            var allowed = limiter.Allow();

            // assert
            allowed.ShouldBe(true);
        }
Exemple #3
0
        public void TokensBecomeAvailableAtLimitPerSecondRate()
        {
            // arrange
            var clock   = new FakeSystemClock();
            var limiter = new Limiter(new Limit(10), 50, clock);

            // act
            var initiallyAllowed = limiter.AllowN(clock.UtcNow, 50);
            var thenNotAllowed1  = limiter.Allow();

            clock.Advance(TimeSpan.FromMilliseconds(100));
            var oneTokenAvailable = limiter.Allow();
            var thenNotAllowed2   = limiter.Allow();

            clock.Advance(TimeSpan.FromMilliseconds(200));
            var twoTokensAvailable1 = limiter.Allow();
            var twoTokensAvailable2 = limiter.Allow();
            var thenNotAllowed3     = limiter.Allow();

            // assert
            initiallyAllowed.ShouldBeTrue();
            thenNotAllowed1.ShouldBeFalse();
            oneTokenAvailable.ShouldBeTrue();
            thenNotAllowed2.ShouldBeFalse();
            twoTokensAvailable1.ShouldBeTrue();
            twoTokensAvailable2.ShouldBeTrue();
            thenNotAllowed3.ShouldBeFalse();
        }
Exemple #4
0
        public void ReserveTellsYouHowLongToWait()
        {
            // arrange
            var clock   = new FakeSystemClock();
            var limiter = new Limiter(new Limit(10), 50, clock);

            // act
            var initiallyAllowed = limiter.AllowN(clock.UtcNow, 50);
            var thenNotAllowed1  = limiter.Allow();

            var reserveOne = limiter.Reserve();
            var delayOne   = reserveOne.Delay();

            var reserveTwoMore = limiter.Reserve(clock.UtcNow, 2);
            var delayTwoMore   = reserveTwoMore.Delay();

            clock.Advance(TimeSpan.FromMilliseconds(450));

            var reserveAlreadyAvailable = limiter.Reserve();
            var delayAlreadyAvailable   = reserveAlreadyAvailable.Delay();

            var reserveHalfAvailable = limiter.Reserve();
            var delayHalfAvailable   = reserveHalfAvailable.Delay();

            // assert
            initiallyAllowed.ShouldBeTrue();
            thenNotAllowed1.ShouldBeFalse();
            reserveOne.Ok.ShouldBeTrue();
            delayOne.ShouldBe(TimeSpan.FromMilliseconds(100));
            reserveTwoMore.Ok.ShouldBeTrue();
            delayTwoMore.ShouldBe(TimeSpan.FromMilliseconds(300));
            reserveAlreadyAvailable.Ok.ShouldBeTrue();
            delayAlreadyAvailable.ShouldBe(TimeSpan.Zero);
            reserveHalfAvailable.Ok.ShouldBeTrue();
            delayHalfAvailable.ShouldBe(TimeSpan.FromMilliseconds(50));
        }