From 13bc96fc8a2282ccefdbc76c436a23f23fcca4cc Mon Sep 17 00:00:00 2001 From: Cristian Pufu Date: Mon, 13 Nov 2023 09:19:16 +0200 Subject: [PATCH] Fix unit tests --- .../Concurrency/RedisConcurrencyRateLimiter.cs | 1 + .../FixedWindow/RedisFixedWindowRateLimiter.cs | 1 + .../SlidingWindow/RedisSlidingWindowRateLimiter.cs | 1 + .../TokenBucket/RedisTokenBucketRateLimiter.cs | 1 + .../UnitTests/ConcurrencyUnitTests.cs | 10 ++++------ .../UnitTests/FixedWindowUnitTests.cs | 4 +--- .../UnitTests/SlidingWindowUnitTests.cs | 8 +++----- .../UnitTests/TokenBucketUnitTests.cs | 4 +--- 8 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs b/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs index 20871d8..e2d8ef8 100644 --- a/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs +++ b/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs @@ -76,6 +76,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { + // https://github.com/cristipufu/aspnetcore-redis-rate-limiting/issues/66 return FailedLease; } diff --git a/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs b/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs index 2c1cdcf..b2998d2 100644 --- a/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs +++ b/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs @@ -62,6 +62,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { + // https://github.com/cristipufu/aspnetcore-redis-rate-limiting/issues/66 return FailedLease; } diff --git a/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs b/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs index 8bbd4b6..53ad2ec 100644 --- a/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs +++ b/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs @@ -62,6 +62,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { + // https://github.com/cristipufu/aspnetcore-redis-rate-limiting/issues/66 return FailedLease; } diff --git a/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs b/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs index 42ae207..875123f 100644 --- a/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs +++ b/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs @@ -67,6 +67,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { + // https://github.com/cristipufu/aspnetcore-redis-rate-limiting/issues/66 return FailedLease; } diff --git a/test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs b/test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs index 6c3a2f2..fed46f9 100644 --- a/test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs +++ b/test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs @@ -48,9 +48,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit() QueueLimit = 1, ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - var ex = Assert.Throws(() => limiter.AttemptAcquire(2)); - Assert.Equal("permitCount", ex.ParamName); - ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); + var ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); Assert.Equal("permitCount", ex.ParamName); } @@ -89,7 +87,7 @@ public async Task CanAcquireAsyncResource() Assert.Equal(1, stats.CurrentAvailablePermits); } - [Fact] + [Fact(Skip = "Sync always returns failed lease")] public void CanAcquireResource() { using var limiter = new RedisConcurrencyRateLimiter( @@ -161,7 +159,7 @@ public async Task FailsWhenQueuingMoreThanLimit() ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - using var lease = limiter.AttemptAcquire(); + using var lease = await limiter.AcquireAsync(); var wait = limiter.AcquireAsync(); var failedLease = await limiter.AcquireAsync(); @@ -182,7 +180,7 @@ public async Task QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable() ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - var lease = limiter.AttemptAcquire(); + var lease = await limiter.AcquireAsync(); var wait = limiter.AcquireAsync(); await Task.Delay(1000); diff --git a/test/RedisRateLimiting.Tests/UnitTests/FixedWindowUnitTests.cs b/test/RedisRateLimiting.Tests/UnitTests/FixedWindowUnitTests.cs index d85d92f..0023868 100644 --- a/test/RedisRateLimiting.Tests/UnitTests/FixedWindowUnitTests.cs +++ b/test/RedisRateLimiting.Tests/UnitTests/FixedWindowUnitTests.cs @@ -57,9 +57,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit() Window = TimeSpan.FromMinutes(1), ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - var ex = Assert.Throws(() => limiter.AttemptAcquire(2)); - Assert.Equal("permitCount", ex.ParamName); - ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); + var ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); Assert.Equal("permitCount", ex.ParamName); } diff --git a/test/RedisRateLimiting.Tests/UnitTests/SlidingWindowUnitTests.cs b/test/RedisRateLimiting.Tests/UnitTests/SlidingWindowUnitTests.cs index 869317c..c1bf730 100644 --- a/test/RedisRateLimiting.Tests/UnitTests/SlidingWindowUnitTests.cs +++ b/test/RedisRateLimiting.Tests/UnitTests/SlidingWindowUnitTests.cs @@ -64,9 +64,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit() Window = TimeSpan.FromMinutes(1), ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - var ex = Assert.Throws(() => limiter.AttemptAcquire(2)); - Assert.Equal("permitCount", ex.ParamName); - ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); + var ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); Assert.Equal("permitCount", ex.ParamName); } @@ -110,7 +108,7 @@ public async Task CanAcquireAsyncResourceWithSmallWindow() new RedisSlidingWindowRateLimiterOptions { PermitLimit = 1, - Window = TimeSpan.FromMilliseconds(100), + Window = TimeSpan.FromMilliseconds(600), ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); @@ -120,7 +118,7 @@ public async Task CanAcquireAsyncResourceWithSmallWindow() using var lease2 = await limiter.AcquireAsync(); Assert.False(lease2.IsAcquired); - await Task.Delay(TimeSpan.FromMilliseconds(100)); + await Task.Delay(TimeSpan.FromMilliseconds(600)); using var lease3 = await limiter.AcquireAsync(); Assert.True(lease3.IsAcquired); diff --git a/test/RedisRateLimiting.Tests/UnitTests/TokenBucketUnitTests.cs b/test/RedisRateLimiting.Tests/UnitTests/TokenBucketUnitTests.cs index 0280650..b44c586 100644 --- a/test/RedisRateLimiting.Tests/UnitTests/TokenBucketUnitTests.cs +++ b/test/RedisRateLimiting.Tests/UnitTests/TokenBucketUnitTests.cs @@ -77,9 +77,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit() ReplenishmentPeriod = TimeSpan.FromMinutes(1), ConnectionMultiplexerFactory = Fixture.ConnectionMultiplexerFactory, }); - var ex = Assert.Throws(() => limiter.AttemptAcquire(2)); - Assert.Equal("permitCount", ex.ParamName); - ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); + var ex = await Assert.ThrowsAsync(async () => await limiter.AcquireAsync(2)); Assert.Equal("permitCount", ex.ParamName); }