@@ -93,34 +93,31 @@ public void testRateLimitImpl() {
93
93
RateLimiterImpl limiterImpl =
94
94
new RateLimiterImpl (TWO_TOKENS_PER_MINUTE , 2 , mClock , mockConfigResolver , NETWORK , false );
95
95
PerfMetric metric = PerfMetric .getDefaultInstance ();
96
- // clock is 15 seconds, token count is 1.
97
- currentTime = currentTime .plusSeconds (15 );
98
- assertThat (limiterImpl .check (metric )).isTrue ();
99
- // clock is 30 seconds, count is 1.
100
- currentTime = currentTime .plusSeconds (15 );
96
+ // clock is 30 seconds, count is 2, afterwards is 1
97
+ currentTime = currentTime .plusSeconds (30 );
101
98
assertThat (limiterImpl .check (metric )).isTrue ();
102
- // clock is 45 seconds, count is 0.
99
+ // clock is 45 seconds, count is 1.5, afterwards is 0.5
103
100
currentTime = currentTime .plusSeconds (15 );
104
101
assertThat (limiterImpl .check (metric )).isTrue ();
105
- // clock is 60 seconds, count is 1
102
+ // clock is 60 seconds, count is 1, afterwards is 0
106
103
currentTime = currentTime .plusSeconds (15 );
107
104
assertThat (limiterImpl .check (metric )).isTrue ();
108
- // clock is 75 seconds, count is 0,
105
+ // clock is 75 seconds, count is 0.5, afterwards is 0.5
109
106
currentTime = currentTime .plusSeconds (15 );
110
107
assertThat (limiterImpl .check (metric )).isFalse ();
111
- // clock is 90 seconds, count is 1
108
+ // clock is 90 seconds, count is 1, afterwards is 0
112
109
currentTime = currentTime .plusSeconds (15 );
113
110
assertThat (limiterImpl .check (metric )).isTrue ();
114
- // clock is 105 seconds, count is 0
111
+ // clock is 105 seconds, count is 0.5, afterwards is 0.5
115
112
currentTime = currentTime .plusSeconds (15 );
116
113
assertThat (limiterImpl .check (metric )).isFalse ();
117
- // clock is 120 seconds, count is 1,
114
+ // clock is 120 seconds, count is 1, afterwards is 1
118
115
currentTime = currentTime .plusSeconds (15 );
119
116
assertThat (limiterImpl .check (metric )).isTrue ();
120
- // clock is 135 seconds, count is 0,
117
+ // clock is 135 seconds, count is 0.5, afterwards is 0.5
121
118
currentTime = currentTime .plusSeconds (15 );
122
119
assertThat (limiterImpl .check (metric )).isFalse ();
123
- // clock is 150 seconds, count is 1,
120
+ // clock is 150 seconds, count is 1, afterwards is 1
124
121
currentTime = currentTime .plusSeconds (15 );
125
122
assertThat (limiterImpl .check (metric )).isTrue ();
126
123
}
@@ -136,32 +133,32 @@ public void testRateLimiterImplWithIrregularTimeIntervals() {
136
133
when (mockConfigResolver .getNetworkRequestSamplingRate ()).thenReturn (1.0f );
137
134
138
135
// allow 2 logs every minute. token bucket capacity is 2.
139
- // clock is 0 , token count is 2.
136
+ // clock is 0s , token count is 2.0 .
140
137
RateLimiterImpl limiterImpl =
141
138
new RateLimiterImpl (TWO_TOKENS_PER_MINUTE , 2 , mClock , mockConfigResolver , NETWORK , false );
142
139
PerfMetric metric = PerfMetric .getDefaultInstance ();
143
- // clock is 20 seconds , count before check is 2, 0 new tokens added, count after check is 1
140
+ // clock is 20s , count before check is 2.00 , 0.00 new tokens added, count after check is 1.00
144
141
currentTime = currentTime .plusSeconds (20 );
145
142
assertThat (limiterImpl .check (metric )).isTrue ();
146
- // clock is 40 seconds , count before check is 1, 1 new tokens added, count after check is 1
147
- currentTime = currentTime .plusSeconds (20 );
143
+ // clock is 60s , count before check is 1.00 , 1.00 new tokens added, count after check is 1.00
144
+ currentTime = currentTime .plusSeconds (40 );
148
145
assertThat (limiterImpl .check (metric )).isTrue ();
149
- // clock is 59 seconds , count before check is 1, 0 new tokens added, count after check is 0
150
- currentTime = currentTime .plusSeconds (19 );
146
+ // clock is 89s , count before check is 1.00 , 0.96 new tokens added, count after check is 0.96
147
+ currentTime = currentTime .plusSeconds (29 );
151
148
assertThat (limiterImpl .check (metric )).isTrue ();
152
- // clock is 60 seconds , count before check is 0, 1 new tokens added, count after check is 0
153
- currentTime = currentTime .plusSeconds (1 );
149
+ // clock is 95s , count before check is 0.96, 0.20 new tokens added, count after check is 0.16
150
+ currentTime = currentTime .plusSeconds (6 );
154
151
assertThat (limiterImpl .check (metric )).isTrue ();
155
- // clock is 80 seconds , count before check is 0, 0 new tokens added, count after check is 0
156
- currentTime = currentTime .plusSeconds (20 );
152
+ // clock is 110s , count before check is 0.16 , 0.50 new tokens added, count after check is 0.66
153
+ currentTime = currentTime .plusSeconds (15 );
157
154
assertThat (limiterImpl .check (metric )).isFalse ();
158
- // clock is 130 seconds , count before check is 0, 2 new tokens added, count after check is 1
155
+ // clock is 160s , count before check is 0.66, 1.34 new tokens added, count after check is 1.00
159
156
currentTime = currentTime .plusSeconds (50 );
160
157
assertThat (limiterImpl .check (metric )).isTrue ();
161
- // clock is 131 seconds , count before check is 1, 0 new tokens added, count after check is 0
158
+ // clock is 161s , count before check is 1.00 , 0.03 new tokens added, count after check is 0.03
162
159
currentTime = currentTime .plusSeconds (1 );
163
160
assertThat (limiterImpl .check (metric )).isTrue ();
164
- // clock is 132 seconds , count before check is 0, 0 new tokens added, count after check is 0
161
+ // clock is 162s , count before check is 0.03 , 0.03 new tokens added, count after check is 0.06
165
162
currentTime = currentTime .plusSeconds (1 );
166
163
assertThat (limiterImpl .check (metric )).isFalse ();
167
164
}
@@ -182,29 +179,29 @@ public void testRateLimiterImplWithIrregularTimeIntervals() {
182
179
new RateLimiterImpl (TWO_TOKENS_PER_MINUTE , 2 , mClock , mockConfigResolver , NETWORK , false );
183
180
PerfMetric metric = PerfMetric .getDefaultInstance ();
184
181
185
- // clock is 20 seconds , count before check is 2, 0 new tokens added, count after check is 1
186
- currentTime = currentTime .plusSeconds (20 );
182
+ // clock is 0 , count before check is 2, 0 new tokens added, count after check is 1
183
+ currentTime = currentTime .plusSeconds (0 );
187
184
assertThat (limiterImpl .check (metric )).isTrue ();
188
- // clock is 40 seconds , count before check is 1, 1 new tokens added, count after check is 1
189
- currentTime = currentTime .plusSeconds (20 );
185
+ // clock is 30 , count before check is 1, 1 new tokens added, count after check is 1
186
+ currentTime = currentTime .plusSeconds (30 );
190
187
assertThat (limiterImpl .check (metric )).isTrue ();
191
- // clock is 59 seconds , count before check is 1, 0 new tokens added, count after check is 0
192
- currentTime = currentTime .plusSeconds (19 );
188
+ // clock is 59, count before check is 1, 0.96 new tokens added, count after check is 0.96
189
+ currentTime = currentTime .plusSeconds (29 );
193
190
assertThat (limiterImpl .check (metric )).isTrue ();
194
- // clock is 60 seconds , count before check is 0, 1 new tokens added, count after check is 0
191
+ // clock is 60, count before check is 0.96, 0.04 new tokens added, count after check is 0
195
192
currentTime = currentTime .plusSeconds (1 );
196
193
assertThat (limiterImpl .check (metric )).isTrue ();
197
194
198
- // clock is 660 seconds , count before check is 0, 2 new tokens added, count after check is 1
195
+ // clock is 660, count before check is 0, 2 new tokens added, count after check is 1
199
196
currentTime = currentTime .plusSeconds (600 );
200
197
assertThat (limiterImpl .check (metric )).isTrue ();
201
- // clock is 661 seconds , count before check is 1, 0 new tokens added, count after check is 0
198
+ // clock is 661, count before check is 1, 0.03 new tokens added, count after check is 0.03
202
199
currentTime = currentTime .plusSeconds (1 );
203
200
assertThat (limiterImpl .check (metric )).isTrue ();
204
- // clock is 662 seconds , count before check is 0, 0 new tokens added, count after check is 0
201
+ // clock is 662, count before check is 0.03 , 0.03 new tokens added, count after check is 0.06
205
202
currentTime = currentTime .plusSeconds (1 );
206
203
assertThat (limiterImpl .check (metric )).isFalse ();
207
- // clock is 663 seconds , count before check is 0, 0 new tokens added, count after check is 0
204
+ // clock is 663, count before check is 0.06 , 0.03 new tokens added, count after check is 0.10
208
205
currentTime = currentTime .plusSeconds (1 );
209
206
assertThat (limiterImpl .check (metric )).isFalse ();
210
207
}
@@ -330,46 +327,46 @@ public void testRateLimit() {
330
327
PerfMetric .newBuilder ()
331
328
.setNetworkRequestMetric (NetworkRequestMetric .getDefaultInstance ())
332
329
.build ();
333
- // clock is 15 seconds, token count is 1.
334
- currentTime = currentTime .plusSeconds (15 );
335
- assertThat (limiter .isEventRateLimited (trace )).isFalse ();
336
- assertThat (limiter .isEventRateLimited (network )).isFalse ();
337
- // clock is 30 seconds, count is 0.
330
+ // clock is 15 seconds, token count afterwards is 1.
338
331
currentTime = currentTime .plusSeconds (15 );
339
332
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
340
333
assertThat (limiter .isEventRateLimited (network )).isFalse ();
341
- // clock is 45 seconds, count is 0.
334
+ // clock is 30 seconds, count afterwards is 0.5 .
342
335
currentTime = currentTime .plusSeconds (15 );
343
336
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
344
337
assertThat (limiter .isEventRateLimited (network )).isFalse ();
345
- // clock is 60 seconds, count is 0
338
+ // clock is 45 seconds, count afterwards is 0
346
339
currentTime = currentTime .plusSeconds (15 );
347
340
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
348
341
assertThat (limiter .isEventRateLimited (network )).isFalse ();
349
- // clock is 75 seconds, count is 0,
342
+ // clock is 60 seconds, count afterwards is 0.5
350
343
currentTime = currentTime .plusSeconds (15 );
351
344
assertThat (limiter .isEventRateLimited (trace )).isTrue ();
352
345
assertThat (limiter .isEventRateLimited (network )).isTrue ();
353
- // clock is 90 seconds, count is 0
346
+ // clock is 75 seconds, count afterwards is 0
354
347
currentTime = currentTime .plusSeconds (15 );
355
348
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
356
349
assertThat (limiter .isEventRateLimited (network )).isFalse ();
357
- // clock is 105 seconds, count is 0
350
+ // clock is 90 seconds, count afterwards is 0.5
358
351
currentTime = currentTime .plusSeconds (15 );
359
352
assertThat (limiter .isEventRateLimited (trace )).isTrue ();
360
353
assertThat (limiter .isEventRateLimited (network )).isTrue ();
361
- // clock is 120 seconds, count is 0,
354
+ // clock is 105 seconds, count afterwards is 0
362
355
currentTime = currentTime .plusSeconds (15 );
363
356
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
364
357
assertThat (limiter .isEventRateLimited (network )).isFalse ();
365
- // clock is 135 seconds, count is 0,
358
+ // clock is 120 seconds, count afterwards is 0.5
366
359
currentTime = currentTime .plusSeconds (15 );
367
360
assertThat (limiter .isEventRateLimited (trace )).isTrue ();
368
361
assertThat (limiter .isEventRateLimited (network )).isTrue ();
369
- // clock is 150 seconds, count is 0,
362
+ // clock is 135 seconds, count afterwards is 0
370
363
currentTime = currentTime .plusSeconds (15 );
371
364
assertThat (limiter .isEventRateLimited (trace )).isFalse ();
372
365
assertThat (limiter .isEventRateLimited (network )).isFalse ();
366
+ // clock is 150 seconds, count afterwards is 0.5
367
+ currentTime = currentTime .plusSeconds (15 );
368
+ assertThat (limiter .isEventRateLimited (trace )).isTrue ();
369
+ assertThat (limiter .isEventRateLimited (network )).isTrue ();
373
370
}
374
371
375
372
@ Test
@@ -722,16 +719,19 @@ public void testChangeRate() {
722
719
RateLimiterImpl limiterImpl =
723
720
new RateLimiterImpl (TWO_TOKENS_PER_MINUTE , 2 , mClock , mockConfigResolver , TRACE , false );
724
721
PerfMetric metric = PerfMetric .getDefaultInstance ();
725
- // clock is 15 seconds, token count is 1.
726
- currentTime = currentTime .plusSeconds (15 );
722
+ // clock is 0 seconds, token count is 2, afterwards is 1
723
+ currentTime = currentTime .plusSeconds (0 );
727
724
assertThat (limiterImpl .check (metric )).isTrue ();
728
- // clock is 30 seconds, count is 0.
725
+ // clock is 15 seconds, count is 1.5, afterwards is 0.5
729
726
currentTime = currentTime .plusSeconds (15 );
730
727
assertThat (limiterImpl .check (metric )).isTrue ();
731
- // clock is 45 seconds, count is 0.
728
+ // clock is 30 seconds, count is 1, afterwards is 0
732
729
currentTime = currentTime .plusSeconds (15 );
733
730
assertThat (limiterImpl .check (metric )).isTrue ();
734
- // clock is 60 seconds, count is 0
731
+ // clock is 45 seconds, count is 0.5, afterwards is 0.5
732
+ currentTime = currentTime .plusSeconds (15 );
733
+ assertThat (limiterImpl .check (metric )).isFalse ();
734
+ // clock is 60 seconds, count is 1, afterwards is 0
735
735
currentTime = currentTime .plusSeconds (15 );
736
736
assertThat (limiterImpl .check (metric )).isTrue ();
737
737
0 commit comments