|
| 1 | +using System.Threading.RateLimiting; |
| 2 | +using ManagedCode.Orleans.RateLimiting.Core.Extensions; |
| 3 | +using ManagedCode.Orleans.RateLimiting.Core.Interfaces; |
| 4 | +using ManagedCode.Orleans.RateLimiting.Core.Models.Holders; |
| 5 | +using ManagedCode.Orleans.RateLimiting.Tests.Cluster; |
| 6 | +using Orleans; |
| 7 | +using Orleans.Runtime; |
| 8 | + |
| 9 | +namespace ManagedCode.Orleans.RateLimiting.Tests; |
| 10 | + |
| 11 | +[ClassDataSource<TestClusterApplication>(Shared = SharedType.PerTestSession)] |
| 12 | +public class RateLimiterConfigurationAndSnapshotPersistenceTests |
| 13 | +{ |
| 14 | + private const int ExpectedAvailablePermits = PermitLimit - PermitCount; |
| 15 | + private const int ManagementGrainKey = 0; |
| 16 | + private const int PermitCount = 2; |
| 17 | + private const int PermitLimit = 5; |
| 18 | + private const int QueueLimit = 0; |
| 19 | + private const int SegmentsPerWindow = 2; |
| 20 | + private const int TokensPerPeriod = 1; |
| 21 | + private const int WindowMinutes = 10; |
| 22 | + |
| 23 | + private static readonly TimeSpan ActivationCollectionAge = TimeSpan.Zero; |
| 24 | + private static readonly TimeSpan FixedWindow = TimeSpan.FromMinutes(WindowMinutes); |
| 25 | + |
| 26 | + private readonly TestClusterApplication _testApp; |
| 27 | + |
| 28 | + public RateLimiterConfigurationAndSnapshotPersistenceTests(TestClusterApplication testApp) |
| 29 | + { |
| 30 | + _testApp = testApp; |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task FixedWindowConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection() |
| 35 | + { |
| 36 | + var rateLimiter = _testApp.Cluster.Client.GetFixedWindowRateLimiter($"{nameof(FixedWindowConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection)}-{Guid.NewGuid():N}"); |
| 37 | + var options = new FixedWindowRateLimiterOptions |
| 38 | + { |
| 39 | + AutoReplenishment = false, |
| 40 | + PermitLimit = PermitLimit, |
| 41 | + QueueLimit = QueueLimit, |
| 42 | + Window = FixedWindow |
| 43 | + }; |
| 44 | + |
| 45 | + await rateLimiter.Configure(options); |
| 46 | + await AcquireAndDisposeAsync(rateLimiter); |
| 47 | + await ForceActivationCollectionAsync(); |
| 48 | + |
| 49 | + var configuration = await rateLimiter.GetConfiguration(); |
| 50 | + configuration.PermitLimit.ShouldBe(PermitLimit); |
| 51 | + configuration.QueueLimit.ShouldBe(QueueLimit); |
| 52 | + configuration.Window.ShouldBe(FixedWindow); |
| 53 | + |
| 54 | + await AssertCurrentAvailablePermitsAsync(rateLimiter); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public async Task SlidingWindowConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection() |
| 59 | + { |
| 60 | + var rateLimiter = _testApp.Cluster.Client.GetSlidingWindowRateLimiter($"{nameof(SlidingWindowConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection)}-{Guid.NewGuid():N}"); |
| 61 | + var options = new SlidingWindowRateLimiterOptions |
| 62 | + { |
| 63 | + AutoReplenishment = false, |
| 64 | + PermitLimit = PermitLimit, |
| 65 | + QueueLimit = QueueLimit, |
| 66 | + SegmentsPerWindow = SegmentsPerWindow, |
| 67 | + Window = FixedWindow |
| 68 | + }; |
| 69 | + |
| 70 | + await rateLimiter.Configure(options); |
| 71 | + await AcquireAndDisposeAsync(rateLimiter); |
| 72 | + await ForceActivationCollectionAsync(); |
| 73 | + |
| 74 | + var configuration = await rateLimiter.GetConfiguration(); |
| 75 | + configuration.PermitLimit.ShouldBe(PermitLimit); |
| 76 | + configuration.QueueLimit.ShouldBe(QueueLimit); |
| 77 | + configuration.SegmentsPerWindow.ShouldBe(SegmentsPerWindow); |
| 78 | + configuration.Window.ShouldBe(FixedWindow); |
| 79 | + |
| 80 | + await AssertCurrentAvailablePermitsAsync(rateLimiter); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public async Task TokenBucketConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection() |
| 85 | + { |
| 86 | + var rateLimiter = _testApp.Cluster.Client.GetTokenBucketRateLimiter($"{nameof(TokenBucketConfigurationAndCurrentQuotaValueSurviveForcedActivationCollection)}-{Guid.NewGuid():N}"); |
| 87 | + var options = new TokenBucketRateLimiterOptions |
| 88 | + { |
| 89 | + AutoReplenishment = false, |
| 90 | + QueueLimit = QueueLimit, |
| 91 | + ReplenishmentPeriod = FixedWindow, |
| 92 | + TokenLimit = PermitLimit, |
| 93 | + TokensPerPeriod = TokensPerPeriod |
| 94 | + }; |
| 95 | + |
| 96 | + await rateLimiter.Configure(options); |
| 97 | + await AcquireAndDisposeAsync(rateLimiter); |
| 98 | + await ForceActivationCollectionAsync(); |
| 99 | + |
| 100 | + var configuration = await rateLimiter.GetConfiguration(); |
| 101 | + configuration.QueueLimit.ShouldBe(QueueLimit); |
| 102 | + configuration.ReplenishmentPeriod.ShouldBe(FixedWindow); |
| 103 | + configuration.TokenLimit.ShouldBe(PermitLimit); |
| 104 | + configuration.TokensPerPeriod.ShouldBe(TokensPerPeriod); |
| 105 | + |
| 106 | + await AssertCurrentAvailablePermitsAsync(rateLimiter); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public async Task ConcurrencyConfigurationCurrentQuotaValueAndActiveLeasesSurviveForcedActivationCollection() |
| 111 | + { |
| 112 | + var rateLimiter = _testApp.Cluster.Client.GetConcurrencyLimiter($"{nameof(ConcurrencyConfigurationCurrentQuotaValueAndActiveLeasesSurviveForcedActivationCollection)}-{Guid.NewGuid():N}"); |
| 113 | + var options = new ConcurrencyLimiterOptions |
| 114 | + { |
| 115 | + PermitLimit = PermitLimit, |
| 116 | + QueueLimit = QueueLimit |
| 117 | + }; |
| 118 | + |
| 119 | + await rateLimiter.Configure(options); |
| 120 | + var heldLease = await rateLimiter.AcquireAsync(PermitCount); |
| 121 | + heldLease.IsAcquired.ShouldBeTrue(); |
| 122 | + |
| 123 | + await ForceActivationCollectionAsync(); |
| 124 | + |
| 125 | + var configuration = await rateLimiter.GetConfiguration(); |
| 126 | + configuration.PermitLimit.ShouldBe(PermitLimit); |
| 127 | + configuration.QueueLimit.ShouldBe(QueueLimit); |
| 128 | + |
| 129 | + await AssertCurrentAvailablePermitsAsync(rateLimiter); |
| 130 | + |
| 131 | + await heldLease.DisposeAsync(); |
| 132 | + } |
| 133 | + |
| 134 | + private static async Task AcquireAndDisposeAsync<TGrain, TOptions>(BaseRateLimiterHolder<TGrain, TOptions> rateLimiter) |
| 135 | + where TGrain : IGrainWithStringKey, IRateLimiterGrainWithConfiguration<TOptions> |
| 136 | + where TOptions : class |
| 137 | + { |
| 138 | + await using var acquiredLease = await rateLimiter.AcquireAsync(PermitCount); |
| 139 | + acquiredLease.IsAcquired.ShouldBeTrue(); |
| 140 | + } |
| 141 | + |
| 142 | + private static async Task AssertCurrentAvailablePermitsAsync<TGrain, TOptions>(BaseRateLimiterHolder<TGrain, TOptions> rateLimiter) |
| 143 | + where TGrain : IGrainWithStringKey, IRateLimiterGrainWithConfiguration<TOptions> |
| 144 | + where TOptions : class |
| 145 | + { |
| 146 | + var statistics = await rateLimiter.GetStatisticsAsync(); |
| 147 | + statistics.ShouldNotBeNull(); |
| 148 | + statistics.CurrentAvailablePermits.ShouldBe(ExpectedAvailablePermits); |
| 149 | + } |
| 150 | + |
| 151 | + private async Task ForceActivationCollectionAsync() |
| 152 | + { |
| 153 | + var managementGrain = _testApp.Cluster.Client.GetGrain<IManagementGrain>(ManagementGrainKey); |
| 154 | + await managementGrain.ForceActivationCollection(ActivationCollectionAge); |
| 155 | + } |
| 156 | +} |
0 commit comments