org.junit.jupiter.engine.extension.TimeoutConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-jupiter-engine Show documentation
Show all versions of junit-jupiter-engine Show documentation
Module "junit-jupiter-engine" of JUnit 5.
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.engine.extension;
import static org.junit.jupiter.api.Timeout.DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TIMEOUT_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME;
import static org.junit.jupiter.api.Timeout.ThreadMode.SAME_THREAD;
import static org.junit.jupiter.api.Timeout.ThreadMode.SEPARATE_THREAD;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.junit.jupiter.api.Timeout.ThreadMode;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
/**
* @since 5.5
*/
class TimeoutConfiguration {
private static final Logger logger = LoggerFactory.getLogger(TimeoutConfiguration.class);
private final TimeoutDurationParser parser = new TimeoutDurationParser();
private final Map> cache = new ConcurrentHashMap<>();
private final AtomicReference> threadMode = new AtomicReference<>();
private final ExtensionContext extensionContext;
TimeoutConfiguration(ExtensionContext extensionContext) {
this.extensionContext = extensionContext;
}
Optional getDefaultTestMethodTimeout() {
return parseOrDefault(DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultTestableMethodTimeout);
}
Optional getDefaultTestTemplateMethodTimeout() {
return parseOrDefault(DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME,
this::getDefaultTestableMethodTimeout);
}
Optional getDefaultTestFactoryMethodTimeout() {
return parseOrDefault(DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultTestableMethodTimeout);
}
Optional getDefaultBeforeAllMethodTimeout() {
return parseOrDefault(DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultLifecycleMethodTimeout);
}
Optional getDefaultBeforeEachMethodTimeout() {
return parseOrDefault(DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultLifecycleMethodTimeout);
}
Optional getDefaultAfterEachMethodTimeout() {
return parseOrDefault(DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultLifecycleMethodTimeout);
}
Optional getDefaultAfterAllMethodTimeout() {
return parseOrDefault(DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultLifecycleMethodTimeout);
}
private Optional getDefaultTestableMethodTimeout() {
return parseOrDefault(DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultTimeout);
}
private Optional getDefaultLifecycleMethodTimeout() {
return parseOrDefault(DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME, this::getDefaultTimeout);
}
private Optional getDefaultTimeout() {
return parseTimeoutDuration(DEFAULT_TIMEOUT_PROPERTY_NAME);
}
private Optional parseOrDefault(String propertyName,
Supplier> defaultSupplier) {
Optional timeoutConfiguration = parseTimeoutDuration(propertyName);
return timeoutConfiguration.isPresent() ? timeoutConfiguration : defaultSupplier.get();
}
private Optional parseTimeoutDuration(String propertyName) {
return cache.computeIfAbsent(propertyName, key -> extensionContext.getConfigurationParameter(key).map(value -> {
try {
return parser.parse(value);
}
catch (Exception e) {
logger.warn(e,
() -> String.format("Ignored invalid timeout '%s' set via the '%s' configuration parameter.", value,
key));
return null;
}
}));
}
Optional getDefaultTimeoutThreadMode() {
if (threadMode.get() != null) {
return threadMode.get();
}
else {
Optional configuredThreadMode = parseTimeoutThreadModeConfiguration();
threadMode.set(configuredThreadMode);
return configuredThreadMode;
}
}
private Optional parseTimeoutThreadModeConfiguration() {
return extensionContext.getConfigurationParameter(DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME).map(value -> {
try {
ThreadMode threadMode = ThreadMode.valueOf(value.toUpperCase());
if (threadMode == ThreadMode.INFERRED) {
logger.warn(() -> String.format(
"Invalid timeout thread mode '%s', only %s and %s can be used as configuration parameter for %s.",
value, SAME_THREAD, SEPARATE_THREAD, DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME));
return null;
}
return threadMode;
}
catch (Exception e) {
logger.warn(e,
() -> String.format("Invalid timeout thread mode '%s' set via the '%s' configuration parameter.",
value, DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME));
return null;
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy