TableRateLimiter { private void throttle(int credits) { long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); if (waitTimeMetric != null) { waitTimeMetric.update(System.nanoTime() - startNs); } } TableRateLimiter(String tableId, RateLimiter rateLimiter, CreditFunction<K, V> creditFn, String tag); void setTimerMetric(Timer timer); void throttle(K key, Object ... args); void throttle(K key, V value, Object ... args); void throttle(int opId, Object ... args); void throttle(Collection<K> keys, Object ... args); void throttleRecords(Collection<Entry<K, V>> records, Object ... args);  }
@Test public void testThrottle() { TableRateLimiter<String, String> rateLimitHelper = getThrottler(); Timer timer = mock(Timer.class); rateLimitHelper.setTimerMetric(timer); int times = 0; rateLimitHelper.throttle("foo"); verify(rateLimitHelper.rateLimiter, times(++times)).acquire(anyMapOf(String.class, Integer.class)); verify(timer, times(times)).update(anyLong()); rateLimitHelper.throttle("foo", "bar"); verify(rateLimitHelper.rateLimiter, times(++times)).acquire(anyMapOf(String.class, Integer.class)); verify(timer, times(times)).update(anyLong()); rateLimitHelper.throttle(Arrays.asList("foo", "bar")); verify(rateLimitHelper.rateLimiter, times(++times)).acquire(anyMapOf(String.class, Integer.class)); verify(timer, times(times)).update(anyLong()); rateLimitHelper.throttle(1, 2); verify(rateLimitHelper.rateLimiter, times(++times)).acquire(anyMapOf(String.class, Integer.class)); verify(timer, times(times)).update(anyLong()); }
@Test public void testThrottleUnknownTag() { TableRateLimiter<String, String> rateLimitHelper = getThrottler("unknown_tag"); rateLimitHelper.throttle("foo"); verify(rateLimitHelper.rateLimiter, times(0)).acquire(anyInt()); verify(rateLimitHelper.rateLimiter, times(1)).acquire(anyMapOf(String.class, Integer.class)); }