Reputation: 28061
I'm trying to unit test an application which publishes/consumes messages using the Disruptor ring buffer. In the test I would like to
Is there a simple way that I can wait for the messages to be consumed (step 3 above)?
Upvotes: 0
Views: 82
Reputation: 28061
I ended up solving this with a loop which completes when RingBuffer.remainingCapacity()
is equal to Disruptor.getBufferSize()
public class MyTest {
private Disruptor<MyEvent> disruptor;
@BeforeEach
public void beforeEach() {
disruptor = new Disruptor<>(...);
disruptor.handleEventsWith(...);
disruptor.start();
}
@AfterEach
public void afterEach() {
disruptor.shutdown();
}
@Test
public void myTest() throws Exception {
disruptor.publishEvent(...);
disruptor.publishEvent(...);
awaitRingBuffer();
assertThat(...);
}
private void awaitRingBuffer() throws InterruptedException {
RingBuffer<MyEvent> ringBuffer = disruptor.getRingBuffer();
while (ringBuffer.remainingCapacity() != disruptor.getBufferSize()) {
Thread.sleep(10);
}
}
}
Upvotes: 0