@@ -109,7 +109,9 @@ public void init() throws IOException {
109109 RateLimitUtil .rateLimitMap .clear ();
110110 RateLimitUtil .rateLimits .clear ();
111111 }
112-
112+ // Reset cache for each test
113+ cache .rateLimitCache .clear ();
114+
113115 // Reset to default auth user
114116 authUser .setRateLimitTier (1 );
115117 authUser .setSuperuser (false );
@@ -163,15 +165,23 @@ public void testAuthenticatedUserGettingRateLimited() throws InterruptedExceptio
163165 authUser .setRateLimitTier (2 ); // 120 cals per hour - 1 added token every 30 seconds
164166 boolean rateLimited = false ;
165167 int cnt ;
168+ long startTime = System .currentTimeMillis ();
166169 for (cnt = 0 ; cnt <200 ; cnt ++) {
167170 rateLimited = !cache .checkRate (authUser , action );
168171 if (rateLimited ) {
169172 break ;
170173 }
171174 }
172- assertTrue (rateLimited );
173- assertEquals (120 , cnt );
175+ long endTime = System .currentTimeMillis ();
176+ System .out .println ("Test loop took " + (endTime - startTime ) + " ms" );
177+ //Add a few seconds to account for time outside loop
178+ long durationMinutes = (6000 + endTime - startTime ) / 60000L ;
179+ // 120 calls/hr = 2 calls/min. Add any tokens that may have been added during the test run
180+ long expectedMax = 120 + (durationMinutes * 2 );
174181
182+ assertTrue (rateLimited );
183+ assertTrue (cnt >= 120 && cnt <= expectedMax , "cnt was " + cnt + ", expected between 120 and " + expectedMax );
184+
175185 for (cnt = 0 ; cnt <60 ; cnt ++) {
176186 Thread .sleep (1000 );// Wait for bucket to be replenished (check each second for 1 minute max)
177187 rateLimited = !cache .checkRate (authUser , action );
0 commit comments