|
1 | 1 | using System; |
2 | 2 | using System.Collections.Concurrent; |
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Threading; |
5 | 6 | using System.Threading.Tasks; |
@@ -133,6 +134,71 @@ public async Task Timed_RateLimited_Processor_Cancels_Unprocessed_Items_Promptly |
133 | 134 | await processor.DisposeAsync(); |
134 | 135 | } |
135 | 136 |
|
| 137 | + [Test, Timeout(10_000)] |
| 138 | + public async Task Timed_Rate_Limit_Allows_Concurrency_Independent_Of_Permit_Count(CancellationToken cancellationToken) |
| 139 | + { |
| 140 | + const int itemCount = 6; |
| 141 | + |
| 142 | + var startedCount = 0; |
| 143 | + var allStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 144 | + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 145 | + |
| 146 | + await using var processor = Enumerable.Range(0, itemCount).ToList() |
| 147 | + .ForEachAsync(async _ => |
| 148 | + { |
| 149 | + if (Interlocked.Increment(ref startedCount) == itemCount) |
| 150 | + { |
| 151 | + allStarted.TrySetResult(); |
| 152 | + } |
| 153 | + |
| 154 | + await release.Task; |
| 155 | + }, cancellationToken) |
| 156 | + .ProcessInParallel( |
| 157 | + permitsPerWindow: 2, |
| 158 | + window: TimeSpan.FromMilliseconds(100), |
| 159 | + maxConcurrency: itemCount); |
| 160 | + |
| 161 | + try |
| 162 | + { |
| 163 | + await allStarted.Task.WaitAsync(TimeSpan.FromSeconds(3), cancellationToken); |
| 164 | + } |
| 165 | + finally |
| 166 | + { |
| 167 | + release.TrySetResult(); |
| 168 | + } |
| 169 | + |
| 170 | + await processor.WaitAsync(); |
| 171 | + |
| 172 | + await Assert.That(startedCount).IsEqualTo(itemCount); |
| 173 | + } |
| 174 | + |
| 175 | + [Test, Retry(3), Timeout(10_000)] |
| 176 | + public async Task Timed_Rate_Limit_Does_Not_Exceed_Permits_At_Replenishment(CancellationToken cancellationToken) |
| 177 | + { |
| 178 | + const int permitsPerWindow = 3; |
| 179 | + var window = TimeSpan.FromMilliseconds(200); |
| 180 | + var startedAt = new ConcurrentBag<TimeSpan>(); |
| 181 | + var stopwatch = Stopwatch.StartNew(); |
| 182 | + |
| 183 | + await using var processor = Enumerable.Range(0, 9).ToList() |
| 184 | + .ForEachAsync(_ => |
| 185 | + { |
| 186 | + startedAt.Add(stopwatch.Elapsed); |
| 187 | + return Task.CompletedTask; |
| 188 | + }, cancellationToken) |
| 189 | + .ProcessInParallel(permitsPerWindow, window, maxConcurrency: 9); |
| 190 | + |
| 191 | + await processor.WaitAsync(); |
| 192 | + |
| 193 | + var orderedStarts = startedAt.OrderBy(x => x).ToArray(); |
| 194 | + |
| 195 | + for (var i = permitsPerWindow; i < orderedStarts.Length; i++) |
| 196 | + { |
| 197 | + await Assert.That(orderedStarts[i] - orderedStarts[i - permitsPerWindow]) |
| 198 | + .IsGreaterThan(window / 2); |
| 199 | + } |
| 200 | + } |
| 201 | + |
136 | 202 | [Test] |
137 | 203 | public async Task Result_Order_Is_Preserved_Regardless_Of_Completion_Order() |
138 | 204 | { |
|
0 commit comments