This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new d8c574b1a2a HDDS-14259. Improve TestManagedDirectSlice. (#9571)
d8c574b1a2a is described below

commit d8c574b1a2a3f8eab9bf251774e190403954a2b9
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Tue Dec 30 20:41:41 2025 -0800

    HDDS-14259. Improve TestManagedDirectSlice. (#9571)
---
 .../utils/db/managed/TestManagedDirectSlice.java   | 84 ++++++++++++++++++----
 1 file changed, 69 insertions(+), 15 deletions(-)

diff --git 
a/hadoop-hdds/managed-rocksdb/src/test/java/org/apache/hadoop/hdds/utils/db/managed/TestManagedDirectSlice.java
 
b/hadoop-hdds/managed-rocksdb/src/test/java/org/apache/hadoop/hdds/utils/db/managed/TestManagedDirectSlice.java
index f13aba39a7b..a69bcd8d06c 100644
--- 
a/hadoop-hdds/managed-rocksdb/src/test/java/org/apache/hadoop/hdds/utils/db/managed/TestManagedDirectSlice.java
+++ 
b/hadoop-hdds/managed-rocksdb/src/test/java/org/apache/hadoop/hdds/utils/db/managed/TestManagedDirectSlice.java
@@ -20,31 +20,85 @@
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.nio.ByteBuffer;
-import java.util.Arrays;
-import org.apache.commons.lang3.RandomUtils;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.CsvSource;
+import java.util.Random;
+import org.apache.hadoop.hdds.utils.db.CodecBuffer;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests for ManagedDirectSlice.
  */
 public class TestManagedDirectSlice {
-
   static {
     ManagedRocksObjectUtils.loadRocksDBLibrary();
   }
 
-  @ParameterizedTest
-  @CsvSource({"0, 1024", "1024, 1024", "512, 1024", "0, 100", "10, 512", "0, 
0"})
-  public void testManagedDirectSliceWithOffset(int offset, int 
numberOfBytesWritten) {
-    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
-    byte[] randomBytes = RandomUtils.secure().nextBytes(numberOfBytesWritten);
-    byteBuffer.put(randomBytes);
-    byteBuffer.flip();
-    byteBuffer.position(offset);
-    try (ManagedDirectSlice directSlice = new ManagedDirectSlice(byteBuffer);
-         ManagedSlice slice = new ManagedSlice(Arrays.copyOfRange(randomBytes, 
offset, numberOfBytesWritten))) {
+  static final Random RANDOM = new Random();
+  static final byte[] ZEROS = new byte[1 << 10];
+  private static int count = 0;
+
+  @Test
+  public void testManagedDirectSlice() {
+    // test small sizes
+    final int small = 8;
+    for (int size = 0; size <= small; size++) {
+      testManagedDirectSlice(size);
+    }
+
+    // test power of 2 sizes
+    for (int i = 1; i <= 10; i++) {
+      final int size = small << i;
+      testManagedDirectSlice(size - 1);
+      testManagedDirectSlice(size);
+      testManagedDirectSlice(size + 1);
+    }
+
+    // test random sizes
+    final int bound = ZEROS.length << 10;
+    for (int i = 0; i < 4; i++) {
+      final int size = RANDOM.nextInt(bound);
+      testManagedDirectSlice(size);
+    }
+    for (int i = 0; i < 4; i++) {
+      final int size = RANDOM.nextInt(bound) + ZEROS.length;
+      testManagedDirectSlice(size);
+    }
+  }
+
+  static void testManagedDirectSlice(int size) {
+    // test small positions
+    final int small = 3;
+    for (int position = 0; position < small; position++) {
+      testManagedDirectSlice(size, position);
+    }
+    // test large positions
+    for (int i = 0; i < small; i++) {
+      testManagedDirectSlice(size, ZEROS.length - i);
+    }
+    // test random positions
+    for (int i = 0; i < 4; i++) {
+      final int bound = ZEROS.length - 2 * small + 1;
+      final int position = RANDOM.nextInt(bound) + small; // small <= position 
<= ZEROS.length-small
+      testManagedDirectSlice(size, position);
+    }
+  }
+
+  static void testManagedDirectSlice(int size, int position) {
+    System.out.printf("%3d: size %d and position %d%n", ++count, size, 
position);
+    final byte[] bytes = new byte[size];
+    RANDOM.nextBytes(bytes);
+    try (CodecBuffer buffer = CodecBuffer.allocateDirect(size + position)
+            .put(ByteBuffer.wrap(ZEROS, 0, position))
+            .put(ByteBuffer.wrap(bytes));
+         ManagedDirectSlice directSlice = new 
ManagedDirectSlice(getByteBuffer(buffer, position));
+         ManagedSlice slice = new ManagedSlice(bytes)) {
+      assertEquals(slice.size(), directSlice.size());
       assertEquals(slice, directSlice);
     }
   }
+
+  static ByteBuffer getByteBuffer(CodecBuffer buffer, int position) {
+    final ByteBuffer byteBuffer = buffer.asReadOnlyByteBuffer();
+    byteBuffer.position(position);
+    return byteBuffer;
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to