use of com.linkedin.r2.util.Timeout 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());
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestRampUpRateLimiter method testRampDownImmediately.
@Test(timeOut = TEST_TIMEOUT)
public void testRampDownImmediately() {
ClockedExecutor clockedExecutor = new ClockedExecutor();
RampUpRateLimiter rateLimiter = new RampUpRateLimiterImpl(new SmoothRateLimiter(clockedExecutor, clockedExecutor, clockedExecutor, _queue, Integer.MAX_VALUE, SmoothRateLimiter.BufferOverflowMode.DROP, RATE_LIMITER_NAME_TEST), clockedExecutor);
rateLimiter.setRate(1000d, ONE_SECOND_PERIOD, MINIMUM_BURST);
List<FutureCallback<None>> callbacks = new ArrayList<>();
IntStream.range(0, 1002).forEach(i -> {
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callbacks.add(callback);
});
// -1 because if it passes a full second, the new batch of permits will be issued
clockedExecutor.runFor(ONE_SECOND_PERIOD - 1);
IntStream.range(0, 1000).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(1000, 1002).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
rateLimiter.setRate(1, ONE_SECOND_PERIOD, 1, Integer.MAX_VALUE);
clockedExecutor.runFor(ONE_SECOND_PERIOD);
IntStream.range(1000, 1001).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(1001, 1002).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
clockedExecutor.runFor(ONE_SECOND_PERIOD);
IntStream.range(1001, 1002).forEach(i -> assertTrue(callbacks.get(i).isDone()));
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestConstantQpsRateLimiter method lowNonWholeRate.
@Test(timeOut = TEST_TIMEOUT)
public void lowNonWholeRate() {
for (int i = 0; i < TEST_NUM_CYCLES; i++) {
ClockedExecutor executor = new ClockedExecutor();
ClockedExecutor circularBufferExecutor = new ClockedExecutor();
ConstantQpsRateLimiter rateLimiter = new ConstantQpsRateLimiter(executor, executor, executor, TestEvictingCircularBuffer.getBuffer(circularBufferExecutor));
rateLimiter.setRate(TEST_LOW_FRACTIONAL_QPS, ONE_SECOND, UNLIMITED_BURST);
rateLimiter.setBufferCapacity(1);
TattlingCallback<None> tattler = new TattlingCallback<>(executor);
rateLimiter.submit(tattler);
// run for enough time such that 3 queries are sent
executor.runFor((int) (((ONE_SECOND / TEST_LOW_FRACTIONAL_QPS) * 3) - 1));
Assert.assertTrue(tattler.getInteractCount() == 3);
}
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestSmoothRateLimiter method testSubmitExceedsMaxBufferedButNoReject.
@Test(timeOut = TEST_TIMEOUT)
public void testSubmitExceedsMaxBufferedButNoReject() throws InterruptedException, ExecutionException, TimeoutException {
SmoothRateLimiter rateLimiter = new SmoothRateLimiter(_scheduledExecutorService, _executor, _clock, _queue, 0, SmoothRateLimiter.BufferOverflowMode.SCHEDULE_WITH_WARNING, RATE_LIMITER_NAME_TEST);
rateLimiter.setRate(ONE_PERMIT_PER_PERIOD, ONE_SECOND_PERIOD, UNLIMITED_BURST);
int numberOfTasks = 100;
FutureCallback<None> callback = new FutureCallback<>();
Callback<None> callbacks = new MultiCallback(callback, numberOfTasks);
for (int i = 0; i < numberOfTasks; i++) {
try {
rateLimiter.submit(callbacks);
} catch (RejectedExecutionException e) {
Assert.fail("It should have just run a task and not throw a RejectedExecutionException");
}
}
callback.get(5, TimeUnit.SECONDS);
Assert.assertTrue("The tasks should run", callback.isDone());
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class WarmUpLoadBalancerTest method testDeletingFilesAfterShutdown.
@Test(timeOut = 10000, groups = { "ci-flaky" })
public void testDeletingFilesAfterShutdown() throws InterruptedException, ExecutionException, TimeoutException {
createDefaultServicesIniFiles();
TestLoadBalancer balancer = new TestLoadBalancer();
List<String> allServicesBeforeShutdown = getAllDownstreamServices();
List<String> partialServices = getPartialDownstreams();
DownstreamServicesFetcher returnPartialDownstreams = callback -> callback.onSuccess(partialServices);
LoadBalancer warmUpLoadBalancer = new WarmUpLoadBalancer(balancer, balancer, Executors.newSingleThreadScheduledExecutor(), _tmpdir.getAbsolutePath(), MY_SERVICES_FS, returnPartialDownstreams, WarmUpLoadBalancer.DEFAULT_SEND_REQUESTS_TIMEOUT_SECONDS, WarmUpLoadBalancer.DEFAULT_CONCURRENT_REQUESTS);
FutureCallback<None> callback = new FutureCallback<>();
warmUpLoadBalancer.start(callback);
callback.get(5000, TimeUnit.MILLISECONDS);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
warmUpLoadBalancer.shutdown(() -> shutdownCallback.onSuccess(None.none()));
shutdownCallback.get(5000, TimeUnit.MILLISECONDS);
List<String> allServicesAfterShutdown = getAllDownstreamServices();
Assert.assertTrue(allServicesBeforeShutdown.size() > partialServices.size(), "After shutdown the unused services should have been deleted. Expected lower number of:" + allServicesBeforeShutdown.size() + ", actual " + partialServices.size());
Assert.assertTrue(partialServices.containsAll(allServicesAfterShutdown) && allServicesAfterShutdown.containsAll(partialServices), "There should be just the services that were passed by the partial fetcher");
}
Aggregations