EmbeddedTaggedRateLimiter implements RateLimiter { @Override public void acquire(Map<String, Integer> tagToCreditsMap) { ensureTagsAreValid(tagToCreditsMap); tagToCreditsMap.forEach((tag, numberOfCredits) -> tagToRateLimiterMap.get(tag).acquire(numberOfCredits)); } EmbeddedTaggedRateLimiter(int creditsPerSecond);  EmbeddedTaggedRateLimiter(Map<String, Integer> tagToCreditsPerSecondMap); @Override void acquire(Map<String, Integer> tagToCreditsMap); @Override Map<String, Integer> acquire(Map<String, Integer> tagToCreditsMap, long timeout, TimeUnit unit); @Override Set<String> getSupportedTags(); @Override void acquire(int numberOfCredits); @Override int acquire(int numberOfCredit, long timeout, TimeUnit unit); @Override void init(Context context);  }
@Test @Ignore("Flaky Test: Test fails in travis.") public void testAcquire() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(TARGET_RATE); initRateLimiter(rateLimiter); int count = 0; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { rateLimiter.acquire(INCREMENT); count += INCREMENT; } long rate = count * 1000 / TEST_INTERVAL; verifyRate(rate); }
@Test @Ignore("Flaky Test.") public void testAcquireWithTimeout() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(TARGET_RATE); initRateLimiter(rateLimiter); boolean hasSeenZeros = false; int count = 0; int callCount = 0; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { ++callCount; int availableCredits = rateLimiter.acquire(INCREMENT, 20, MILLISECONDS); if (availableCredits <= 0) { hasSeenZeros = true; } else { count += INCREMENT; } } long rate = count * 1000 / TEST_INTERVAL; verifyRate(rate); junit.framework.Assert.assertTrue(Math.abs(callCount - TARGET_RATE_PER_TASK * TEST_INTERVAL / 1000 / INCREMENT) <= 2); junit.framework.Assert.assertFalse(hasSeenZeros); }
@Test(expected = IllegalStateException.class) public void testFailsWhenUninitialized() { new EmbeddedTaggedRateLimiter(100).acquire(1); }
@Test(expected = IllegalArgumentException.class) public void testFailsWhenUsingTags() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(10); initRateLimiter(rateLimiter); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", 1); tagToCredits.put("green", 1); rateLimiter.acquire(tagToCredits); }
@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); } { long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }
@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); } { long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }
@Test(expected = IllegalStateException.class) public void testFailsWhenUninitializedTagged() { Map<String, Integer> tagToTargetRateMap = new HashMap<>(); tagToTargetRateMap.put("red", 1000); tagToTargetRateMap.put("green", 2000); new EmbeddedTaggedRateLimiter(tagToTargetRateMap).acquire(tagToTargetRateMap); }
@Test(expected = IllegalArgumentException.class) public void testFailsWhenNotUsingTags() { Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", 1); tagToCredits.put("green", 1); RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(tagToCredits); initRateLimiter(rateLimiter); rateLimiter.acquire(1); }