Пример #1
0
        public bool IsLimited(RateLimiterRules rule, string accessToken)
        {
            //Get Configuration
            var timePeriod            = config.Period;
            var maxActionsInPeriod    = config.MaxActionsInPeriod;
            var minTimeBetweenActions = config.MinTimeBetweenActions;

            // Get Clients Data
            var actionsInPeriod = data.ClientActionsInPeriod(accessToken, timePeriod);
            var lastAction      = data.LastClientAction(accessToken);

            if (RateLimiterRules.All == rule)
            {
                return(IsMaxActionInPeriod(actionsInPeriod, maxActionsInPeriod) || IsTimeBetweenActions(lastAction, minTimeBetweenActions));
            }
            else if (RateLimiterRules.MaximumAcctionsInTimeSpan == rule)
            {
                return(IsMaxActionInPeriod(actionsInPeriod, maxActionsInPeriod));
            }
            else if (RateLimiterRules.TimeBetweenActions == rule)
            {
                return(IsTimeBetweenActions(lastAction, minTimeBetweenActions));
            }

            return(false);
        }
        public void IsLimited_StateUnderTest_ExpectedBehavior(RateLimiterRules rule, int actionCount, bool expextedResult)
        {
            // Arrange
            var service = this.CreateService();

            mockDataAccess.Setup(m => m.ClientActionsInPeriod(It.Is <string>(s => s.Equals("test")), It.Is <TimeSpan>(t => t.TotalMinutes == 5)))
            .Returns(actionCount);

            var mockedDate = DateTime.Now;

            mockDataAccess.Setup(m => m.LastClientAction(It.Is <string>(s => s.Equals("test"))))
            .Returns(mockedDate);

            string accessToken = "test";

            // Act
            var result = service.IsLimited(
                rule,
                accessToken);

            // Assert
            Assert.Equal(expextedResult, result);
        }
Пример #3
0
 public bool IsLimited(RateLimiterRules rule, string accessToken)
 {
     return(this.RateService.IsLimited(rule, accessToken));
 }