Move seed note running out of user thread, still locking

This commit is contained in:
Ivan Vilata-i-Balaguer 2016-04-22 13:08:00 +02:00
parent a9d0272e88
commit 0977ef398b

View file

@ -16,7 +16,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@ -26,65 +29,55 @@ public class NetworkStressTest {
@Before
public void setup() throws IOException, InterruptedException {
// Use an executor that uses a single daemon thread.
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(this.getClass().getSimpleName())
.setDaemon(true)
.build();
UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
tempDir = createTempDirectory();
seedNode = new SeedNode(tempDir.toString());
final NodeAddress seedNodeAddress = new NodeAddress("localhost:8002");
final Set<NodeAddress> seedNodes = new HashSet<>(1);
seedNodes.add(seedNodeAddress); // the only seed node in tests
// Use as a barrier to wait for concurrent tasks.
final CountDownLatch latch = new CountDownLatch(1 /*seed node*/);
// Start the seed node.
final NodeAddress seedNodeAddress = new NodeAddress("localhost:8002");
UserThread.execute(() -> {
try {
seedNode.createAndStartP2PService(seedNodeAddress, true /*localhost*/,
2 /*regtest*/, false /*detailed logging*/, null /*seed nodes*/,
new P2PServiceListener() {
@Override
public void onRequestingDataCompleted() {
// do nothing
}
seedNode.createAndStartP2PService(seedNodeAddress, true /*localhost*/,
2 /*regtest*/, true /*detailed logging*/, seedNodes,
new P2PServiceListener() {
@Override
public void onRequestingDataCompleted() {
// do nothing
}
@Override
public void onNoSeedNodeAvailable() {
// expected, do nothing
}
@Override
public void onNoSeedNodeAvailable() {
// expected, do nothing
}
@Override
public void onNoPeersAvailable() {
// expected, do nothing
}
@Override
public void onNoPeersAvailable() {
// expected, do nothing
}
@Override
public void onBootstrapComplete() {
latch.countDown(); // one less task to wait on
}
@Override
public void onBootstrapComplete() {
// do nothing
}
@Override
public void onTorNodeReady() {
// do nothing
}
@Override
public void onTorNodeReady() {
// do nothing
System.out.println("TOR NODE READY");
}
@Override
public void onHiddenServicePublished() {
// do nothing
}
@Override
public void onHiddenServicePublished() {
latch.countDown(); // one less task to wait on
}
@Override
public void onSetupFailed(Throwable throwable) {
//XXXX
}
});
} catch (Throwable t) {
//log.error("Executing task failed. " + t.getMessage());
t.printStackTrace();
}
});
@Override
public void onSetupFailed(Throwable throwable) {
//XXXX
}
}
);
// Wait for concurrent tasks to finish.
latch.await();
@ -96,7 +89,7 @@ public class NetworkStressTest {
final CountDownLatch latch = new CountDownLatch(1 /*seed node*/);
// Stop the seed node.
if (seedNode != null) {
seedNode.shutDown(() -> {latch.countDown();});
seedNode.shutDown(latch::countDown);
}
// Wait for concurrent tasks to finish.
latch.await();