/** Tests that getNextHop and snapshot agree. */ @Test public void testSnapshotVsGetNextHop() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); // add a group of neighbors to the routing table List<TrustGraphNodeId> neighbors = createNeighbors(1000); rt.addNeighbors(neighbors); // capture a snapshot of the routing RandomRoutingTable.Snapshot snapshot = rt.snapshot(); Map<TrustGraphNodeId, TrustGraphNodeId> routes = snapshot.getRoutes(); assertTrue(routes.size() == neighbors.size()); assertTrue(BasicRandomRoutingTable.isValidSnapshot(snapshot)); /* make sure that each mapping in the snapshot matches up with * the result of getNextHop and vice versa. */ for (Map.Entry<TrustGraphNodeId, TrustGraphNodeId> e : routes.entrySet()) { assertTrue(rt.getNextHop(e.getKey()).equals(e.getValue())); } for (TrustGraphNodeId n : neighbors) { assertTrue(rt.getNextHop(n).equals(routes.get(n))); } }
/** Tests that adding neighbors via the single add method fixes self mappings. */ @Test public void testAddNeighborFixesSelfMapping() { RandomRoutingTable rt = new BasicRandomRoutingTable(); RandomRoutingTable.Snapshot snapshot; Map<TrustGraphNodeId, TrustGraphNodeId> routes; // create a set of neighbors to add List<TrustGraphNodeId> neighbors = createNeighbors(4); // add the first neighbor, it should just contain // the first neighbor mapped to itself TrustGraphNodeId first = neighbors.get(0); rt.addNeighbor(first); assertTrue(rt.getNextHop(first).equals(first)); snapshot = rt.snapshot(); routes = snapshot.getRoutes(); assertTrue(routes.size() == 1); assertTrue(BasicRandomRoutingTable.isValidSnapshot(snapshot)); // add the other neighbors. This should immediately fix // the self routing. for (int i = 1; i < neighbors.size(); i++) { rt.addNeighbor(neighbors.get(i)); assertTrue(!rt.getNextHop(first).equals(first)); snapshot = rt.snapshot(); routes = snapshot.getRoutes(); assertTrue(routes.size() == 1 + i); assertTrue(BasicRandomRoutingTable.isValidSnapshot(snapshot)); } }
/** * Tests that the output of snapshot can be used to construct a routing table and that the routing * is preserved. */ @Test public void testSnapshotReload() throws Exception { RandomRoutingTable rt1 = new BasicRandomRoutingTable(); // create a set of neighbors and add them to the table List<TrustGraphNodeId> neighbors = createNeighbors(1000); rt1.addNeighbors(neighbors); RandomRoutingTable.Snapshot snapshotIn = rt1.snapshot(); // create a routing table using the snapshot RandomRoutingTable rt2 = new BasicRandomRoutingTable(snapshotIn); RandomRoutingTable.Snapshot snapshotOut = rt2.snapshot(); /* verify each route that we created appears in the routing * according to getNextHop and a snapshot. */ for (Map.Entry<TrustGraphNodeId, TrustGraphNodeId> e : snapshotIn.getRoutes().entrySet()) { TrustGraphNodeId key = e.getKey(); TrustGraphNodeId value = e.getValue(); // getNextHop(key) == value assertTrue(rt2.getNextHop(key).equals(value)); // snapshot[key] == value assertTrue(snapshotOut.getRoutes().get(key).equals(value)); } }
/** Tests that routes are created correctly when snapshots are loaded. */ @Test public void testSnapshotLoad() throws Exception { // create a set of neighbors and add them to the table List<TrustGraphNodeId> neighbors = createNeighbors(1000); Map<TrustGraphNodeId, TrustGraphNodeId> routesIn = new HashMap<TrustGraphNodeId, TrustGraphNodeId>(); for (int i = 0; i < neighbors.size(); i++) { // map each neigbor to the next, circularly routesIn.put(neighbors.get(i), neighbors.get((i + 1) % neighbors.size())); } // create a routing table using the constructed snapshot RandomRoutingTable.Snapshot snapshotIn = new BasicRandomRoutingTable.Snapshot(routesIn, new ArrayList(routesIn.keySet())); RandomRoutingTable rt = new BasicRandomRoutingTable(snapshotIn); Map<TrustGraphNodeId, TrustGraphNodeId> snapshotOut = rt.snapshot().getRoutes(); /* verify each route that we created appears in the routing * according to getNextHop and a snapshot. */ for (Map.Entry<TrustGraphNodeId, TrustGraphNodeId> e : routesIn.entrySet()) { TrustGraphNodeId key = e.getKey(); TrustGraphNodeId value = e.getValue(); // getNextHop(key) == value assertTrue(rt.getNextHop(key).equals(value)); // snapshot[key] == value assertTrue(snapshotOut.get(key).equals(value)); } }
/** Tests that the single add neighbor method addNeighbor creates a valid set of routes. */ @Test public void testAddNeighborBasic() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); /* create a large group of neigbors and add them * individually. */ Collection<TrustGraphNodeId> neighbors = createNeighbors(1000); for (TrustGraphNodeId n : neighbors) { rt.addNeighbor(n); } // there should be a route for every neighbor added for (TrustGraphNodeId n : neighbors) { assertTrue(rt.contains(n)); } // every neighbor should be routed to someone else for (TrustGraphNodeId n : neighbors) { TrustGraphNodeId next = rt.getNextHop(n); assertTrue(next != null && !next.equals(n)); } // A snapshot of the resulting routing table should validate RandomRoutingTable.Snapshot snapshot = rt.snapshot(); Map<TrustGraphNodeId, TrustGraphNodeId> routes = snapshot.getRoutes(); assertTrue(routes.size() == neighbors.size()); assertTrue(BasicRandomRoutingTable.isValidSnapshot(snapshot)); // every neighbor should be in the snapshot for (TrustGraphNodeId n : neighbors) { assertTrue(routes.containsKey(n)); assertTrue(routes.containsValue(n)); } }
/** * Tests that using the bulk addNeighbors function does not disrupt more than one previous route. */ @Test public void testAddNeighborsPreservesRoutes() throws Exception { RandomRoutingTable rt = new BasicRandomRoutingTable(); /* create a group of neighbors and add them to the * routing table. A snapshot of the routing is created * so that it can be compared to the routing table * after adding additional neighbors. */ Collection<TrustGraphNodeId> originalNeighbors = createNeighbors(500); rt.addNeighbors(originalNeighbors); RandomRoutingTable.Snapshot snapshot = rt.snapshot(); Map<TrustGraphNodeId, TrustGraphNodeId> routes = snapshot.getRoutes(); /* count tracks how many neighbors have been added * to the routing so far. */ int count = originalNeighbors.size(); /* for a few randomly chosen sizes, bulk add * neighbors and check the properties * of the routing. */ Random rng = new Random(); for (int i = 0; i < 10; i++) { int newCount = 100 + rng.nextInt(400); Collection<TrustGraphNodeId> newNeighbors = createNeighbors(newCount); rt.addNeighbors(newNeighbors); /* check that at most one route has changed by counting * the number of routes that are the same as the last * run. */ int sameCount = 0; for (Map.Entry<TrustGraphNodeId, TrustGraphNodeId> e : routes.entrySet()) { if (rt.getNextHop(e.getKey()).equals(e.getValue())) { sameCount += 1; } } assertTrue(routes.size() - sameCount <= 1); // check that all the new neighors are also routed for (TrustGraphNodeId n : newNeighbors) { assertTrue(rt.contains(n)); } /* take a new snapshot and ensure that it * has the correct size and is valid */ snapshot = rt.snapshot(); routes = snapshot.getRoutes(); count += newCount; assertTrue(routes.size() == count); assertTrue(BasicRandomRoutingTable.isValidSnapshot(snapshot)); } }