Skip to content

Commit

Permalink
add test for overflow of the ring buffer read/write counters
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Aug 27, 2022
1 parent 46cd583 commit ac8e049
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.truth.Truth.assertThat;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -31,28 +32,27 @@
* @author [email protected] (Ben Manes)
*/
public final class BoundedBufferTest {
static final String DUMMY = "test";

@DataProvider
public Object[][] buffer() {
return new Object[][] {{ new BoundedBuffer<String>() }};
return new Object[][] {{ new BoundedBuffer<Boolean>() }};
}

@Test(dataProvider = "buffer")
public void offer(BoundedBuffer<String> buffer) {
public void offer(BoundedBuffer<Boolean> buffer) {
ConcurrentTestHarness.timeTasks(10, () -> {
for (int i = 0; i < 100; i++) {
buffer.offer(DUMMY);
buffer.offer(Boolean.TRUE);
}
});
assertThat(buffer.writes()).isGreaterThan(0);
assertThat(buffer.writes()).isEqualTo(buffer.size());
}

@Test(dataProvider = "buffer")
public void drain(BoundedBuffer<String> buffer) {
public void drain(BoundedBuffer<Boolean> buffer) {
for (int i = 0; i < BoundedBuffer.BUFFER_SIZE; i++) {
buffer.offer(DUMMY);
buffer.offer(Boolean.TRUE);
}
long[] read = new long[1];
buffer.drainTo(e -> read[0]++);
Expand All @@ -62,12 +62,12 @@ public void drain(BoundedBuffer<String> buffer) {

@Test(dataProvider = "buffer")
@SuppressWarnings("ThreadPriorityCheck")
public void offerAndDrain(BoundedBuffer<String> buffer) {
public void offerAndDrain(BoundedBuffer<Boolean> buffer) {
var lock = new ReentrantLock();
var reads = new AtomicInteger();
ConcurrentTestHarness.timeTasks(10, () -> {
for (int i = 0; i < 1000; i++) {
boolean shouldDrain = (buffer.offer(DUMMY) == Buffer.FULL);
boolean shouldDrain = (buffer.offer(Boolean.TRUE) == Buffer.FULL);
if (shouldDrain && lock.tryLock()) {
buffer.drainTo(e -> reads.incrementAndGet());
lock.unlock();
Expand All @@ -79,4 +79,22 @@ public void offerAndDrain(BoundedBuffer<String> buffer) {
assertThat(reads.longValue()).isEqualTo(buffer.reads());
assertThat(reads.longValue()).isEqualTo(buffer.writes());
}

@Test
public void overflow() {
var buffer = new BoundedBuffer.RingBuffer<Boolean>(null);
buffer.writeCounter = Long.MAX_VALUE;
buffer.readCounter = Long.MAX_VALUE;

buffer.offer(Boolean.TRUE);
var data = new ArrayList<>();
buffer.drainTo(data::add);

for (var e : buffer.buffer) {
assertThat(e).isNull();
}
assertThat(data).containsExactly(Boolean.TRUE);
assertThat(buffer.readCounter).isEqualTo(Long.MIN_VALUE);
assertThat(buffer.writeCounter).isEqualTo(Long.MIN_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public void evict_admit(BoundedLocalCache<Int, Int> cache, CacheContext context)
assertThat(cache.admit(candidate, victim)).isFalse();
}

// Allow
// Admit a small, random percentage of warm candidates to protect against hash flooding
while (cache.frequencySketch().frequency(candidate) < ADMIT_HASHDOS_THRESHOLD) {
cache.frequencySketch().increment(candidate);
}
Expand All @@ -701,8 +701,8 @@ public void evict_admit(BoundedLocalCache<Int, Int> cache, CacheContext context)
}
}
assertThat(allow).isGreaterThan(0);
assertThat(reject).isGreaterThan(0);
assertThat(reject).isGreaterThan(allow);
assertThat(100.0 * allow / (allow + reject)).isIn(Range.open(0.5, 1.5));
}

@Test(groups = "isolated")
Expand Down

0 comments on commit ac8e049

Please sign in to comment.