From 2905b8620f665d8c076eb58071139d59603ca925 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sat, 6 Jul 2024 12:04:00 +0800 Subject: [PATCH 1/2] [Improve] add MapCap & LruMap unit test Signed-off-by: yuluo-yx --- .../hertzbeat/common/util/LruHashMapTest.java | 50 ++++++++++++++++++- .../hertzbeat/common/util/MapCapUtilTest.java | 27 ++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java index 8890f7401c4..73a4642d8a4 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java @@ -19,12 +19,60 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + /** * Test case for {@link LruHashMap} */ class LruHashMapTest { @Test - void removeEldestEntry() { + void testLruHashMap() { + + int initThreshold = 3; + LruHashMap initLruMap = new LruHashMap<>(initThreshold); + assertNotNull(initLruMap); + assertEquals(0, initLruMap.size()); + + int putAndGetThreshold = 2; + LruHashMap putAndGetLruMap = new LruHashMap<>(putAndGetThreshold); + + putAndGetLruMap.put(1, "one"); + putAndGetLruMap.put(2, "two"); + + // Both entries should be present + assertEquals("one", putAndGetLruMap.get(1)); + assertEquals("two", putAndGetLruMap.get(2)); + + int evictionThreshold = 2; + LruHashMap evictionLruMap = new LruHashMap<>(evictionThreshold); + + evictionLruMap.put(1, "one"); + evictionLruMap.put(2, "two"); + evictionLruMap.put(3, "three"); + + // The least recently used entry (1, "one") should be evicted + assertNull(evictionLruMap.get(1)); + assertEquals("two", evictionLruMap.get(2)); + assertEquals("three", evictionLruMap.get(3)); + + int accessOrderThreshold = 2; + LruHashMap accessOrderLruMap = new LruHashMap<>(accessOrderThreshold); + + accessOrderLruMap.put(1, "one"); + accessOrderLruMap.put(2, "two"); + + // Access the first entry to make it recently used + accessOrderLruMap.get(1); + + accessOrderLruMap.put(3, "three"); + + // The least recently used entry (2, "two") should be evicted + assertEquals("one", accessOrderLruMap.get(1)); + assertNull(accessOrderLruMap.get(2)); + assertEquals("three", accessOrderLruMap.get(3)); } + } diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java new file mode 100644 index 00000000000..7d4b9ced4d7 --- /dev/null +++ b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java @@ -0,0 +1,27 @@ +package org.apache.hertzbeat.common.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test for {@link MapCapUtil} + */ +class MapCapUtilTest { + + @Test + public void testCalInitMap() { + int size = 0; + int expectedCapacity = (int) Math.ceil(size / 0.75); + int actualCapacity = MapCapUtil.calInitMap(size); + + assertEquals(expectedCapacity, actualCapacity); + + size = 10; + expectedCapacity = (int) Math.ceil(size / 0.75); + actualCapacity = MapCapUtil.calInitMap(size); + + assertEquals(expectedCapacity, actualCapacity); + } + +} From a480a5e563d1605e266850a95df8d3942214d60f Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sat, 6 Jul 2024 12:05:41 +0800 Subject: [PATCH 2/2] fix: add license header Signed-off-by: yuluo-yx --- .../hertzbeat/common/util/MapCapUtilTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java index 7d4b9ced4d7..fac11a9cdf0 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java @@ -1,3 +1,20 @@ +/* + * 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.hertzbeat.common.util; import org.junit.jupiter.api.Test;