|
| 1 | +using Buttercup.TestUtils; |
| 2 | +using Microsoft.Extensions.Time.Testing; |
| 3 | +using Moq; |
| 4 | +using StackExchange.Redis; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Buttercup.Security; |
| 8 | + |
| 9 | +public sealed class SlidingWindowRateLimiterTests |
| 10 | +{ |
| 11 | + private readonly FakeTimeProvider timeProvider = new(); |
| 12 | + |
| 13 | + private readonly SlidingWindowRateLimiterOptions options = new() |
| 14 | + { |
| 15 | + Limit = 15, |
| 16 | + SegmentsPerWindow = 5, |
| 17 | + Window = TimeSpan.FromMilliseconds(50), |
| 18 | + }; |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public async Task IsAllowed_ReturnsTrueOrFalseBasedOnLimit() |
| 22 | + { |
| 23 | + var keySuffix = Random.Shared.Next(); |
| 24 | + var key1 = $"Foo{keySuffix}"; |
| 25 | + var key2 = $"Bar{keySuffix}"; |
| 26 | + |
| 27 | + using var connection = await RedisConnection.GetConnection(); |
| 28 | + var connectionManager = new FakeRedisConnectionManager(connection); |
| 29 | + var rateLimiter = new SlidingWindowRateLimiter(connectionManager, this.timeProvider); |
| 30 | + |
| 31 | + for (var i = 0; i < 5; i++) |
| 32 | + { |
| 33 | + Assert.True(await rateLimiter.IsAllowed(key1, this.options)); |
| 34 | + } |
| 35 | + |
| 36 | + this.timeProvider.Advance(TimeSpan.FromMilliseconds(10)); |
| 37 | + |
| 38 | + for (var i = 0; i < 5; i++) |
| 39 | + { |
| 40 | + Assert.True(await rateLimiter.IsAllowed(key1, this.options)); |
| 41 | + } |
| 42 | + |
| 43 | + this.timeProvider.Advance(TimeSpan.FromMilliseconds(20)); |
| 44 | + |
| 45 | + for (var i = 0; i < 5; i++) |
| 46 | + { |
| 47 | + Assert.True(await rateLimiter.IsAllowed(key1, this.options)); |
| 48 | + } |
| 49 | + |
| 50 | + Assert.False(await rateLimiter.IsAllowed(key1, this.options)); |
| 51 | + Assert.True(await rateLimiter.IsAllowed(key2, this.options)); |
| 52 | + |
| 53 | + this.timeProvider.Advance(TimeSpan.FromMilliseconds(20)); |
| 54 | + |
| 55 | + for (var i = 0; i < 5; i++) |
| 56 | + { |
| 57 | + Assert.True(await rateLimiter.IsAllowed(key1, this.options)); |
| 58 | + } |
| 59 | + |
| 60 | + Assert.False(await rateLimiter.IsAllowed(key1, this.options)); |
| 61 | + Assert.True(await rateLimiter.IsAllowed(key2, this.options)); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task IsAllowed_ChecksAndRethrowsExceptions() |
| 66 | + { |
| 67 | + var connectionMock = new Mock<IConnectionMultiplexer>(); |
| 68 | + var connectionManager = new FakeRedisConnectionManager(connectionMock.Object); |
| 69 | + var rateLimiter = new SlidingWindowRateLimiter(connectionManager, this.timeProvider); |
| 70 | + |
| 71 | + var expectedException = new RedisException("Fake exception"); |
| 72 | + connectionMock.Setup(x => x.GetDatabase(-1, null)).Throws(expectedException); |
| 73 | + |
| 74 | + Assert.Same( |
| 75 | + expectedException, |
| 76 | + await Assert.ThrowsAsync<RedisException>( |
| 77 | + () => rateLimiter.IsAllowed("Foo", this.options))); |
| 78 | + Assert.Same(expectedException, Assert.Single(connectionManager.CheckedExceptions)); |
| 79 | + } |
| 80 | +} |
0 commit comments