use of net.sergeych.tools.Reporter in project universa by UniversaBlockchain.
the class CLIMainTest method checkTheNetwork.
@Test
public void checkTheNetwork() throws Exception {
Reporter r = callMain("--network", "--verbose");
assertThat(r.getMessage(-1) + r.getMessage(-2) + r.getMessage(-3) + r.getMessage(-4) + r.getMessage(-5), matches("3 node"));
}
use of net.sergeych.tools.Reporter in project universa by UniversaBlockchain.
the class Client method getState.
public ItemResult getState(HashId itemId, Reporter reporter) throws ClientError {
final ExecutorService pool = Executors.newCachedThreadPool();
final List<ErrorRecord> errors = new ArrayList<>();
final AsyncEvent<Void> consensusFound = new AsyncEvent<>();
final int checkConsensus = getNodes().size() / 3;
final AtomicInteger nodesLeft = new AtomicInteger(nodes.size());
return protect(() -> {
final Map<ItemState, List<ItemResult>> states = new HashMap<>();
for (int i = 0; i < nodes.size(); i++) {
final int nn = i;
pool.submit(() -> {
for (int retry = 0; retry < 5; retry++) {
// reporter.verbose("trying "+reporter+" for node "+nn);
try {
Client c = getClient(nn);
ItemResult r = c.command("getState", "itemId", itemId).getOrThrow("itemResult");
r.meta.put("url", c.getNodeNumber());
synchronized (states) {
List<ItemResult> list = states.get(r.state);
if (list == null) {
list = new ArrayList();
states.put(r.state, list);
}
list.add(r);
if (r.errors.size() > 0)
reporter.warning("errors from " + c.getNodeNumber() + ": " + r.errors);
break;
}
} catch (IOException e) {
// reporter.warning("can't get answer from node " + nn + ", retry #" + retry + ": " + e);
}
}
// Now we should check the consensus
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
if (itemResults.size() >= checkConsensus) {
consensusFound.fire();
return;
}
});
if (nodesLeft.decrementAndGet() < 1)
consensusFound.fire();
});
}
consensusFound.await(5000);
pool.shutdownNow();
final ItemResult[] consensus = new ItemResult[1];
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
consensus[0] = itemResults.get(0);
});
if (consensus[0] != null)
reporter.message("State consensus found:" + consensus[0]);
else {
reporter.warning("no consensus found " + states.size());
}
if (states.size() > 1) {
states.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().size())).forEach(kv -> {
List<ItemResult> itemResults = kv.getValue();
reporter.message("" + kv.getKey() + ": " + itemResults.size() + ": " + itemResults.stream().map(x -> x.meta.getStringOrThrow("url")).collect(Collectors.toSet()));
});
}
return consensus[0];
});
}
use of net.sergeych.tools.Reporter in project universa by UniversaBlockchain.
the class ClientNetworkTest method checkNetwork.
// we do moving the network addresses now
@Test
public void checkNetwork() throws Exception {
ClientNetwork n = new ClientNetwork(null);
assertEquals(30, n.size());
ConsoleInterceptor.copyOut(() -> {
int active = n.checkNetworkState(new Reporter());
assertEquals(30, active);
});
}
Aggregations