Skip to content

Commit

Permalink
Merge pull request #45253 from gsmet/assorted-withtestresource-fixes
Browse files Browse the repository at this point in the history
Assorted @WithTestResource fixes
  • Loading branch information
geoand authored Dec 24, 2024
2 parents f253d80 + 48d51cd commit a5343b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
14 changes: 7 additions & 7 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1224,20 +1224,20 @@ public @interface WithRepeatableTestResource {
}
----

=== Usage of `@WithTestResources`
=== Usage of `@WithTestResource`

While test resources provided by `@QuarkusTestResource` are available either globally or restricted to the annotated test class (`restrictToAnnotatedClass`), the annotation `@WithTestResources` allows to additionally group tests by test resources for execution.
`@WithTestResources` has a `scope` property that takes a `TestResourceScope` enum value:
While test resources provided by `@QuarkusTestResource` are available either globally or restricted to the annotated test class (`restrictToAnnotatedClass`), the annotation `@WithTestResource` allows to additionally group tests by test resources for execution.
`@WithTestResource` has a `scope` property that takes a `TestResourceScope` enum value:

- `TestResourceScope.MATCHING_RESOURCES` (default): Quarkus will group tests with the same test resources and run them together. After a group has been executed, all test resources will be stopped, and the next group will be executed.
- `TestResourceScope.RESTRICTED_TO_CLASS`: The test resource is available only for the annotated test class and will be stopped after the test class has been executed.
- `TestResourceScope.GLOBAL`: Test resources apply to all tests in the testsuite
- `TestResourceScope.GLOBAL`: Test resources apply to all tests in the test suite

Quarkus needs to restart if one of the following is true:

- At least one the existing test resources is restricted to the test class
- At least one the next test resources is restricted to the test class
- Different {@code MATCHING_RESOURCE} scoped test resources are being used
- At least one of the test resources of the current test is restricted to the test class
- At least one of the test resources of the next test is restricted to the test class
- Different `MATCHING_RESOURCES` scoped test resources are being used

== Hang Detection

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public TestResourceManager(Class<?> testClass,

this.testResourceComparisonInfo = new HashSet<>();
for (TestResourceClassEntry uniqueEntry : uniqueEntries) {
testResourceComparisonInfo.add(new TestResourceComparisonInfo(
uniqueEntry.testResourceLifecycleManagerClass().getName(), uniqueEntry.getScope(), uniqueEntry.args));
testResourceComparisonInfo.add(prepareTestResourceComparisonInfo(uniqueEntry));
}

Set<TestResourceClassEntry> remainingUniqueEntries = initParallelTestResources(uniqueEntries);
Expand Down Expand Up @@ -331,7 +330,7 @@ private Set<TestResourceClassEntry> uniqueTestResourceClassEntries(Path testClas
}

/**
* Allows Quarkus to extra basic information about which test resources a test class will require
* Allows Quarkus to extract basic information about which test resources a test class will require
*/
public static Set<TestResourceManager.TestResourceComparisonInfo> testResourceComparisonInfo(Class<?> testClass,
Path testClassLocation, List<TestResourceClassEntry> entriesFromProfile) {
Expand All @@ -343,16 +342,24 @@ public static Set<TestResourceManager.TestResourceComparisonInfo> testResourceCo
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());
}
result.add(new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope(),
args));
result.add(prepareTestResourceComparisonInfo(entry));
}
return result;
}

private static TestResourceComparisonInfo prepareTestResourceComparisonInfo(TestResourceClassEntry entry) {
Map<String, String> args;
if (entry.configAnnotation != null) {
args = new HashMap<>(entry.args);
args.put("configAnnotation", entry.configAnnotation.annotationType().getName());
} else {
args = entry.args;
}

return new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope(),
args);
}

private static Set<TestResourceClassEntry> getUniqueTestResourceClassEntries(Class<?> testClass,
Path testClassLocation,
Consumer<Set<TestResourceClassEntry>> afterMetaAnnotationAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ public void orderClasses(ClassOrdererContext context) {
})
.orElseGet(ClassName::new).orderClasses(context);

var classDecriptors = context.getClassDescriptors();
var firstPassIndexMap = IntStream.range(0, classDecriptors.size()).boxed()
.collect(Collectors.toMap(classDecriptors::get, i -> String.format("%06d", i)));
var classDescriptors = context.getClassDescriptors();
var firstPassIndexMap = IntStream.range(0, classDescriptors.size()).boxed()
.collect(Collectors.toMap(classDescriptors::get, i -> String.format("%06d", i)));

// second pass: apply the actual Quarkus aware ordering logic, using the first pass indices as order key suffixes
classDecriptors.sort(Comparator.comparing(classDescriptor -> {
classDescriptors.sort(Comparator.comparing(classDescriptor -> {
var secondaryOrderSuffix = firstPassIndexMap.get(classDescriptor);
Optional<String> customOrderKey = getCustomOrderKey(classDescriptor, context, secondaryOrderSuffix)
.or(() -> getCustomOrderKey(classDescriptor, context));
Expand Down

0 comments on commit a5343b3

Please sign in to comment.