Skip to content

Commit 2dbe4f6

Browse files
authored
Kill abandoned external servers started during Yaml Tests (#3152)
This adds a static constructor that will find all processes matching the directory for external servers and kill them. This is most important when running/debugging locally, if you stop the debugger, or SIGKILL, there will be a bunch of processes left behind. This changes it so that we will kill them the next time you run the test.
1 parent 21b3330 commit 2dbe4f6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

yaml-tests/src/main/java/com/apple/foundationdb/relational/yamltests/YamlTestExtension.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.apple.foundationdb.relational.yamltests.configs.YamlTestConfig;
3232
import com.apple.foundationdb.relational.yamltests.server.ExternalServer;
3333
import com.google.common.collect.Iterables;
34-
import com.google.common.collect.Streams;
3534
import org.apache.logging.log4j.LogManager;
3635
import org.apache.logging.log4j.Logger;
3736
import org.junit.jupiter.api.Assertions;
@@ -109,14 +108,19 @@ public void beforeAll(final ExtensionContext context) throws Exception {
109108
@SuppressWarnings("PMD.UnnecessaryLocalBeforeReturn") // It complains about the local variable `e` being returned
110109
public void afterAll(final ExtensionContext context) throws Exception {
111110
final Optional<Exception> exception =
112-
Streams.stream(Iterables.concat(testConfigs, maintainConfigs)).map(config -> {
113-
try {
114-
config.afterAll();
115-
return null;
116-
} catch (Exception e) {
117-
return e;
118-
}
119-
}).filter(Objects::nonNull).findFirst();
111+
Stream.concat(
112+
// if beforeAll fails in certain places, or isn't run, testConfigs and/or maintainConfigs could
113+
// be null
114+
Objects.requireNonNullElse(testConfigs, List.<YamlTestConfig>of()).stream(),
115+
Objects.requireNonNullElse(maintainConfigs, List.<YamlTestConfig>of()).stream())
116+
.map(config -> {
117+
try {
118+
config.afterAll();
119+
return null;
120+
} catch (Exception e) {
121+
return e;
122+
}
123+
}).filter(Objects::nonNull).findFirst();
120124
for (ExternalServer server : servers) {
121125
try {
122126
server.stop();

yaml-tests/src/main/java/com/apple/foundationdb/relational/yamltests/server/ExternalServer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.File;
2828
import java.io.IOException;
29+
import java.util.Arrays;
2930
import java.util.List;
3031
import java.util.Objects;
3132
import java.util.jar.Attributes;
@@ -66,6 +67,25 @@ public ExternalServer(File serverJar, final int grpcPort, final int httpPort) {
6667
this.httpPort = httpPort;
6768
}
6869

70+
static {
71+
final String serverPath = Objects.requireNonNull(System.getProperty(EXTERNAL_SERVER_PROPERTY_NAME));
72+
// kill all existing processes
73+
ProcessHandle.allProcesses().filter(process ->
74+
process.info().arguments().map(arguments ->
75+
Arrays.stream(arguments).anyMatch(argument ->
76+
argument.startsWith(serverPath)))
77+
.orElse(false) &&
78+
process.info().command().map(command ->
79+
command.endsWith("/java"))
80+
.orElse(false))
81+
.forEach(process -> {
82+
if (logger.isInfoEnabled()) {
83+
logger.info("Killing existing server: pid=" + process.pid() + " " + process.info());
84+
}
85+
process.destroy();
86+
});
87+
}
88+
6989
/**
7090
* Get the port to use when connecting.
7191
*

0 commit comments

Comments
 (0)