forked from ohadeytan/caffeine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for overflow of the ring buffer read/write counters
- Loading branch information
Showing
2 changed files
with
28 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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]++); | ||
|
@@ -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(); | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters