bucket.addOrUpdate(peer);
@Test public void testIsFull() { for (KBucket bucket: buckets) { Destination[] destinations = destinationMap.get(bucket); for (int i=0; i<K-1; i++) { bucket.addOrUpdate(new KademliaPeer(destinations[i])); assertFalse(bucket.isFull()); } bucket.addOrUpdate(new KademliaPeer(destinations[K-1])); assertTrue(bucket.isFull()); } }
@Test public void testAddOrUpdate() { for (KBucket bucket: buckets) { KademliaPeer[] peers = destinationMap.get(bucket); // add a peer bucket.addOrUpdate(peers[0]); assertEquals(1, bucket.size()); assertEquals(peers[0], bucket.iterator().next()); // add the same peer again, verify there is no duplicate bucket.addOrUpdate(peers[0]); assertEquals(1, bucket.size()); assertEquals(peers[0], bucket.iterator().next()); // add a different peer bucket.addOrUpdate(peers[1]); assertEquals(2, bucket.size()); assertEquals(peers[0], bucket.getPeer(peers[0])); assertEquals(peers[1], bucket.getPeer(peers[1])); } }
@Test public void testRemove() { for (KBucket bucket: buckets) { Destination[] destinations = destinationMap.get(bucket); assertEquals(0, bucket.size()); bucket.addOrUpdate(new KademliaPeer(destinations[0])); assertEquals(1, bucket.size()); bucket.remove(destinations[0]); assertEquals(0, bucket.size()); bucket.addOrUpdate(new KademliaPeer(destinations[0])); bucket.addOrUpdate(new KademliaPeer(destinations[1])); bucket.addOrUpdate(new KademliaPeer(destinations[2])); assertEquals(3, bucket.size()); bucket.remove(destinations[0]); assertEquals(2, bucket.size()); bucket.remove(destinations[1]); assertEquals(1, bucket.size()); bucket.remove(destinations[2]); assertEquals(0, bucket.size()); } }
@Test public void testSplit() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { assertEquals("K must be an even number for this test to work.", 0, K%2); for (KBucket bucket: buckets) { KademliaPeer[] peers = destinationMap.get(bucket); int originalDepth = KademliaTestUtil.getDepth(bucket); for (int i=0; i<K; i++) { assertFalse(bucket.shouldSplit(peers[i])); bucket.addOrUpdate(peers[i]); } assertTrue(bucket.shouldSplit(peers[K])); KBucket newBucket = bucket.split(); assertNotNull(newBucket); assertEquals(originalDepth+1, KademliaTestUtil.getDepth(bucket)); assertEquals(originalDepth+1, KademliaTestUtil.getDepth(bucket)); assertTrue(maxId(bucket).compareTo(minId(newBucket)) < 0); } }