/** Tests adding and removing neighbors in random order repeatedly emptying out the table. */ @Test public void testAddRemoveEmpty() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); for (int i = 0; i < 10; i++) { // create i neighbors LinkedList<TrustGraphNodeId> neighbors = new LinkedList<TrustGraphNodeId>(createNeighbors(i)); // add them, then remove in order for (TrustGraphNodeId n : neighbors) { rt.addNeighbor(n); } for (TrustGraphNodeId n : neighbors) { rt.removeNeighbor(n); assertTrue(BasicRandomRoutingTable.isValidSnapshot(rt.snapshot())); } assertTrue(rt.isEmpty()); // add them and remove them in reverse order for (TrustGraphNodeId n : neighbors) { rt.addNeighbor(n); } Collections.reverse(neighbors); for (TrustGraphNodeId n : neighbors) { rt.removeNeighbor(n); assertTrue(BasicRandomRoutingTable.isValidSnapshot(rt.snapshot())); } assertTrue(rt.isEmpty()); // add them and remove them in random order for (TrustGraphNodeId n : neighbors) { rt.addNeighbor(n); } Collections.shuffle(neighbors); for (TrustGraphNodeId n : neighbors) { rt.removeNeighbor(n); assertTrue(BasicRandomRoutingTable.isValidSnapshot(rt.snapshot())); } assertTrue(rt.isEmpty()); } }
/** Tests the isEmpty() method of BasicRandomRoutingTable */ @Test public void testIsEmpty() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); // initially emtpy assertTrue(rt.isEmpty()); // add a neighbor -> no longer empty TrustGraphNodeId neighbor = createNeighbors(1).get(0); rt.addNeighbor(neighbor); assertTrue(!rt.isEmpty()); // remove the neighbor -> empty again rt.removeNeighbor(neighbor); assertTrue(rt.isEmpty()); }
/** * Tests that removeNeighbor only removes the intended neighbor from the routing and preserves a * valid routing. */ @Test public void testRemoveNeighbor() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); // create a set of neighbors to test with List<TrustGraphNodeId> neighbors = createNeighbors(100); rt.addNeighbors(neighbors); // make sure everyone is in the table to start with for (TrustGraphNodeId n : neighbors) { assertTrue(rt.contains(n)); } /* remove the neighbors in random order. Check that the * routing remains valid, the expected item has been removed * and that all other items are still present. */ LinkedList<TrustGraphNodeId> shuffled = new LinkedList(neighbors); Collections.shuffle(shuffled); for (int i = 0; i < neighbors.size(); i++) { // select a neighbor to remove and remove it TrustGraphNodeId remove = shuffled.pop(); rt.removeNeighbor(remove); // make sure it's gone assertFalse(rt.contains(remove)); // make sure everything else is still there. for (TrustGraphNodeId n : shuffled) { assertTrue(rt.contains(n)); } // make sure the routing is still valid in total. assertTrue(BasicRandomRoutingTable.isValidSnapshot(rt.snapshot())); } // make sure everything was eventually removed assertTrue(rt.isEmpty()); }