forked from BenFradet/RiotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRateLimiterTest.cs
More file actions
296 lines (274 loc) · 10.4 KB
/
RateLimiterTest.cs
File metadata and controls
296 lines (274 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RiotSharp;
namespace RiotSharpTest
{
[TestClass]
public class RateLimiterTest
{
public static readonly TimeSpan TenSeconds = TimeSpan.FromSeconds(10);
/// <summary>
/// Slightly more than the percentage extra delay tests tend to take.
/// </summary>
public const double ErrorFactor = 1.003;
public static readonly TimeSpan ErrorDelay = TimeSpan.FromMilliseconds(20);
public const int Limit = 10;
internal RateLimiter RateLimiter;
public Stopwatch Stopwatch = new Stopwatch();
[TestInitialize]
public void Initialize()
{
RateLimiter = new RateLimiter(Limit, int.MaxValue);
Stopwatch.Restart();
}
/// <summary>
/// Sends a single request, expected to unblock immediately.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter")]
public void SingleRequest()
{
RateLimiter.HandleRateLimit();
AssertDelayed(TimeSpan.Zero);
}
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
public async Task SingleRequestAsync()
{
await RateLimiter.HandleRateLimitAsync();
AssertDelayed(TimeSpan.Zero);
}
/// <summary>
/// Basic check of the SetRetryAfter.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter")]
public void RetryAfterBasic()
{
RateLimiter.HandleRateLimit();
AssertDelayed(TimeSpan.Zero);
var delay = TimeSpan.FromSeconds(5);
RateLimiter.SetRetryAfter(delay);
RateLimiter.HandleRateLimit();
AssertDelayed(delay);
}
/// <summary>
/// Sends 30 requests, expects each block of 10 requests to unblock after 10 second intervals.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter")]
[Timeout(1000 * 10 * 3)]
public void ManyRequests()
{
for (var i = 0; i < Limit * 3; i++)
{
RateLimiter.HandleRateLimit();
AssertDelayed(TimeSpan.FromTicks(i / Limit * TenSeconds.Ticks));
}
}
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 3)]
public async Task ManyRequestsAsync()
{
var tasks = Enumerable.Range(0, Limit*3).Select(i => RateLimiter.HandleRateLimitAsync()
.ContinueWith(task => {
AssertDelayed(TimeSpan.FromTicks(i / Limit * TenSeconds.Ticks), i);
})).ToList();
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
/// <summary>
/// Sends 10 requests, waits 10 seconds, sends another 10 requests. Expects requests to unblock immediately.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter")]
[Timeout(1000 * 10 * 2)]
public void DelayedRequests()
{
for (var i = 0; i < Limit; i++)
{
RateLimiter.HandleRateLimit();
AssertDelayed(TimeSpan.Zero);
}
var delay = TenSeconds;
Task.Delay(delay).Wait();
for (var i = 0; i < Limit; i++)
{
RateLimiter.HandleRateLimit();
AssertDelayed(delay);
}
}
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 2)]
public async Task DelayedRequestsAsync()
{
var expectedDelayed = TenSeconds;
// delayed tasks first, just for fun
var tasks = Enumerable.Range(Limit * 2, Limit).Select(i => Task.Delay(expectedDelayed)
.ContinueWith(task => RateLimiter.HandleRateLimitAsync())
.ContinueWith(task =>
{
AssertDelayed(expectedDelayed, i);
})).ToList();
// immediate tasks`
var expected = TimeSpan.Zero;
tasks.AddRange(Enumerable.Range(0, Limit).Select(i => RateLimiter.HandleRateLimitAsync()
.ContinueWith(task =>
{
AssertDelayed(expected, i);
})));
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
/// <summary>
/// Sends 10 requests, waits 20 seconds, sends another 10 requests. Expects requests to unblock immediately.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter")]
[Timeout(1000 * 10 * 3)]
public void AlternatingRequests()
{
for (var i = 0; i < Limit; i++)
{
RateLimiter.HandleRateLimit();
AssertDelayed(TimeSpan.Zero);
}
var delay = TimeSpan.FromTicks(TenSeconds.Ticks * 2);
Task.Delay(delay).Wait();
for (var i = 0; i < Limit; i++)
{
RateLimiter.HandleRateLimit();
AssertDelayed(delay);
}
}
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 3)]
public async Task AlternatingRequestsAsync()
{
var expectedDelayed = TimeSpan.FromTicks(TenSeconds.Ticks * 2);
// delayed tasks first, just for fun
var tasks = Enumerable.Range(Limit*2, Limit).Select(i => Task.Delay(expectedDelayed)
.ContinueWith(task => RateLimiter.HandleRateLimitAsync())
.ContinueWith(task =>
{
AssertDelayed(expectedDelayed, i);
})).ToList();
// immediate tasks
var expected = TimeSpan.Zero;
tasks.AddRange(Enumerable.Range(0, Limit).Select(i => RateLimiter.HandleRateLimitAsync()
.ContinueWith(task =>
{
AssertDelayed(expected, i);
})));
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
/// <summary>
/// Sends 5 requests. Waits 5 seconds. Sends 10 More requests.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 2)]
public async Task MultipleManyRequests15Async()
{
// first 5 requests
var tasks = Enumerable.Range(0, 5).Select(i => RateLimiter.HandleRateLimitAsync()
.ContinueWith(t => AssertDelayed(TimeSpan.Zero, i))).ToList();
var fiveSeconds = TimeSpan.FromSeconds(5);
await Task.Delay(fiveSeconds);
tasks.AddRange(Enumerable.Range(5, 10).Select(i => RateLimiter.HandleRateLimitAsync()
.ContinueWith(t => AssertDelayed(i < 10 ? fiveSeconds : TenSeconds, i))));
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
/// <summary>
/// Same as above, but does the delay within the task.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 2)]
public async Task MultipleManyRequests15InlineAsync()
{
// order is more random, so we keep track of each group instead
int[] counts = {0, 0, 0};
// first 5 requests
var tasks = Enumerable.Range(0, 5).Select(async i => {
await RateLimiter.HandleRateLimitAsync();
counts[Stopwatch.Elapsed.Seconds / 10]++;
}).ToList();
var fiveSeconds = TimeSpan.FromSeconds(5);
tasks.AddRange(Enumerable.Range(5, 10).Select(async i =>
{
await Task.Delay(fiveSeconds);
await RateLimiter.HandleRateLimitAsync();
counts[Stopwatch.Elapsed.Seconds / 10]++;
}));
await Task.WhenAll(tasks);
Assert.AreEqual(10, counts[0]);
Assert.AreEqual(5, counts[1]);
Assert.AreEqual(0, counts[2]);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
/// <summary>
/// Sends 15 requests. Waits 10 seconds. Expects 10 requests each to be delayed by 0, 10, and 20 seconds.
/// </summary>
[TestMethod]
[TestCategory("RateLimiter"), TestCategory("Async")]
[Timeout(1000 * 10 * 3)]
public async Task MultipleManyRequests30InlineAsync()
{
// order is more random, so we keep track of each group instead
int[] counts = { 0, 0, 0 };
// last 15 tasks first, just for fun
var tasks = Enumerable.Range(15, 15).Select(async i =>
{
await Task.Delay(TenSeconds);
await RateLimiter.HandleRateLimitAsync();
counts[Stopwatch.Elapsed.Seconds / 10]++;
}).ToList();
// first 15 tasks
tasks.AddRange(Enumerable.Range(0, 15).Select(async i =>
{
await RateLimiter.HandleRateLimitAsync();
counts[Stopwatch.Elapsed.Seconds / 10]++;
}));
await Task.WhenAll(tasks);
Assert.AreEqual(10, counts[0]);
Assert.AreEqual(10, counts[1]);
Assert.AreEqual(10, counts[2]);
foreach (var task in tasks)
{
Assert.IsNull(task.Exception);
}
}
private void AssertDelayed(TimeSpan expected, int i=0)
{
var actual = Stopwatch.Elapsed;
Assert.IsTrue(expected < actual,
string.Format("{0} too soon. Expected: {1}. Actual: {2}.", i, expected, actual));
Assert.IsTrue(actual < TimeSpan.FromTicks((int) (expected.Ticks * ErrorFactor + ErrorDelay.Ticks)),
string.Format("{0} too late. Expected: {1}. Actual: {2}.", i, expected, actual));
}
}
}