use of io.quarkus.test.common.RestorableSystemProperties in project quarkus by quarkusio.
the class QuarkusTestExtension method doJavaStart.
private ExtensionState doJavaStart(ExtensionContext context, Class<? extends QuarkusTestProfile> profile) throws Throwable {
TracingHandler.quarkusStarting();
hangDetectionExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "Quarkus hang detection timer thread");
}
});
String time = "10m";
// config is not established yet
// we can only read from system properties
String sysPropString = System.getProperty(QUARKUS_TEST_HANG_DETECTION_TIMEOUT);
if (sysPropString != null) {
time = sysPropString;
}
hangTimeout = new DurationConverter().convert(time);
hangTaskKey = hangDetectionExecutor.schedule(hangDetectionTask, hangTimeout.toMillis(), TimeUnit.MILLISECONDS);
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;
testHttpEndpointProviders = TestHttpEndpointProvider.load();
StartupAction startupAction = augmentAction.createInitialRuntimeApplication();
Thread.currentThread().setContextClassLoader(startupAction.getClassLoader());
populateDeepCloneField(startupAction);
// must be done after the TCCL has been set
testResourceManager = (Closeable) startupAction.getClassLoader().loadClass(TestResourceManager.class.getName()).getConstructor(Class.class, Class.class, List.class, boolean.class, Map.class, Optional.class).newInstance(requiredTestClass, profile != null ? profile : null, getAdditionalTestResources(profileInstance, startupAction.getClassLoader()), profileInstance != null && profileInstance.disableGlobalTestResources(), startupAction.getDevServicesProperties(), Optional.empty());
testResourceManager.getClass().getMethod("init", String.class).invoke(testResourceManager, profile != null ? profile.getName() : null);
Map<String, String> properties = (Map<String, String>) testResourceManager.getClass().getMethod("start").invoke(testResourceManager);
startupAction.overrideConfig(properties);
hasPerTestResources = (boolean) testResourceManager.getClass().getMethod("hasPerTestResources").invoke(testResourceManager);
populateCallbacks(startupAction.getClassLoader());
populateTestMethodInvokers(startupAction.getClassLoader());
if (profileInstance == null || !profileInstance.runMainMethod()) {
runningQuarkusApplication = startupAction.run(profileInstance == null ? new String[0] : profileInstance.commandLineParameters());
} else {
Class<?> lifecycleManager = Class.forName(ApplicationLifecycleManager.class.getName(), true, startupAction.getClassLoader());
lifecycleManager.getDeclaredMethod("setDefaultExitCodeHandler", Consumer.class).invoke(null, (Consumer<Integer>) integer -> {
});
runningQuarkusApplication = startupAction.runMainClass(profileInstance.commandLineParameters());
}
String patternString = runningQuarkusApplication.getConfigValue("quarkus.test.class-clone-pattern", String.class).orElse("java\\..*");
clonePattern = Pattern.compile(patternString);
TracingHandler.quarkusStarted();
if (hangTaskKey != null) {
hangTaskKey.cancel(false);
hangTimeout = runningQuarkusApplication.getConfigValue(QUARKUS_TEST_HANG_DETECTION_TIMEOUT, Duration.class).orElse(Duration.of(10, ChronoUnit.MINUTES));
hangTaskKey = hangDetectionExecutor.schedule(hangDetectionTask, hangTimeout.toMillis(), TimeUnit.MILLISECONDS);
}
ConfigProviderResolver.setInstance(new RunningAppConfigResolver(runningQuarkusApplication));
RestorableSystemProperties restorableSystemProperties = RestorableSystemProperties.setProperties(Collections.singletonMap("test.url", TestHTTPResourceManager.getUri(runningQuarkusApplication)));
Closeable tm = testResourceManager;
Closeable shutdownTask = new Closeable() {
@Override
public void close() throws IOException {
TracingHandler.quarkusStopping();
try {
runningQuarkusApplication.close();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
TracingHandler.quarkusStopped();
try {
while (!shutdownTasks.isEmpty()) {
shutdownTasks.pop().run();
}
} finally {
try {
tm.close();
} finally {
restorableSystemProperties.close();
GroovyCacheCleaner.clearGroovyCache();
shutdownHangDetection();
}
}
try {
TestClassIndexer.removeIndex(requiredTestClass);
} catch (Exception ignored) {
}
}
}
};
ExtensionState state = new ExtensionState(testResourceManager, shutdownTask);
return state;
} catch (Throwable e) {
if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) {
activateLogging();
}
try {
if (testResourceManager != null) {
testResourceManager.close();
}
} catch (Exception ex) {
e.addSuppressed(ex);
}
throw e;
} finally {
if (originalCl != null) {
Thread.currentThread().setContextClassLoader(originalCl);
}
}
}
Aggregations