From 0e616d9dcc705323684a20e76175ef750b28db48 Mon Sep 17 00:00:00 2001 From: James Watson Date: Mon, 28 Oct 2019 01:07:28 +0000 Subject: [PATCH 1/2] [Java] Add test for ExpandableRingBuffer to assert capacity. --- .../org/agrona/ExpandableRingBufferTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java b/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java index 4f6c55330..bdce171e7 100644 --- a/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java +++ b/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java @@ -17,6 +17,7 @@ import org.agrona.concurrent.UnsafeBuffer; +import org.junit.Ignore; import org.junit.Test; import org.mockito.InOrder; import org.mockito.Mockito; @@ -181,6 +182,26 @@ public void shouldAppendThenIterateMessagesInOrder() inOrder.verify(mockConsumer).onMessage(any(MutableDirectBuffer.class), anyInt(), eq(MSG_LENGTH_TWO), anyInt()); } + @Test + @Ignore + public void shouldAppendMessagesWithinCapacityWithoutExpanding() + { + final int initialCapacity = 1024; + final int maxCapacity = 2048; + + final ExpandableRingBuffer ringBuffer = new ExpandableRingBuffer(initialCapacity, maxCapacity, false); + + final ExpandableRingBuffer.MessageConsumer mockConsumer = mock(ExpandableRingBuffer.MessageConsumer.class); + when(mockConsumer.onMessage(any(), anyInt(), anyInt(), anyInt())).thenReturn(Boolean.TRUE); + + assertThat(ringBuffer.capacity(), is(initialCapacity)); + + final int messageLength = 32; + ringBuffer.append(TEST_MSG, 0, messageLength); + + assertThat(ringBuffer.capacity(), is(initialCapacity)); + } + @Test public void shouldAppendMessagesWithPaddingWithoutExpanding() { From 8951d951d525da9d122a45e2f8e56c1fb18ddb11 Mon Sep 17 00:00:00 2001 From: James Watson Date: Mon, 28 Oct 2019 01:10:56 +0000 Subject: [PATCH 2/2] [Java] Only increase capacity when necessary. --- .../java/org/agrona/ExpandableRingBuffer.java | 20 +++++++++++-------- .../org/agrona/ExpandableRingBufferTest.java | 2 -- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/agrona/src/main/java/org/agrona/ExpandableRingBuffer.java b/agrona/src/main/java/org/agrona/ExpandableRingBuffer.java index 957593c38..1af2d854a 100644 --- a/agrona/src/main/java/org/agrona/ExpandableRingBuffer.java +++ b/agrona/src/main/java/org/agrona/ExpandableRingBuffer.java @@ -386,15 +386,19 @@ public boolean append(final DirectBuffer srcBuffer, final int srcOffset, final i else if (tailOffset >= headOffset) { final int toEndRemaining = capacity - tailOffset; - if (alignedLength > toEndRemaining && alignedLength <= (totalRemaining - toEndRemaining)) - { - buffer.putInt(tailOffset + MESSAGE_LENGTH_OFFSET, toEndRemaining); - buffer.putInt(tailOffset + MESSAGE_TYPE_OFFSET, MESSAGE_TYPE_PADDING); - tail += toEndRemaining; - } - else + + if (alignedLength > toEndRemaining) { - resize(alignedLength); + if (alignedLength <= (totalRemaining - toEndRemaining)) + { + buffer.putInt(tailOffset + MESSAGE_LENGTH_OFFSET, toEndRemaining); + buffer.putInt(tailOffset + MESSAGE_TYPE_OFFSET, MESSAGE_TYPE_PADDING); + tail += toEndRemaining; + } + else + { + resize(alignedLength); + } } } diff --git a/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java b/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java index bdce171e7..d8fe77057 100644 --- a/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java +++ b/agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java @@ -17,7 +17,6 @@ import org.agrona.concurrent.UnsafeBuffer; -import org.junit.Ignore; import org.junit.Test; import org.mockito.InOrder; import org.mockito.Mockito; @@ -183,7 +182,6 @@ public void shouldAppendThenIterateMessagesInOrder() } @Test - @Ignore public void shouldAppendMessagesWithinCapacityWithoutExpanding() { final int initialCapacity = 1024;