Search in sources :

Example 76 with Timeout

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());
}
Also used : AsyncRateLimiter(com.linkedin.r2.transport.http.client.AsyncRateLimiter) Test(org.testng.annotations.Test)

Example 77 with Timeout

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()));
}
Also used : SmoothRateLimiter(com.linkedin.r2.transport.http.client.SmoothRateLimiter) ArrayList(java.util.ArrayList) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 78 with Timeout

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);
    }
}
Also used : ConstantQpsRateLimiter(com.linkedin.r2.transport.http.client.ConstantQpsRateLimiter) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Example 79 with Timeout

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());
}
Also used : SmoothRateLimiter(com.linkedin.r2.transport.http.client.SmoothRateLimiter) MultiCallback(com.linkedin.common.callback.MultiCallback) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.testng.annotations.Test)

Example 80 with Timeout

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");
}
Also used : Arrays(java.util.Arrays) FutureCallback(com.linkedin.common.callback.FutureCallback) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) URISyntaxException(java.net.URISyntaxException) BeforeMethod(org.testng.annotations.BeforeMethod) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) File(java.io.File) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) Assert(org.testng.Assert) None(com.linkedin.common.util.None) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) DownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.DownstreamServicesFetcher) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)78 RequestContext (com.linkedin.r2.message.RequestContext)46 CountDownLatch (java.util.concurrent.CountDownLatch)40 TimeoutException (java.util.concurrent.TimeoutException)40 None (com.linkedin.common.util.None)33 FutureCallback (com.linkedin.common.callback.FutureCallback)32 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)26 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 URI (java.net.URI)25 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)21 ByteString (com.linkedin.data.ByteString)19 HashMap (java.util.HashMap)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 RestResponse (com.linkedin.r2.message.rest.RestResponse)18 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)17 ExecutionException (java.util.concurrent.ExecutionException)17 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)15 IOException (java.io.IOException)15 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)13