@@ -0,0 +1,1734 @@
+package lbms.plugins.mldht.kad;%0A%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.net.InetSocketAddress;%0Aimport java.util.ArrayList;%0Aimport java.util.EnumSet;%0Aimport java.util.List;%0Aimport java.util.stream.IntStream;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport lbms.plugins.mldht.kad.Node.InsertOptions;%0A%0Apublic class NodeTest %7B%0A%09%0A%09Node node;%0A%09%0A%09@Before%0A%09public void setup() %7B%0A%09%09DHT dht = NodeFactory.buildDHT();%0A%09%09node = dht.getNode();%0A%09%09node.initKey(null);%0A%09%09%0A%09%09NodeFactory.fillTable(node);%0A%09%7D%0A%09%0A%09%0A%09@Test%0A%09public void testBucketMerges() %7B%0A%09%09%0A%09%09Prefix p = new Prefix(Key.createRandomKey(), 20);%0A%09%09%0A%09%09List%3CKBucketEntry%3E added = new ArrayList%3C%3E();%0A%09%09%0A%09%09%0A%09%09for(int i=0;i%3C100;i++) %7B%0A%09%09%09KBucketEntry e = new KBucketEntry(new InetSocketAddress(NodeFactory.generateIp((byte) 0), 1337), p.createRandomKeyFromPrefix());%0A%09%09%09added.add(e);%0A%09%09%09e.signalResponse(0);%0A%09%09%09assertTrue(e.verifiedReachable());%0A%09%09%09assertTrue(e.eligibleForNodesList());%0A%09%09%09assertFalse(e.removableWithoutReplacement());%0A%09%09%09node.insertEntry(e, EnumSet.of(InsertOptions.ALWAYS_SPLIT_IF_FULL, InsertOptions.FORCE_INTO_MAIN_BUCKET));%0A%09%09%7D%0A%0A%09%09%0A%09%09// new Diagnostics().formatRoutingTable(System.out, node);%0A%09%09%0A%0A%09%09assertTrue(node.table().entryForId(p).prefix.getDepth() %3E p.getDepth());%0A%09%09%0A%09%09node.mergeBuckets();%0A%09%09%0A%09%09assertTrue(node.table().entryForId(p).prefix.getDepth() %3E p.getDepth());%0A%09%09%0A%09%09added.forEach(e -%3E %7B%0A%09%09%09e.signalScheduledRequest();%0A%09%09%09IntStream.rangeClosed(0, KBucketEntry.MAX_TIMEOUTS).forEach(x -%3E e.signalRequestTimeout());%0A%09%09%09assertTrue(e.removableWithoutReplacement());%0A%09%09%7D);%0A%0A%09%09%0A%09%09node.mergeBuckets();%0A%09%09%0A%09%09assertTrue(node.table().entryForId(p).prefix.getDepth() %3C p.getDepth());%0A%09%09%09%0A%09%09%0A%09%09%0A%09%7D%0A%0A%7D%0A

add test to ensure bucket merges can happen