@Test public void testAcquireWithTimeoutTagged() { RateLimiter rateLimiter = createRateLimiter(); Map<String, Integer> tagToCount = new HashMap<>(); tagToCount.put("red", 0); tagToCount.put("green", 0); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", INCREMENT); tagToCredits.put("green", INCREMENT); long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { Map<String, Integer> resultMap = rateLimiter.acquire(tagToCredits, 20, MILLISECONDS); tagToCount.put("red", tagToCount.get("red") + resultMap.get("red")); tagToCount.put("green", tagToCount.get("green") + resultMap.get("green")); } { long rate = tagToCount.get("red") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } { // Note: due to blocking, green is capped at red's QPS long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }
@Test public void testAcquireTagged() { RateLimiter rateLimiter = createRateLimiter(); Map<String, Integer> tagToCount = new HashMap<>(); tagToCount.put("red", 0); tagToCount.put("green", 0); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", INCREMENT); tagToCredits.put("green", INCREMENT); long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { rateLimiter.acquire(tagToCredits); tagToCount.put("red", tagToCount.get("red") + INCREMENT); tagToCount.put("green", tagToCount.get("green") + INCREMENT); } { long rate = tagToCount.get("red") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } { // Note: due to blocking, green is capped at red's QPS long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }