|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace StackExchange.Redis.Tests; |
| 6 | + |
| 7 | +[RunPerProtocol] |
| 8 | +public class GcraIntegrationTests(ITestOutputHelper output, SharedConnectionFixture fixture) : TestBase(output, fixture) |
| 9 | +{ |
| 10 | + [Fact(Timeout = 5000)] |
| 11 | + public async Task GcraRateLimit_SmokeTest() |
| 12 | + { |
| 13 | + await using var conn = Create(require: new Version(8, 8, 0)); |
| 14 | + var db = conn.GetDatabase(); |
| 15 | + var key = Me(); |
| 16 | + db.KeyDelete(key, CommandFlags.FireAndForget); |
| 17 | + for (int i = 0; i < 15; i++) |
| 18 | + { |
| 19 | + var result = await db.StringGcraRateLimitAsync(key, maxBurst: 10, requestsPerPeriod: 10, periodSeconds: 1.0); |
| 20 | + Log($"Run {i}: Limited: {result.Limited}, Available: {result.AvailableRequests}, RetryAfter: {result.RetryAfterSeconds}, FullBurstAfter: {result.FullBurstAfterSeconds}, MaxRequests: {result.MaxRequests}"); |
| 21 | + if (i <= 10) |
| 22 | + { |
| 23 | + Assert.False(result.Limited, $"run {i}"); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + Assert.True(result.Limited, $"run {i}"); |
| 28 | + } |
| 29 | + Assert.Equal(11, result.MaxRequests); |
| 30 | + Assert.Equal(Math.Max(0, 10 - i), result.AvailableRequests); |
| 31 | + if (result.Limited) |
| 32 | + { |
| 33 | + Assert.True(result.RetryAfterSeconds > 0); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + Assert.Equal(-1, result.RetryAfterSeconds); |
| 38 | + } |
| 39 | + Assert.True(result.FullBurstAfterSeconds > 0); |
| 40 | + } |
| 41 | + await db.TryAcquireGcraAsync( |
| 42 | + key, |
| 43 | + maxBurst: 10, |
| 44 | + requestsPerPeriod: 10, |
| 45 | + allow: TimeSpan.FromSeconds(5), |
| 46 | + cancellationToken: TestContext.Current.CancellationToken); |
| 47 | + } |
| 48 | +} |
0 commit comments