Search in sources :

Example 1 with RateLimiter

use of org.apache.accumulo.core.util.ratelimit.RateLimiter in project accumulo by apache.

the class RateLimitedOutputStreamTest method permitsAreProperlyAcquired.

@Test
public void permitsAreProperlyAcquired() throws Exception {
    // Create variables for tracking behaviors of mock object
    AtomicLong rateLimiterPermitsAcquired = new AtomicLong();
    // Construct mock object
    RateLimiter rateLimiter = EasyMock.niceMock(RateLimiter.class);
    // Stub Mock Method
    rateLimiter.acquire(EasyMock.anyLong());
    EasyMock.expectLastCall().andAnswer(() -> rateLimiterPermitsAcquired.addAndGet(EasyMock.getCurrentArgument(0))).anyTimes();
    EasyMock.replay(rateLimiter);
    long bytesWritten = 0;
    try (RateLimitedOutputStream os = new RateLimitedOutputStream(new NullOutputStream(), rateLimiter)) {
        for (int i = 0; i < 100; ++i) {
            byte[] bytes = new byte[Math.abs(random.nextInt() % 65536)];
            os.write(bytes);
            bytesWritten += bytes.length;
        }
        assertEquals(bytesWritten, os.position());
    }
    assertEquals(bytesWritten, rateLimiterPermitsAcquired.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RateLimiter(org.apache.accumulo.core.util.ratelimit.RateLimiter) Test(org.junit.jupiter.api.Test)

Example 2 with RateLimiter

use of org.apache.accumulo.core.util.ratelimit.RateLimiter in project accumulo by apache.

the class RateLimitedInputStreamTest method permitsAreProperlyAcquired.

@Test
public void permitsAreProperlyAcquired() throws Exception {
    // Create variables for tracking behaviors of mock object
    AtomicLong rateLimiterPermitsAcquired = new AtomicLong();
    // Construct mock object
    RateLimiter rateLimiter = EasyMock.niceMock(RateLimiter.class);
    // Stub Mock Method
    rateLimiter.acquire(EasyMock.anyLong());
    EasyMock.expectLastCall().andAnswer(() -> rateLimiterPermitsAcquired.addAndGet(EasyMock.getCurrentArgument(0))).anyTimes();
    EasyMock.replay(rateLimiter);
    long bytesRetrieved = 0;
    try (InputStream is = new RateLimitedInputStream(new RandomInputStream(), rateLimiter)) {
        for (int i = 0; i < 100; ++i) {
            int count = Math.abs(random.nextInt()) % 65536;
            int countRead = is.read(new byte[count]);
            assertEquals(count, countRead);
            bytesRetrieved += count;
        }
    }
    assertEquals(bytesRetrieved, rateLimiterPermitsAcquired.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) InputStream(java.io.InputStream) RateLimiter(org.apache.accumulo.core.util.ratelimit.RateLimiter) Test(org.junit.jupiter.api.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2 RateLimiter (org.apache.accumulo.core.util.ratelimit.RateLimiter)2 Test (org.junit.jupiter.api.Test)2 InputStream (java.io.InputStream)1