Exemple #1
0
        public async Task PartialEndpointRateLimiterEndpoints(string endpoint1, string endpoint2, bool expectLimiting)
        {
            var log = new Log("Test");

            log.Level = LogLevel.Trace;

            var rateLimiter = new RateLimiter();

            rateLimiter.AddPartialEndpointLimit("/sapi/", 1, TimeSpan.FromSeconds(0.1), countPerEndpoint: true);

            var result1 = await rateLimiter.LimitRequestAsync(log, endpoint1, HttpMethod.Get, false, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default);

            var result2 = await rateLimiter.LimitRequestAsync(log, endpoint2, HttpMethod.Get, false, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default);

            Assert.IsTrue(result1.Data == 0);
            Assert.IsTrue(expectLimiting ? result2.Data > 0 : result2.Data == 0);
        }
Exemple #2
0
        public async Task PartialEndpointRateLimiterEndpoints(string endpoint, bool expectLimiting)
        {
            var log = new Log("Test");

            log.Level = LogLevel.Trace;

            var rateLimiter = new RateLimiter();

            rateLimiter.AddPartialEndpointLimit("/sapi/", 1, TimeSpan.FromSeconds(0.1));

            for (var i = 0; i < 2; i++)
            {
                var result1 = await rateLimiter.LimitRequestAsync(log, endpoint, HttpMethod.Get, false, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default);

                bool expected = i == 1 ? (expectLimiting ? result1.Data > 1 : result1.Data == 0) : result1.Data == 0;
                Assert.IsTrue(expected);
            }
        }
Exemple #3
0
        public async Task PartialEndpointRateLimiterBasics(int requests, double perSeconds)
        {
            var log = new Log("Test");

            log.Level = LogLevel.Trace;

            var rateLimiter = new RateLimiter();

            rateLimiter.AddPartialEndpointLimit("/sapi/", requests, TimeSpan.FromSeconds(perSeconds));

            for (var i = 0; i < requests + 1; i++)
            {
                var result1 = await rateLimiter.LimitRequestAsync(log, "/sapi/v1/system/status", HttpMethod.Get, false, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default);

                Assert.IsTrue(i == requests? result1.Data > 1 : result1.Data == 0);
            }

            await Task.Delay((int)Math.Round(perSeconds * 1000) + 10);

            var result2 = await rateLimiter.LimitRequestAsync(log, "/sapi/v1/system/status", HttpMethod.Get, false, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default);

            Assert.IsTrue(result2.Data == 0);
        }