/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(Runnable)}. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed */ public void execute(Object taskKey, Runnable task) { execute(1, taskKey, task); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(Runnable)}. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed */ public void execute(Object taskKey, Runnable task) { execute(1, taskKey, task); }
@Test public void executeWithPermitsFail() { try { limiter.execute(-1, new Object(), DoNothingRunnable.instance()); fail("Exception should have thrown"); } catch (IllegalArgumentException e) { // expected } try { limiter.execute(1, new Object(), null); fail("Exception should have thrown"); } catch (IllegalArgumentException e) { // expected } }
@Test public void executeWithPermitsReturnedDelayTest() { long firstDelay = limiter.execute(10, "foo", DoNothingRunnable.instance()); assertEquals(0, firstDelay); long secondDelay = limiter.execute(1, "foo", DoNothingRunnable.instance()); // should be incremented from first delay assertTrue(secondDelay > 8000); }
@Test public void getCurrentMinimumDelayTest() { Object key = new Object(); assertEquals(0, limiter.getMinimumDelay(key)); limiter.execute(10, key, DoNothingRunnable.instance()); int delay = limiter.getMinimumDelay(key); assertEquals(10000, delay, 1000); limiter.execute(10, key, DoNothingRunnable.instance()); delay = limiter.getMinimumDelay(key); assertEquals(20000, delay, 1000); }
@Test public void verifyCleanupTaskTest() { double permits = .1; Object key = new Object(); limiter.execute(permits, key, new TestRunnable()); assertEquals(2, scheduler.advance(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT)); assertEquals(1, limiter.getTrackedKeyCount()); assertFalse(limiter.currentLimiters.isEmpty()); if (TEST_PROFILE == TestLoad.Stress) { // too slow for normal tests right now TestUtils.sleep((long)(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT + (1000 * permits))); TestUtils.blockTillClockAdvances(); assertEquals(1, scheduler.advance(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT)); assertTrue(limiter.currentLimiters.isEmpty()); assertEquals(0, limiter.getTrackedKeyCount()); } }
@Test public void getFutureTillDelayTest() { Object key = new Object(); // verify that an empty limiter returns a finished future ListenableFuture<?> f = limiter.getFutureTillDelay(key, 0); assertTrue(f.isDone()); // verify it works if the limiter has waiting tasks limiter.execute(key, DoNothingRunnable.instance()); f = limiter.getFutureTillDelay(key, 0); assertFalse(f.isDone()); scheduler.advance(1000); assertTrue(f.isDone()); }