Skip to content

Commit

Permalink
Allow running Test Resources as a non-daemon process (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosanchez authored Oct 29, 2024
1 parent d80080f commit 356bb08
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,10 @@ protected final void initialize() {
}
}
this.targetDirectory = new File(runnableProject.getBuild().getDirectory());
this.testResourcesHelper = new TestResourcesHelper(testResourcesEnabled,
shared,
buildDirectory,
explicitPort,
clientTimeout,
serverIdleTimeoutMinutes,
runnableProject,
mavenSession,
dependencyResolutionService,
toolchainManager,
testResourcesVersion,
classpathInference,
testResourcesDependencies,
sharedServerNamespace,
debugServer);
this.testResourcesHelper = new TestResourcesHelper(testResourcesEnabled, shared, buildDirectory, explicitPort,
clientTimeout, serverIdleTimeoutMinutes, runnableProject, mavenSession, dependencyResolutionService,
toolchainManager, testResourcesVersion, classpathInference, testResourcesDependencies,
sharedServerNamespace, debugServer, false);
resolveDependencies();
if (watches == null) {
watches = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DefaultServerFactory implements ServerFactory {
private final AtomicBoolean serverStarted;
private final String testResourcesVersion;
private final boolean debugServer;
private final boolean foreground;

private Process process;

Expand All @@ -52,13 +53,15 @@ public DefaultServerFactory(Log log,
MavenSession mavenSession,
AtomicBoolean serverStarted,
String testResourcesVersion,
boolean debugServer) {
boolean debugServer,
boolean foreground) {
this.log = log;
this.toolchainManager = toolchainManager;
this.mavenSession = mavenSession;
this.serverStarted = serverStarted;
this.testResourcesVersion = testResourcesVersion;
this.debugServer = debugServer;
this.foreground = foreground;
}

@Override
Expand Down Expand Up @@ -94,6 +97,13 @@ public void startServer(ServerUtils.ProcessParameters processParameters) {
var builder = new ProcessBuilder(cli);
try {
process = builder.inheritIO().start();
if (foreground) {
log.info("Test Resources Service started in foreground. Press Ctrl+C to stop.");
process.waitFor();
}
} catch (InterruptedException e) {
log.error("Failed to start server", e);
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("Failed to start server", e);
serverStarted.set(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ public StartTestResourcesServerMojo(MavenProject mavenProject,

@Override
public final void execute() throws MojoExecutionException {
var helper = new TestResourcesHelper(
testResourcesEnabled, shared, buildDirectory,
explicitPort, clientTimeout, serverIdleTimeoutMinutes, mavenProject, mavenSession,
dependencyResolutionService, toolchainManager, testResourcesVersion,
classpathInference, testResourcesDependencies, sharedServerNamespace,
debugServer);
var helper = new TestResourcesHelper(testResourcesEnabled, shared, buildDirectory, explicitPort, clientTimeout,
serverIdleTimeoutMinutes, mavenProject, mavenSession, dependencyResolutionService, toolchainManager,
testResourcesVersion, classpathInference, testResourcesDependencies, sharedServerNamespace, debugServer,
foreground);
helper.start();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ public StopTestResourcesServerMojo(MavenProject mavenProject, MavenSession maven

@Override
public final void execute() throws MojoExecutionException {
var helper = new TestResourcesHelper(testResourcesEnabled, shared, buildDirectory,
explicitPort, clientTimeout, serverIdleTimeoutMinutes, mavenProject, mavenSession,
dependencyResolutionService, toolchainManager, testResourcesVersion,
classpathInference, testResourcesDependencies, sharedServerNamespace,
debugServer);
var helper = new TestResourcesHelper(testResourcesEnabled, shared, buildDirectory, explicitPort, clientTimeout,
serverIdleTimeoutMinutes, mavenProject, mavenSession, dependencyResolutionService, toolchainManager,
testResourcesVersion, classpathInference, testResourcesDependencies, sharedServerNamespace, debugServer,
foreground);
helper.stop(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public class TestResourcesConfiguration {
@Parameter(property = CONFIG_PROPERTY_PREFIX + "debug-server", defaultValue = DISABLED)
protected boolean debugServer;

/**
* Whether the test resources server should be started in the foreground.
*
* @since 4.7.0
*/
@Parameter(property = CONFIG_PROPERTY_PREFIX + "foreground", defaultValue = DISABLED)
protected boolean foreground;

/**
* @return Whether to enable or disable Micronaut test resources support.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public class TestResourcesHelper {

private boolean debugServer;

private boolean foreground = false;

public TestResourcesHelper(boolean enabled,
boolean shared,
File buildDirectory,
Expand All @@ -111,7 +113,8 @@ public TestResourcesHelper(boolean enabled,
boolean classpathInference,
List<Dependency> testResourcesDependencies,
String sharedServerNamespace,
boolean debugServer) {
boolean debugServer,
boolean foreground) {
this(mavenSession, enabled, shared, buildDirectory);
this.explicitPort = explicitPort;
this.clientTimeout = clientTimeout;
Expand All @@ -124,6 +127,7 @@ public TestResourcesHelper(boolean enabled,
this.testResourcesDependencies = testResourcesDependencies;
this.sharedServerNamespace = sharedServerNamespace;
this.debugServer = debugServer;
this.foreground = foreground;
}

public TestResourcesHelper(MavenSession mavenSession, boolean enabled, boolean shared, File buildDirectory) {
Expand Down Expand Up @@ -164,7 +168,7 @@ private void doStart() throws IOException {
Path buildDir = buildDirectory.toPath();
Path serverSettingsDirectory = getServerSettingsDirectory();
var serverStarted = new AtomicBoolean(false);
var serverFactory = new DefaultServerFactory(log, toolchainManager, mavenSession, serverStarted, testResourcesVersion, debugServer);
var serverFactory = new DefaultServerFactory(log, toolchainManager, mavenSession, serverStarted, testResourcesVersion, debugServer, foreground);
Optional<ServerSettings> optionalServerSettings = startOrConnectToExistingServer(accessToken, buildDir, serverSettingsDirectory, serverFactory);
if (optionalServerSettings.isPresent()) {
ServerSettings serverSettings = optionalServerSettings.get();
Expand Down

0 comments on commit 356bb08

Please sign in to comment.