diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/AbstractConsistentHash.java b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/AbstractConsistentHash.java index 8731cf6f..34e92b6d 100644 --- a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/AbstractConsistentHash.java +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/AbstractConsistentHash.java @@ -45,7 +45,7 @@ protected void sort(){} * @return */ public String process(List values,String key){ - + // fix https://github.com/crossoverJie/cim/issues/79 clear(); for (String value : values) { add(hash(value), value); diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHash.java b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHash.java index 97a8f902..73c6f011 100644 --- a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHash.java +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHash.java @@ -1,6 +1,9 @@ package com.crossoverjie.cim.common.route.algorithm.consistenthash; import com.crossoverjie.cim.common.data.construct.SortArrayMap; +import com.google.common.annotations.VisibleForTesting; + +import java.util.TreeMap; /** * Function:自定义排序 Map 实现 @@ -32,6 +35,15 @@ public void sort() { sortArrayMap.sort(); } + /** + * Used only in test. + * @return Return the data structure of the current algorithm. + */ + @VisibleForTesting + public SortArrayMap getSortArrayMap() { + return sortArrayMap; + } + @Override protected void clear() { sortArrayMap.clear(); diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHash.java b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHash.java index ae587899..650ba766 100644 --- a/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHash.java +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHash.java @@ -2,6 +2,7 @@ import com.crossoverjie.cim.common.enums.StatusEnum; import com.crossoverjie.cim.common.exception.CIMException; +import com.google.common.annotations.VisibleForTesting; import java.util.SortedMap; import java.util.TreeMap; @@ -35,6 +36,15 @@ protected void clear() { treeMap.clear(); } + /** + * Used only in test. + * @return Return the data structure of the current algorithm. + */ + @VisibleForTesting + public TreeMap getTreeMap() { + return treeMap; + } + @Override public String getFirstNodeValue(String value) { long hash = super.hash(value); @@ -48,4 +58,5 @@ public String getFirstNodeValue(String value) { } return treeMap.firstEntry().getValue(); } + } diff --git a/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHashTest.java b/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHashTest.java index 8b11cc57..97df099e 100644 --- a/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHashTest.java +++ b/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/SortArrayMapConsistentHashTest.java @@ -137,7 +137,7 @@ public void getFirstNodeValue8() { @Test public void testVirtualNode() throws NoSuchFieldException, IllegalAccessException { - AbstractConsistentHash map = new SortArrayMapConsistentHash(); + SortArrayMapConsistentHash map = new SortArrayMapConsistentHash(); List strings = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -145,13 +145,9 @@ public void testVirtualNode() throws NoSuchFieldException, IllegalAccessExceptio } String process = map.process(strings,"zhangsan"); - Field sortArrayMapField = SortArrayMapConsistentHash.class.getDeclaredField("sortArrayMap"); - sortArrayMapField.setAccessible(true); - Field virtualNodeSizeField = SortArrayMapConsistentHash.class.getDeclaredField("VIRTUAL_NODE_SIZE"); - virtualNodeSizeField.setAccessible(true); - SortArrayMap sortArrayMap = (SortArrayMap) sortArrayMapField.get(map); - int virtualNodeSize = (int) virtualNodeSizeField.get(map); + SortArrayMap sortArrayMap = map.getSortArrayMap(); + int virtualNodeSize = 2; System.out.println("sortArrayMapSize = " + sortArrayMap.size() + "\n" + "virtualNodeSize = " + virtualNodeSize); Assert.assertEquals(sortArrayMap.size(), (virtualNodeSize + 1) * 10); diff --git a/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHashTest.java b/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHashTest.java index c5a36830..01060894 100644 --- a/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHashTest.java +++ b/cim-common/src/test/java/com/crossoverjie/cim/common/route/algorithm/consistenthash/TreeMapConsistentHashTest.java @@ -40,9 +40,6 @@ public void getFirstNodeValue2() { String process = map.process(strings, "zhangsan2"); Assert.assertEquals(PROCESS, process); } - - -// Assert.assertEquals("127.0.0.9",process); } @@ -79,7 +76,7 @@ public void getFirstNodeValue4() { @Test public void testVirtualNode() throws NoSuchFieldException, IllegalAccessException { - AbstractConsistentHash map = new TreeMapConsistentHash(); + TreeMapConsistentHash map = new TreeMapConsistentHash(); List strings = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -87,13 +84,9 @@ public void testVirtualNode() throws NoSuchFieldException, IllegalAccessExceptio } String process = map.process(strings,"zhangsan"); - Field treeMapField = TreeMapConsistentHash.class.getDeclaredField("treeMap"); - treeMapField.setAccessible(true); - Field virtualNodeSizeField = TreeMapConsistentHash.class.getDeclaredField("VIRTUAL_NODE_SIZE"); - virtualNodeSizeField.setAccessible(true); - TreeMap treeMap = (TreeMap) treeMapField.get(map); - int virtualNodeSize = (int) virtualNodeSizeField.get(map); + TreeMap treeMap = map.getTreeMap(); + int virtualNodeSize = 2; System.out.println("treeMapSize = " + treeMap.size() + "\n" + "virtualNodeSize = " + virtualNodeSize); Assert.assertEquals(treeMap.size(), (virtualNodeSize + 1) * 10);