adoroszlai commented on code in PR #4666:
URL: https://github.com/apache/ozone/pull/4666#discussion_r1186732233


##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestCodec.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdds.utils.db;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.ref.WeakReference;
+import java.nio.ByteBuffer;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Test {@link Codec} implementations.
+ */
+public final class TestCodec {
+  static final Logger LOG = LoggerFactory.getLogger(TestCodec.class);
+  static final int NUM_LOOPS = 10;
+
+  /** Force gc to check leakage. */
+  static void gc() throws InterruptedException {
+    // use WeakReference to detect gc
+    Object obj = new Object();
+    final WeakReference<Object> weakRef = new WeakReference<>(obj);
+    obj = null;
+
+    // loop until gc has completed.
+    for (int i = 0; weakRef.get() != null; i++) {
+      LOG.info("gc {}", i);
+      System.gc();
+      Thread.sleep(100);
+    }
+    CodecBuffer.assertNoLeaks();
+  }
+
+  @Test
+  public void testIntegerCodec() throws Exception {
+    final IntegerCodec codec = new IntegerCodec();
+    runTest(codec, 0, Integer.BYTES);
+    runTest(codec, 1, Integer.BYTES);
+    runTest(codec, -1, Integer.BYTES);
+    runTest(codec, Integer.MAX_VALUE, Integer.BYTES);
+    runTest(codec, Integer.MIN_VALUE, Integer.BYTES);
+
+    for (int i = 0; i < NUM_LOOPS; i++) {
+      final int original = ThreadLocalRandom.current().nextInt();
+      runTest(codec, original, Integer.BYTES);
+    }
+    gc();
+  }
+
+  @Test
+  public void testLongCodec() throws Exception {
+    final LongCodec codec = new LongCodec();
+    runTest(codec, 0L, Long.BYTES);
+    runTest(codec, 1L, Long.BYTES);
+    runTest(codec, -1L, Long.BYTES);
+    runTest(codec, Long.MAX_VALUE, Long.BYTES);
+    runTest(codec, Long.MIN_VALUE, Long.BYTES);
+
+    for (int i = 0; i < NUM_LOOPS; i++) {
+      final long original = ThreadLocalRandom.current().nextLong();
+      runTest(codec, original, Long.BYTES);
+    }
+    gc();
+  }
+
+  @Test
+  public void testStringCodec() throws Exception {
+    final StringCodec codec = new StringCodec();
+    runTest(codec, "", 0);
+
+    for (int i = 0; i < NUM_LOOPS; i++) {
+      final String original = "test" + ThreadLocalRandom.current().nextLong();
+      runTest(codec, original, original.length());
+    }
+    gc();
+  }
+
+  static <T> void runTest(Codec<T> codec, T original,
+      Integer serializedSize) throws Exception {
+    Assertions.assertTrue(codec.supportCodecBuffer());
+
+    // serialize to byte[]
+    final byte[] array = codec.toPersistedFormat(original);
+    if (serializedSize != null) {
+      Assertions.assertEquals(serializedSize, array.length);

Review Comment:
   This would fail if String has multibyte chars.  Should pass 
`String.getBytes(UTF_8).length` instead of `String.length()`.  This is not a 
problem with the current state of the code, since `runTest` is only called with 
Latin1 strings, but could be improved in the future.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to