Skip to content

Commit

Permalink
Remove excessive Test Resources output (#1173)
Browse files Browse the repository at this point in the history
* Remove excessive Test Resources output

* Tests don't need to check for a log line since later they actually verify that the port is not bound any more

* Tests don't need to check for a log line since later they actually verify that the port is not bound any more
  • Loading branch information
alvarosanchez authored Aug 2, 2024
1 parent 28ccd2e commit 58ab935
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("BUILD SUCCESS") : "Build did not succeed"
assert log.text.contains("Starting Micronaut Test Resources service") : "Test Resources service was not started"
assert log.text.contains("Shutting down Micronaut Test Resources service") : "Test Resources service was not shutdown"
assert !log.text.contains("Test Resources is configured in shared mode") : "Test Resources was configured in shared mode"

String port = new File(basedir, "target/test-resources-port.txt").text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("BUILD FAILURE")
assert log.text.contains("Starting Micronaut Test Resources service")
assert log.text.contains("Shutting down Micronaut Test Resources service")
assert !log.text.contains("Test Resources is configured in shared mode")

String port = new File(basedir, "target/test-resources-port.txt").text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("BUILD SUCCESS") : "Build did not succeed"
assert log.text.contains("Starting Micronaut Test Resources service") : "Test Resources service was not started"
assert log.text.contains("Shutting down Micronaut Test Resources service") : "Test Resources service was not shutdown"
assert log.text.contains("Container postgres:latest started") : "postgres container was not started"
assert log.text.contains("Startup completed in") : "Startup was not completed"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ assert log.exists()
assert log.text.contains("BUILD SUCCESS") : "Build did not succeed"
assert log.text.contains("Test Resources is configured in shared mode with the namespace: my-namespace") : "Test Resources was not configured in shared mode"
assert log.text.contains("Starting Micronaut Test Resources service") : "Test Resources service was not started"
assert log.text.contains("Shutting down Micronaut Test Resources service") : "Test Resources service was not shutdown"

String port = new File(basedir, "target/test-resources-port.txt").text
try (ServerSocket s = new ServerSocket(port as int)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ assert log.exists()
assert log.text.contains("BUILD SUCCESS") : "Build did not succeed"
assert log.text.contains("Test Resources is configured in shared mode") : "Test Resources was not configured in shared mode"
assert log.text.contains("Starting Micronaut Test Resources service") : "Test Resources service was not started"
assert log.text.contains("Shutting down Micronaut Test Resources service") : "Test Resources service was not shutdown"

String port = new File(basedir, "target/test-resources-port.txt").text
try (ServerSocket s = new ServerSocket(port as int)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ assert log.exists()
assert log.text.contains("BUILD SUCCESS")

String port = new File(basedir, "target/test-resources-port.txt").text
assert log.text.contains("Test resources service already started on port " + port)
assert log.text.contains("Shutting down Micronaut Test Resources service")

try (ServerSocket s = new ServerSocket(port as int)) {
assert s != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("BUILD SUCCESS") : "Build did not succeed"
assert log.text.contains("Starting Micronaut Test Resources service") : "Test Resources service was not started"
assert log.text.contains("Shutting down Micronaut Test Resources service") : "Test Resources service was not shutdown"
assert !log.text.contains("Test Resources is configured in shared mode") : "Test Resources was configured in shared mode"

String port = new File(basedir, "target/test-resources-port.txt").text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ private void maybeStartTestResourcesServer() throws MojoExecutionException {
}

private void maybeStopTestResourcesServer() throws MojoExecutionException {
testResourcesHelper.stop();
testResourcesHelper.stop(false);
}

private boolean compileProject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final void execute() throws MojoExecutionException {
dependencyResolutionService, toolchainManager, testResourcesVersion,
classpathInference, testResourcesDependencies, sharedServerNamespace,
debugServer);
helper.stop();
helper.stop(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.maven.testresources;

import io.micronaut.core.io.socket.SocketUtils;
import io.micronaut.maven.services.DependencyResolutionService;
import io.micronaut.testresources.buildtools.MavenDependency;
import io.micronaut.testresources.buildtools.ServerFactory;
Expand Down Expand Up @@ -273,30 +274,48 @@ private List<MavenDependency> getApplicationDependencies() {

/**
* Contains the logic to stop the Test Resources Service.
*
* @param quiet Whether to perform logging or not.
*/
public void stop() throws MojoExecutionException {
public void stop(boolean quiet) throws MojoExecutionException {
if (!enabled) {
return;
}
if (isKeepAlive()) {
log.info("Keeping Micronaut Test Resources service alive");
log("Keeping Micronaut Test Resources service alive", quiet);
return;
}
try {
Optional<ServerSettings> optionalServerSettings = ServerUtils.readServerSettings(getServerSettingsDirectory());
if (optionalServerSettings.isPresent() && ServerUtils.isServerStarted(optionalServerSettings.get().getPort())) {
log.info("Shutting down Micronaut Test Resources service");
if (optionalServerSettings.isPresent() && isServerStarted(optionalServerSettings.get().getPort())) {
log("Shutting down Micronaut Test Resources service", quiet);
doStop();
} else {
if (log.isDebugEnabled()) {
log.debug("Cannot find Micronaut Test Resources service settings, server may already be shutdown.");
}
log("Cannot find Micronaut Test Resources service settings, server may already be shutdown", quiet);
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to stop test resources server", e);
}
}

private static boolean isServerStarted(int port) {
if (System.getProperty("test.resources.internal.server.started") != null) {
return Boolean.getBoolean("test.resources.internal.server.started");
} else {
return !SocketUtils.isTcpPortAvailable(port);
}
}

private void log(String message, boolean quiet) {
if (quiet) {
if (log.isDebugEnabled()) {
log.debug(message);
}
} else {
log.info(message);
}
}

private void doStop() throws IOException, MojoExecutionException {
try {
Path settingsDirectory = getServerSettingsDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void afterSessionEnd(MavenSession session) {
helper.setSharedServerNamespace(sharedServerNamespace);
}
try {
helper.stop();
helper.stop(true);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
Expand Down

0 comments on commit 58ab935

Please sign in to comment.