use of com.linkedin.r2.transport.http.client.AsyncRateLimiter in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testSetRateInstantaneous.
@Test(timeOut = TEST_TIMEOUT)
public void testSetRateInstantaneous() {
ClockedExecutor clockedExecutor = new ClockedExecutor();
AsyncRateLimiter rateLimiter = getRateLimiter(clockedExecutor, clockedExecutor, clockedExecutor);
List<FutureCallback<None>> callbacks = new ArrayList<>();
IntStream.range(0, 10).forEachOrdered(i -> {
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callbacks.add(callback);
});
// the last set should take immediately effect, and therefore at ms 0, we should have 3 permits available
rateLimiter.setRate(0d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(1d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(3d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
// trigger task to run them until current time
clockedExecutor.runFor(0);
// We have one permit to begin with so the first task should run immediate and left with four pending
IntStream.range(0, 3).forEach(i -> assertTrue(callbacks.get(i).isDone(), i + " should have been executed " + callbacks.get(i)));
IntStream.range(3, 10).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
IntStream.range(3, 6).forEach(i -> assertTrue(callbacks.get(i).isDone(), i + " should have been executed " + callbacks.get(i)));
IntStream.range(6, 10).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
}
use of com.linkedin.r2.transport.http.client.AsyncRateLimiter in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testSetRate.
@Test(timeOut = TEST_TIMEOUT)
public void testSetRate() throws Exception {
ClockedExecutor clockedExecutor = new ClockedExecutor();
AsyncRateLimiter rateLimiter = getRateLimiter(clockedExecutor, clockedExecutor, clockedExecutor);
rateLimiter.setRate(ONE_PERMIT_PER_PERIOD, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
List<FutureCallback<None>> callbacks = new ArrayList<>();
IntStream.range(0, 5).forEach(i -> {
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callbacks.add(callback);
});
// trigger task to run them until current time
clockedExecutor.runFor(0);
// We have one permit to begin with so the first task should run immediate and left with four pending
callbacks.get(0).get();
IntStream.range(0, 1).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(1, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
// We set the permit rate to two per period and increment the clock by one millisecond. We expect two
// more callbacks to be invoked at the next permit issuance
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(0);
callbacks.get(1).get();
callbacks.get(2).get();
IntStream.range(0, 3).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(3, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
// We set the permit rate back to one per period and increment the clock by one millisecond. We expect
// only one more callbacks to be invoked at the next permit issuance
rateLimiter.setRate(1d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
callbacks.get(3).get();
IntStream.range(0, 4).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(4, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
// We set the permit rate to two per period again and increment the clock by one millisecond. We expect
// only one more callbacks to be invoked at the next permit issuance because only one is left
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
callbacks.get(4).get();
IntStream.range(0, 5).forEach(i -> assertTrue(callbacks.get(i).isDone()));
}
use of com.linkedin.r2.transport.http.client.AsyncRateLimiter in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testCancelAllTwice.
@Test(timeOut = TEST_TIMEOUT)
public void testCancelAllTwice() {
AsyncRateLimiter rateLimiter = getRateLimiter(_scheduledExecutorService, _executor, _clock);
rateLimiter.setRate(ONE_PERMIT_PER_PERIOD, ONE_SECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.cancelAll(new Throwable());
rateLimiter.cancelAll(new Throwable());
}
Aggregations