/** 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 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 that calling RemoveNeighbors removes only the intended neighbors and preserves valid * routing. */ @Test public void testRemoveNeighbors() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); // create a set of neighbors to test with List<TrustGraphNodeId> neighbors = createNeighbors(120); rt.addNeighbors(neighbors); // make sure everyone is in the table to start with for (TrustGraphNodeId n : neighbors) { assertTrue(rt.contains(n)); } /* remove the 120 neighbors in random order, in increasingly large * chunks from 1 to 15 */ LinkedList<TrustGraphNodeId> shuffled = new LinkedList(neighbors); Collections.shuffle(shuffled); for (int i = 1; i <= 15; i++) { /* pop i things from the shuffled list into the list remove, * then remove them in a bulk operation. */ LinkedList<TrustGraphNodeId> remove = new LinkedList(); for (int j = 0; j < i; j++) { remove.push(shuffled.pop()); } rt.removeNeighbors(remove); // make sure they're gone. for (TrustGraphNodeId n : remove) { assertFalse(rt.contains(n)); } // 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()); }
/** Tests the clear() method */ @Test public void testClear() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); // create a set of neighbors and add them to the table List<TrustGraphNodeId> neighbors = createNeighbors(100); rt.addNeighbors(neighbors); // verify that they're all there for (TrustGraphNodeId n : neighbors) { assertTrue(rt.contains(n)); } // clear the table rt.clear(); // verify that they're all not there for (TrustGraphNodeId n : neighbors) { assertFalse(rt.contains(n)); } 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()); }