Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider resources from @TestProfile when checking for restart #44532

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,15 @@ private Set<TestResourceClassEntry> uniqueTestResourceClassEntries(Path testClas
* Allows Quarkus to extra basic information about which test resources a test class will require
*/
public static Set<TestResourceManager.TestResourceComparisonInfo> testResourceComparisonInfo(Class<?> testClass,
Path testClassLocation) {
Path testClassLocation, List<TestResourceClassEntry> entriesFromProfile) {
Set<TestResourceClassEntry> uniqueEntries = getUniqueTestResourceClassEntries(testClass, testClassLocation, null);
if (uniqueEntries.isEmpty()) {
if (uniqueEntries.isEmpty() && entriesFromProfile.isEmpty()) {
return Collections.emptySet();
}
Set<TestResourceManager.TestResourceComparisonInfo> result = new HashSet<>(uniqueEntries.size());
for (TestResourceClassEntry entry : uniqueEntries) {
Set<TestResourceClassEntry> allEntries = new HashSet<>(uniqueEntries);
allEntries.addAll(entriesFromProfile);
Set<TestResourceManager.TestResourceComparisonInfo> result = new HashSet<>(allEntries.size());
for (TestResourceClassEntry entry : allEntries) {
Map<String, String> args = new HashMap<>(entry.args);
if (entry.configAnnotation != null) {
args.put("configAnnotation", entry.configAnnotation.annotationType().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ private QuarkusTestExtensionState ensureStarted(ExtensionContext extensionContex
// we reload the test resources if we changed test class and if we had or will have per-test test resources
boolean reloadTestResources = false;
if ((state == null && !failedBoot) || wrongProfile || (reloadTestResources = isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
selectedProfile))) {
if (wrongProfile || reloadTestResources) {
if (state != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private void ensurePrepared(ExtensionContext extensionContext, Class<? extends Q
// we reload the test resources if we changed test class and if we had or will have per-test test resources
boolean isNewTestClass = !Objects.equals(extensionContext.getRequiredTestClass(), currentJUnitTestClass);
if (wrongProfile || (isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
profile))) {
if (state != null) {
try {
state.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ private QuarkusTestExtensionState ensureStarted(ExtensionContext extensionContex
// we reload the test resources if we changed test class and the new test class is not a nested class, and if we had or will have per-test test resources
boolean reloadTestResources = false;
if ((state == null && !failedBoot) || wrongProfile || (reloadTestResources = isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
selectedProfile))) {
if (wrongProfile || reloadTestResources) {
if (state != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ private TestResourceUtil() {
* This is where we decide if the test resources of the current state vs the ones required by the next test class
* to be executed require a Quarkus restart.
*/
static boolean testResourcesRequireReload(QuarkusTestExtensionState state, Class<?> nextTestClass) {
static boolean testResourcesRequireReload(QuarkusTestExtensionState state, Class<?> nextTestClass,
Class<? extends QuarkusTestProfile> nextTestClassProfile) {
QuarkusTestProfile profileInstance = null;
if (nextTestClassProfile != null) {
try {
profileInstance = nextTestClassProfile.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Set<TestResourceManager.TestResourceComparisonInfo> existingTestResources = existingTestResources(state);
Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources = nextTestResources(nextTestClass);
Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources = nextTestResources(nextTestClass,
profileInstance);

return TestResourceManager.testResourcesRequireReload(existingTestResources, nextTestResources);
}
Expand All @@ -54,9 +64,20 @@ static Set<TestResourceManager.TestResourceComparisonInfo> existingTestResources
return Collections.emptySet();
}

static Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources(Class<?> requiredTestClass) {
static Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources(Class<?> requiredTestClass,
QuarkusTestProfile profileInstance) {

List<TestResourceManager.TestResourceClassEntry> entriesFromProfile = Collections.emptyList();
if (profileInstance != null) {
entriesFromProfile = new ArrayList<>(profileInstance.testResources().size());
for (QuarkusTestProfile.TestResourceEntry entry : profileInstance.testResources()) {
entriesFromProfile.add(new TestResourceManager.TestResourceClassEntry(entry.getClazz(), entry.getArgs(), null,
entry.isParallel(), TestResourceScope.MATCHING_RESOURCES));
}
}

return TestResourceManager
.testResourceComparisonInfo(requiredTestClass, getTestClassesLocation(requiredTestClass));
.testResourceComparisonInfo(requiredTestClass, getTestClassesLocation(requiredTestClass), entriesFromProfile);
}

/**
Expand Down Expand Up @@ -92,7 +113,7 @@ static <T> List<T> copyEntriesFromProfile(
T instance = (T) testResourceClassEntryConstructor.newInstance(
Class.forName(testResource.getClazz().getName(), true, classLoader), testResource.getArgs(),
null, testResource.isParallel(),
Enum.valueOf(testResourceScopeClass, TestResourceScope.RESTRICTED_TO_CLASS.name()));
Enum.valueOf(testResourceScopeClass, TestResourceScope.MATCHING_RESOURCES.name()));
result.add(instance);
}

Expand Down
Loading