public async Task ApiKeyRateLimiterBasics(string key1, string key2, string endpoint1, string endpoint2, bool signed1, bool signed2, bool onlyForSignedRequests, bool expectLimited) { var log = new Log("Test"); log.Level = LogLevel.Trace; var rateLimiter = new RateLimiter(); rateLimiter.AddApiKeyLimit(1, TimeSpan.FromSeconds(0.1), onlyForSignedRequests, false); var result1 = await rateLimiter.LimitRequestAsync(log, endpoint1, HttpMethod.Get, signed1, key1?.ToSecureString(), RateLimitingBehaviour.Wait, 1, default); var result2 = await rateLimiter.LimitRequestAsync(log, endpoint2, HttpMethod.Get, signed2, key2?.ToSecureString(), RateLimitingBehaviour.Wait, 1, default); Assert.IsTrue(result1.Data == 0); Assert.IsTrue(expectLimited ? result2.Data > 0 : result2.Data == 0); }
public async Task ApiKeyRateLimiterIgnores_TotalRateLimiter_IfSet(string endpoint, bool signed1, bool signed2, bool ignoreTotal, bool expectLimited) { var log = new Log("Test"); log.Level = LogLevel.Trace; var rateLimiter = new RateLimiter(); rateLimiter.AddApiKeyLimit(100, TimeSpan.FromSeconds(0.1), true, ignoreTotal); rateLimiter.AddTotalRateLimit(1, TimeSpan.FromSeconds(0.1)); var result1 = await rateLimiter.LimitRequestAsync(log, endpoint, HttpMethod.Get, signed1, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default); var result2 = await rateLimiter.LimitRequestAsync(log, endpoint, HttpMethod.Get, signed2, "123".ToSecureString(), RateLimitingBehaviour.Wait, 1, default); Assert.IsTrue(result1.Data == 0); Assert.IsTrue(expectLimited ? result2.Data > 0 : result2.Data == 0); }