Created
October 29, 2022 22:49
-
-
Save lobster1234/af1e2a2dd2755b5ce1c9958a3a8ec8d4 to your computer and use it in GitHub Desktop.
Test for the token bucket rate limiter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.lobster1234.misc; | |
import junit.framework.TestCase; | |
import java.util.concurrent.TimeUnit; | |
public class TokenBucketRateLimiterTest extends TestCase { | |
public void testTokenBucketRateLimiter() { | |
try { | |
TokenBucketRateLimiter limiter = new TokenBucketRateLimiter(10, TimeUnit.SECONDS); | |
for (int i = 0; i < 11; i++) { | |
limiter.execute(new RequestPayLoad("Test Request"), payLoad -> System.out.println("Success " + payLoad.getPayload()), | |
payLoad -> System.out.println("Failure " + payLoad.getPayload())); | |
} | |
Thread.sleep(5000); //sleep for 5 seconds | |
//try again | |
for (int i = 0; i < 11; i++) { | |
limiter.execute(new RequestPayLoad("Test Request"), payLoad -> System.out.println("Success " + payLoad.getPayload()), | |
payLoad -> System.out.println("Failure " + payLoad.getPayload())); | |
} | |
}catch(Exception e){ | |
e.printStackTrace(); | |
fail(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment