Search in sources :

Example 6 with AsyncRateLimiter

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"));
}
Also used : AsyncRateLimiter(com.linkedin.r2.transport.http.client.AsyncRateLimiter) 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 7 with AsyncRateLimiter

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()));
}
Also used : AsyncRateLimiter(com.linkedin.r2.transport.http.client.AsyncRateLimiter) 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 8 with AsyncRateLimiter

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

Aggregations

AsyncRateLimiter (com.linkedin.r2.transport.http.client.AsyncRateLimiter)8 Test (org.testng.annotations.Test)8 FutureCallback (com.linkedin.common.callback.FutureCallback)7 None (com.linkedin.common.util.None)7 ArrayList (java.util.ArrayList)5 ClockedExecutor (com.linkedin.test.util.ClockedExecutor)4 SettableClock (com.linkedin.util.clock.SettableClock)2 Callback (com.linkedin.common.callback.Callback)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LongAdder (java.util.concurrent.atomic.LongAdder)1