Skip to content

Commit

Permalink
Shutdown vertx instance created within BesuCommand so BesuCommandTest…
Browse files Browse the repository at this point in the history
… doesn't leak file descriptors. (hyperledger#209)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
Signed-off-by: edwardmack <ed@edwardmack.com>
  • Loading branch information
ajsutton authored and edwardmack committed Feb 4, 2020
1 parent 83125e0 commit a6ca12f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ private void synchronize(
final ObservableMetricsSystem metricsSystem = this.metricsSystem.get();
final Runner runner =
runnerBuilder
.vertx(Vertx.vertx(createVertxOptions(metricsSystem)))
.vertx(createVertx(createVertxOptions(metricsSystem)))
.besuController(controller)
.p2pEnabled(p2pEnabled)
.natMethod(natMethod)
Expand Down Expand Up @@ -1483,6 +1483,10 @@ private void synchronize(
runner.awaitStop();
}

protected Vertx createVertx(final VertxOptions vertxOptions) {
return Vertx.vertx(vertxOptions);
}

private VertxOptions createVertxOptions(final MetricsSystem metricsSystem) {
return new VertxOptions()
.setMetricsOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -100,6 +107,8 @@ public abstract class CommandTestAbstract {
private final PrintStream errPrintStream = new PrintStream(commandErrorOutput);
private final HashMap<String, String> environment = new HashMap<>();

private final List<TestBesuCommand> besuCommands = new ArrayList<>();

@Mock protected RunnerBuilder mockRunnerBuilder;
@Mock protected Runner mockRunner;

Expand Down Expand Up @@ -229,6 +238,7 @@ public void displayOutput() throws IOException {

errPrintStream.close();
commandErrorOutput.close();
besuCommands.forEach(TestBesuCommand::close);
}

protected void setEnvironemntVariable(final String name, final String value) {
Expand Down Expand Up @@ -270,6 +280,7 @@ private TestBesuCommand parseCommand(
mockBesuPluginContext,
environment,
storageService);
besuCommands.add(besuCommand);

besuCommand.setBesuConfiguration(commonPluginConfiguration);

Expand All @@ -287,6 +298,7 @@ public static class TestBesuCommand extends BesuCommand {

@CommandLine.Spec CommandLine.Model.CommandSpec spec;
private final PublicKeySubCommand.KeyLoader keyLoader;
private Vertx vertx;

@Override
protected PublicKeySubCommand.KeyLoader getKeyLoader() {
Expand Down Expand Up @@ -322,6 +334,12 @@ protected void validateP2PInterface(final String p2pInterface) {
// For testing, don't actually query for networking interfaces to validate this option
}

@Override
protected Vertx createVertx(final VertxOptions vertxOptions) {
vertx = super.createVertx(vertxOptions);
return vertx;
}

public CommandSpec getSpec() {
return spec;
}
Expand Down Expand Up @@ -349,5 +367,13 @@ public TransactionPoolOptions getTransactionPoolOptions() {
public MetricsCLIOptions getMetricsCLIOptions() {
return metricsCLIOptions;
}

public void close() {
if (vertx != null) {
final AtomicBoolean closed = new AtomicBoolean(false);
vertx.close(event -> closed.set(true));
Awaitility.waitAtMost(30, TimeUnit.SECONDS).until(closed::get);
}
}
}
}

0 comments on commit a6ca12f

Please sign in to comment.