private void testRoutingLoop(boolean tcp) throws Exception {
final Random rnd = new Random(43L);
for (int k = 0; k < 100; k++) {
Number160 find = Number160.createHash("findme");
Peer master = null;
ChannelCreator cc = null;
try {
System.err.println("round " + k);
// setup
Peer[] peers = Utils2.createNodes(200, rnd, 4001);
master = peers[0];
Utils2.perfectRouting(peers);
Comparator<PeerAddress> cmp = PeerMap.createComparator(find);
SortedSet<PeerAddress> ss = new TreeSet<PeerAddress>(cmp);
for (int i = 0; i < peers.length; i++) {
ss.add(peers[i].peerAddress());
}
// do testing
if (tcp) {
FutureChannelCreator fcc = peers[0].connectionBean().reservation().create(0, 2);
fcc.awaitUninterruptibly();
cc = fcc.channelCreator();
} else {
FutureChannelCreator fcc = peers[0].connectionBean().reservation().create(2, 0);
fcc.awaitUninterruptibly();
cc = fcc.channelCreator();
}
RoutingBuilder routingBuilder = new RoutingBuilder();
if (tcp) {
routingBuilder.forceTCP(true);
}
routingBuilder.locationKey(find);
routingBuilder.maxDirectHits(Integer.MAX_VALUE);
routingBuilder.setMaxNoNewInfo(5);
routingBuilder.maxFailures(10);
routingBuilder.maxSuccess(20);
routingBuilder.parallel(2);
FutureRouting frr = peers[50].distributedRouting().route(routingBuilder, Type.REQUEST_1,
cc);
frr.awaitUninterruptibly();
SortedSet<PeerAddress> ss2 = frr.potentialHits();
// test the first 5 peers, because we set noNewInformation to 5,
// which means we find at least 5 entries.
for (int i = 0; i < 5; i++) {
PeerAddress pa = ss.first();
PeerAddress pa2 = ss2.first();
System.err.println("test " + pa + " - " + pa2);
Assert.assertEquals(pa.peerId(), pa2.peerId());
ss.remove(pa);
ss2.remove(pa2);
}
} finally {
if (cc != null) {
cc.shutdown().awaitListenersUninterruptibly();
}
if (master != null) {
master.shutdown().awaitListenersUninterruptibly();
}
}