Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in ExpandableRingBuffer #187

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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