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());
}
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());
}
Aggregations