-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/MatrixTextClient.Tests/LeakyBucketRateLimiterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using MatrixTextClient.Http; | ||
|
||
|
||
namespace MatrixTextClient.Tests | ||
{ | ||
public class LeakyBucketRateLimiterTests | ||
{ | ||
[Test] | ||
public async Task Constructor_ValidParameters_ShouldInitializeCorrectly() | ||
{ | ||
// Arrange | ||
int capacity = 10; | ||
int maxLeaksPerHour = 600; | ||
|
||
// Act | ||
var rateLimiter = new LeakyBucketRateLimiter(capacity, maxLeaksPerHour); | ||
|
||
// Assert | ||
await Assert.That(capacity).IsEqualTo(rateLimiter.Capacity); | ||
await Assert.That(maxLeaksPerHour).IsEqualTo(rateLimiter.MaxLeaksPerHour); | ||
await Assert.That(capacity).IsEqualTo(rateLimiter.WaterLevel); | ||
} | ||
|
||
[Test] | ||
public async Task Constructor_InvalidParameters_ShouldThrowArgumentException() | ||
{ | ||
// Arrange | ||
int invalidCapacity = 400; | ||
int invalidMaxLeaksPerHour = 5; | ||
|
||
// Act & Assert | ||
await Assert.That(() => new LeakyBucketRateLimiter(invalidCapacity, 600)).Throws<ArgumentException>(); | ||
await Assert.That(() => new LeakyBucketRateLimiter(10, invalidMaxLeaksPerHour)).Throws<ArgumentException>(); | ||
} | ||
|
||
[Test] | ||
public async Task Leak_WhenQuotaAvailable_ShouldReturnTrue() | ||
{ | ||
// Arrange | ||
var rateLimiter = new LeakyBucketRateLimiter(10, 600); | ||
|
||
// Act | ||
bool result = rateLimiter.Leak(); | ||
|
||
// await Assert | ||
await Assert.That(result).IsTrue(); | ||
await Assert.That(rateLimiter.WaterLevel).IsEqualTo(9); | ||
} | ||
|
||
[Test] | ||
public async Task Leak_WhenQuotaNotAvailable_ShouldReturnFalse() | ||
{ | ||
// Arrange | ||
var rateLimiter = new LeakyBucketRateLimiter(1, 600); | ||
rateLimiter.Leak(); // Deplete the quota | ||
|
||
// Act | ||
bool result = rateLimiter.Leak(); | ||
|
||
// await Assert | ||
await Assert.That(result).IsFalse(); | ||
await Assert.That(rateLimiter.WaterLevel).IsZero(); | ||
} | ||
|
||
[Test] | ||
public async Task Leak_WhenQuotaRestored_ShouldReturnTrue() | ||
{ | ||
// Arrange | ||
var rateLimiter = new LeakyBucketRateLimiter(1, 6000); | ||
rateLimiter.Leak(); // Deplete the quota | ||
|
||
// Simulate time passing to restore quota | ||
await Task.Delay(1000); // Sleep for 1 second to allow quota restoration | ||
|
||
// Act | ||
bool result = rateLimiter.Leak(); | ||
|
||
// await Assert | ||
await Assert.That(result).IsTrue(); | ||
} | ||
|
||
[Test] | ||
public async Task Leak_MultipleThreads_ShouldBeThreadSafe() | ||
{ | ||
// Arrange | ||
var rateLimiter = new LeakyBucketRateLimiter(10, 600); | ||
int successCount = 0; | ||
int threadCount = 20; | ||
|
||
// Act | ||
Parallel.For(0, threadCount, _ => | ||
{ | ||
if (rateLimiter.Leak()) | ||
{ | ||
Interlocked.Increment(ref successCount); | ||
} | ||
}); | ||
|
||
// await Assert | ||
await Assert.That(successCount).IsEqualTo(10); // Only 10 requests should succeed | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters