Skip to content

Commit

Permalink
Merge pull request #187 from JPWatson/erb-capacity
Browse files Browse the repository at this point in the history
Fix bug in ExpandableRingBuffer
  • Loading branch information
mjpt777 authored Oct 28, 2019
2 parents 0544ade + 8951d95 commit 170a221
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 12 additions & 8 deletions agrona/src/main/java/org/agrona/ExpandableRingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions agrona/src/test/java/org/agrona/ExpandableRingBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ public void shouldAppendThenIterateMessagesInOrder()
inOrder.verify(mockConsumer).onMessage(any(MutableDirectBuffer.class), anyInt(), eq(MSG_LENGTH_TWO), anyInt());
}

@Test
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()
{
Expand Down

0 comments on commit 170a221

Please sign in to comment.