Skip to content

Commit

Permalink
Added tests AtomicBuffer acq/rel/opaque methods
Browse files Browse the repository at this point in the history
Also fixed a bug with the opaque int methods. It was checking
if there was 'long' space available instead of 'int' space.
  • Loading branch information
pveentjer committed Feb 24, 2025
1 parent 880bdf0 commit ceeb89b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
6 changes: 3 additions & 3 deletions agrona/src/main/java/org/agrona/concurrent/UnsafeBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public void putIntOpaque(final int index, final int value)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
boundsCheck0(index, SIZE_OF_INT);
}

UnsafeApi.putIntOpaque(byteArray, addressOffset + index, value);
Expand All @@ -636,7 +636,7 @@ public int getIntOpaque(final int index)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
boundsCheck0(index, SIZE_OF_INT);
}

return UnsafeApi.getIntOpaque(byteArray, addressOffset + index);
Expand All @@ -649,7 +649,7 @@ public int addIntOpaque(final int index, final int increment)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_LONG);
boundsCheck0(index, SIZE_OF_INT);
}

final int oldValue = UnsafeApi.getIntOpaque(byteArray, addressOffset + index);
Expand Down
48 changes: 48 additions & 0 deletions agrona/src/test/java/org/agrona/concurrent/AtomicBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,54 @@ void shouldGetBytesFromBufferToSlice(final ByteBuffer buffer)
assertThat(result, is(testBytes));
}

@Test
void verifyIntOpaqueOperations()
{
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(new byte[4]);
final int index = 0;
assertEquals(0, unsafeBuffer.getIntOpaque(index));
unsafeBuffer.putIntOpaque(index, 10);
assertEquals(10, unsafeBuffer.getIntOpaque(index));
assertEquals(10, unsafeBuffer.addIntOpaque(0, 2));
assertEquals(12, unsafeBuffer.getIntOpaque(index));
}

@Test
void verifyIntReleaseAcquireOperations()
{
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(new byte[4]);
final int index = 0;
assertEquals(0, unsafeBuffer.getIntAcquire(index));
unsafeBuffer.putIntRelease(index, 10);
assertEquals(10, unsafeBuffer.getIntAcquire(index));
assertEquals(10, unsafeBuffer.addIntRelease(0, 2));
assertEquals(12, unsafeBuffer.getIntAcquire(index));
}

@Test
void verifyLongOpaqueOperations()
{
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(new byte[8]);
final int index = 0;
assertEquals(0, unsafeBuffer.getLongOpaque(index));
unsafeBuffer.putLongOpaque(index, 10);
assertEquals(10, unsafeBuffer.getLongOpaque(index));
assertEquals(10, unsafeBuffer.addLongOpaque(index, 2));
assertEquals(12, unsafeBuffer.getLongOpaque(index));
}

@Test
void verifyLongReleaseAcquireOperations()
{
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(new byte[8]);
final int index = 0;
assertEquals(0, unsafeBuffer.getLongAcquire(index));
unsafeBuffer.putLongRelease(index, 10);
assertEquals(10, unsafeBuffer.getLongAcquire(index));
assertEquals(10, unsafeBuffer.addLongRelease(index, 2));
assertEquals(12, unsafeBuffer.getLongAcquire(index));
}

@Test
void verifyAlignmentShouldThrowForAByteArrayIfStrictAlignmentChecksAreEnabled()
{
Expand Down

0 comments on commit ceeb89b

Please sign in to comment.