Skip to content

Commit

Permalink
Merge pull request #42862 from radcortez/quarkus-42844
Browse files Browse the repository at this point in the history
Revert "QuarkusTestProfile overrides in a high ordinal application.properties"
  • Loading branch information
gsmet authored Aug 29, 2024
2 parents 2f3ecac + b7e86f9 commit f63e211
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.quarkus.test.common.PathTestHelper.getAppClassLocationForTestLocation;
import static io.quarkus.test.common.PathTestHelper.getTestClassesLocation;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
Expand All @@ -16,7 +15,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand All @@ -42,6 +40,7 @@
import io.quarkus.runtime.LaunchMode;
import io.quarkus.test.common.PathTestHelper;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.RestorableSystemProperties;
import io.quarkus.test.common.TestClassIndexer;
import io.quarkus.test.common.WithTestResource;

Expand Down Expand Up @@ -148,67 +147,31 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend

// clear the test.url system property as the value leaks into the run when using different profiles
System.clearProperty("test.url");
Map<String, String> additional = new HashMap<>();

QuarkusTestProfile profileInstance = null;
if (profile != null) {
profileInstance = profile.getConstructor().newInstance();

Map<String, String> overrides = new HashMap<>(profileInstance.getConfigOverrides());
additional.putAll(profileInstance.getConfigOverrides());
if (!profileInstance.getEnabledAlternatives().isEmpty()) {
overrides.put("quarkus.arc.selected-alternatives",
profileInstance.getEnabledAlternatives()
.stream()
.peek((c) -> {
if (!c.isAnnotationPresent(Alternative.class)) {
throw new RuntimeException(
"Enabled alternative " + c + " is not annotated with " +
"@Alternative");
}
})
.map(Class::getName)
.collect(Collectors.joining(",")));
additional.put("quarkus.arc.selected-alternatives", profileInstance.getEnabledAlternatives().stream()
.peek((c) -> {
if (!c.isAnnotationPresent(Alternative.class)) {
throw new RuntimeException(
"Enabled alternative " + c + " is not annotated with @Alternative");
}
})
.map(Class::getName).collect(Collectors.joining(",")));
}
if (profileInstance.disableApplicationLifecycleObservers()) {
overrides.put("quarkus.arc.test.disable-application-lifecycle-observers", "true");
additional.put("quarkus.arc.test.disable-application-lifecycle-observers", "true");
}
if (profileInstance.getConfigProfile() != null) {
overrides.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile());
}

// Creates a temporary application.properties file for the test with a high ordinal (build and runtime)
// Note that in the case of the Quarkus Platform, the testClassLocation is actually a jar so we can't
// create a temp directory in it.
Path tempDirectory;
if (Files.isDirectory(testClassLocation) && Files.isWritable(testClassLocation)) {
tempDirectory = Files.createTempDirectory(testClassLocation, requiredTestClass.getSimpleName());
} else {
tempDirectory = Files.createTempDirectory(requiredTestClass.getSimpleName());
additional.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile());
}

Path propertiesFile = tempDirectory.resolve("application.properties");
Files.createFile(propertiesFile);
Properties properties = new Properties();
// TODO - radcortez - This should be higher that system properties, but configuration like ports is being
// passed around using system properties, meaning that it cannot be overridden. We cannot use system
// properties to carry that information. See io.quarkus.vertx.http.runtime.PortSystemProperties
properties.put("config_ordinal", "399");
properties.putAll(overrides);
try (FileOutputStream outputStream = new FileOutputStream(propertiesFile.toFile())) {
properties.store(outputStream, "");
}
addToBuilderIfConditionMet.accept(tempDirectory);

shutdownTasks.add(new Runnable() {
@Override
public void run() {
try {
Files.deleteIfExists(propertiesFile);
Files.deleteIfExists(tempDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
//we just use system properties for now
//it's a lot simpler
shutdownTasks.add(RestorableSystemProperties.setProperties(additional)::close);
}

CuratedApplication curatedApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ private void ensurePrepared(ExtensionContext extensionContext, Class<? extends Q
prepareResult = null;
}
if (prepareResult == null) {
LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
prepareResult = createAugmentor(extensionContext, profile, shutdownTasks);
final LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
PrepareResult result = createAugmentor(extensionContext, profile, shutdownTasks);
prepareResult = result;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public Thread newThread(Runnable r) {
}
hangTimeout = new DurationConverter().convert(time);
hangTaskKey = hangDetectionExecutor.schedule(hangDetectionTask, hangTimeout.toMillis(), TimeUnit.MILLISECONDS);
quarkusTestProfile = profile;

LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
quarkusTestProfile = profile;
Class<?> requiredTestClass = context.getRequiredTestClass();
Closeable testResourceManager = null;
try {
final LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
PrepareResult result = createAugmentor(context, profile, shutdownTasks);
AugmentAction augmentAction = result.augmentAction;
QuarkusTestProfile profileInstance = result.profileInstance;
Expand Down Expand Up @@ -298,7 +298,8 @@ public void close() throws IOException {
}
}
};
return new ExtensionState(testResourceManager, shutdownTask);
ExtensionState state = new ExtensionState(testResourceManager, shutdownTask);
return state;
} catch (Throwable e) {
if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) {
activateLogging();
Expand All @@ -314,16 +315,6 @@ public void close() throws IOException {
effectiveException.addSuppressed(determineEffectiveException(ex));
}

while (!shutdownTasks.isEmpty()) {
shutdownTasks.pop().run();
}

try {
TestClassIndexer.removeIndex(requiredTestClass);
} catch (Exception ignored) {

}

throw effectiveException;
} finally {
if (originalCl != null) {
Expand Down

0 comments on commit f63e211

Please sign in to comment.