Exemple #1
0
        public void WillExceedRateLimit_WhenExpectedCallsDoesNotExceedReamingAndResetTimeHasNotPassed_ExpectFalse()
        {
            int      remaining = _random.Next(10, 60);
            DateTime resetTime = DateTime.Now.AddSeconds(_random.Next(5, 30));

            IRedditRateLimitLogic sut = CreateSut(remaining: remaining, resetTime: resetTime);

            bool result = sut.WillExceedRateLimit(remaining - _random.Next(1, 10));

            Assert.IsFalse(result);
        }
Exemple #2
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeEqualToNull_AssertHandleAsyncWasNotCalledOnExceptionHandler()
        {
            int      used         = _random.Next(30, 60);
            int      remaining    = _random.Next(30, 60);
            DateTime?resetTime    = null;
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut();

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            _exceptionHandlerMock.Verify(m => m.HandleAsync(It.IsAny <AggregateException>()), Times.Never);
            _exceptionHandlerMock.Verify(m => m.HandleAsync(It.IsAny <Exception>()), Times.Never);
        }
Exemple #3
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeEqualToNullAndRemainingEqualToExistingRemaining_ExpectRemainingUntouched()
        {
            int existingRemaining = _random.Next(30, 60);
            int remaining         = existingRemaining;

            int      used         = _random.Next(30, 60);
            DateTime?resetTime    = null;
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(remaining: existingRemaining);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(existingRemaining, sut.Remaining);
        }
Exemple #4
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeEqualToNullAndUsedGreaterThanAlreadyUsed_ExpectUsedEqualToInputValue()
        {
            int alreadyUsed = _random.Next(30, 60);
            int used        = alreadyUsed + _random.Next(1, 10);

            int      remaining    = _random.Next(30, 60);
            DateTime?resetTime    = null;
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(used: alreadyUsed);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(used, sut.Used);
        }
Exemple #5
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeLaterThanExistingResetTime_ExpectResetTimeEqualToInputValue()
        {
            DateTime existingResetTime = DateTime.Now.AddSeconds(_random.Next(30, 60));
            DateTime?resetTime         = existingResetTime.AddSeconds(_random.Next(1, 30));

            int      used         = _random.Next(30, 60);
            int      remaining    = _random.Next(30, 60);
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(resetTime: existingResetTime);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(resetTime, sut.ResetTime);
        }
Exemple #6
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeLaterThanExistingResetTime_AssertHandleAsyncWasNotCalledOnExceptionHandler()
        {
            DateTime existingResetTime = DateTime.Now.AddSeconds(_random.Next(30, 60));
            DateTime?resetTime         = existingResetTime.AddSeconds(_random.Next(1, 30));

            int      used         = _random.Next(30, 60);
            int      remaining    = _random.Next(30, 60);
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(resetTime: existingResetTime);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            _exceptionHandlerMock.Verify(m => m.HandleAsync(It.IsAny <AggregateException>()), Times.Never);
            _exceptionHandlerMock.Verify(m => m.HandleAsync(It.IsAny <Exception>()), Times.Never);
        }
Exemple #7
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeEqualToExistingResetTimeAndUsedEqualToAlreadyUsed_ExpectUsedUntouched()
        {
            int alreadyUsed = _random.Next(30, 60);
            int used        = alreadyUsed;

            DateTime existingResetTime = DateTime.Now.AddSeconds(_random.Next(30, 60));
            DateTime?resetTime         = existingResetTime;

            int      remaining    = _random.Next(30, 60);
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(used: alreadyUsed, resetTime: existingResetTime);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(alreadyUsed, sut.Used);
        }
Exemple #8
0
        public async Task EnforceRateLimitAsync_WhenCalledWithResetTimeBeforeThanExistingResetTimeAndRemainingGreaterThanExistingRemaining_ExpectRemainingUntouched()
        {
            int existingRemaining = _random.Next(30, 60);
            int remaining         = existingRemaining + _random.Next(1, 10);

            DateTime existingResetTime = DateTime.Now.AddSeconds(_random.Next(30, 60));
            DateTime?resetTime         = existingResetTime.AddSeconds(_random.Next(1, 25) * -1);

            int      used         = _random.Next(30, 60);
            DateTime receivedTime = DateTime.Now;

            IRedditRateLimitLogic sut = CreateSut(remaining: existingRemaining, resetTime: existingResetTime);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(existingRemaining, sut.Remaining);
        }
Exemple #9
0
        public async Task EnforceRateLimitAsync_WhenCalledWithReceivedTimeLowerThanLatestReceivedTime_ExpectUsedUntouched()
        {
            int alreadyUsed = _random.Next(30, 60);
            int used        = alreadyUsed + _random.Next(1, 10);

            DateTime existingResetTime = DateTime.Now.AddSeconds(_random.Next(10, 60));
            DateTime?resetTime         = existingResetTime.AddSeconds(_random.Next(10, 60));

            int      remaining    = _random.Next(30, 60);
            DateTime receivedTime = DateTime.Now;

            DateTime latestReceivedTime = receivedTime.AddSeconds(_random.Next(10, 30));
            IRedditRateLimitLogic sut   = CreateSut(used: alreadyUsed, resetTime: existingResetTime, latestReceivedTime: latestReceivedTime);

            await sut.EnforceRateLimitAsync(used, remaining, resetTime, receivedTime);

            Assert.AreEqual(alreadyUsed, sut.Used);
        }
        public void Constructor_WhenCalled_AssertResetUtcTimeEqualToNowWithOneMinute()
        {
            IRedditRateLimitLogic sut = CreateSut();

            Assert.IsTrue(sut.ResetUtcTime >= DateTime.UtcNow && sut.ResetUtcTime <= DateTime.UtcNow.AddMinutes(1));
        }
        public void Constructor_WhenCalled_AssertRemainingEqualTo60()
        {
            IRedditRateLimitLogic sut = CreateSut();

            Assert.AreEqual(60, sut.Remaining);
        }
        public void Constructor_WhenCalled_AssertUsedEqualToZero()
        {
            IRedditRateLimitLogic sut = CreateSut();

            Assert.AreEqual(0, sut.Used);
        }