Skip to content

Commit

Permalink
Fix tests flakyness
Browse files Browse the repository at this point in the history
  • Loading branch information
cristipufu committed Nov 16, 2023
1 parent 98f6b9c commit 0be3a10
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
22 changes: 9 additions & 13 deletions test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void InvalidOptionsThrows()
public async Task ThrowsWhenAcquiringMoreThanLimit()
{
var limiter = new RedisConcurrencyRateLimiter<string>(
string.Empty,
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -55,10 +55,8 @@ public async Task ThrowsWhenAcquiringMoreThanLimit()
[Fact]
public async Task CanAcquireAsyncResource()
{
await Fixture.ClearStatisticsAsync("cc", "Test_CanAcquireAsyncResource");

using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_CanAcquireAsyncResource",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand Down Expand Up @@ -91,7 +89,7 @@ public async Task CanAcquireAsyncResource()
public void CanAcquireResource()
{
using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_CanAcquireResource",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -117,7 +115,7 @@ public void CanAcquireResource()
public async Task CanAcquireResourceAsyncQueuesAndGrabsOldest()
{
using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_CanAcquireResourceAsyncQueuesAndGrabsOldest",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand Down Expand Up @@ -151,7 +149,7 @@ public async Task CanAcquireResourceAsyncQueuesAndGrabsOldest()
public async Task FailsWhenQueuingMoreThanLimit()
{
using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_FailsWhenQueuingMoreThanLimit",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -169,10 +167,8 @@ public async Task FailsWhenQueuingMoreThanLimit()
[Fact]
public async Task QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable()
{
await Fixture.ClearStatisticsAsync("cc", "Test_QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable");

using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand Down Expand Up @@ -217,7 +213,7 @@ public async Task QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable()
public async Task CanDequeueMultipleResourcesAtOnce()
{
using var limiter = new RedisConcurrencyRateLimiter<string>(
"Test_CanDequeueMultipleResourcesAtOnce",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 2,
Expand Down Expand Up @@ -249,7 +245,7 @@ public async Task CanDequeueMultipleResourcesAtOnce()
public async Task CanCancelAcquireAsyncAfterQueuing()
{
var limiter = new RedisConcurrencyRateLimiter<string>(
"CanCancelAcquireAsyncAfterQueuing",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand Down Expand Up @@ -299,7 +295,7 @@ public async Task CanCancelAcquireAsyncAfterQueuing()
public async Task GetPermitWhilePermitEmptyQueueNotEmptyGetsQueued()
{
using var limiter = new RedisConcurrencyRateLimiter<string>(
"GetPermitWhilePermitEmptyQueueNotEmptyGetsQueued",
partitionKey: Guid.NewGuid().ToString(),
new RedisConcurrencyRateLimiterOptions
{
PermitLimit = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void InvalidOptionsThrows()
public async Task ThrowsWhenAcquiringMoreThanLimit()
{
var limiter = new RedisFixedWindowRateLimiter<string>(
string.Empty,
partitionKey: Guid.NewGuid().ToString(),
new RedisFixedWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -65,7 +65,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit()
public async Task CanAcquireAsyncResource()
{
using var limiter = new RedisFixedWindowRateLimiter<string>(
"Test_CanAcquireAsyncResource_FW",
partitionKey: Guid.NewGuid().ToString(),
new RedisFixedWindowRateLimiterOptions
{
PermitLimit = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void InvalidOptionsThrows()
public async Task ThrowsWhenAcquiringMoreThanLimit()
{
var limiter = new RedisSlidingWindowRateLimiter<string>(
string.Empty,
partitionKey: Guid.NewGuid().ToString(),
new RedisSlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand All @@ -71,10 +71,8 @@ public async Task ThrowsWhenAcquiringMoreThanLimit()
[Fact]
public async Task CanAcquireAsyncResource()
{
await Fixture.ClearStatisticsAsync("sw", "Test_CanAcquireAsyncResource_SW");

using var limiter = new RedisSlidingWindowRateLimiter<string>(
"Test_CanAcquireAsyncResource_SW",
partitionKey: Guid.NewGuid().ToString(),
new RedisSlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand Down Expand Up @@ -104,7 +102,7 @@ public async Task CanAcquireAsyncResource()
public async Task CanAcquireAsyncResourceWithSmallWindow()
{
using var limiter = new RedisSlidingWindowRateLimiter<string>(
string.Empty,
partitionKey: Guid.NewGuid().ToString(),
new RedisSlidingWindowRateLimiterOptions
{
PermitLimit = 1,
Expand Down
7 changes: 0 additions & 7 deletions test/RedisRateLimiting.Tests/UnitTests/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ public TestFixture()
ConnectionMultiplexerFactory = () => ConnectionMultiplexer;
}

public async Task ClearStatisticsAsync(string rl, string partitionKey)
{
var key = $"rl:{rl}:{{{partitionKey}}}:stats";
var database = ConnectionMultiplexer.GetDatabase();
await database.KeyDeleteAsync(key);
}

public void Dispose()
{
Dispose(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void InvalidOptionsThrows()
public async Task ThrowsWhenAcquiringMoreThanLimit()
{
var limiter = new RedisTokenBucketRateLimiter<string>(
string.Empty,
partitionKey: Guid.NewGuid().ToString(),
new RedisTokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand All @@ -85,7 +85,7 @@ public async Task ThrowsWhenAcquiringMoreThanLimit()
public async Task CanAcquireAsyncResource()
{
using var limiter = new RedisTokenBucketRateLimiter<string>(
"Test_CanAcquireAsyncResource_TB",
partitionKey: Guid.NewGuid().ToString(),
new RedisTokenBucketRateLimiterOptions
{
TokenLimit = 1,
Expand Down

0 comments on commit 0be3a10

Please sign in to comment.