Skip to content

Commit

Permalink
Consider resource arguments when ordering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Nov 4, 2024
1 parent 4b6d352 commit ac87489
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public TestResourceManager(Class<?> testClass,
this.testResourceComparisonInfo = new HashSet<>();
for (TestResourceClassEntry uniqueEntry : uniqueEntries) {
testResourceComparisonInfo.add(new TestResourceComparisonInfo(
uniqueEntry.testResourceLifecycleManagerClass().getName(), uniqueEntry.getScope()));
uniqueEntry.testResourceLifecycleManagerClass().getName(), uniqueEntry.getScope(), uniqueEntry.args));
}

Set<TestResourceClassEntry> remainingUniqueEntries = initParallelTestResources(uniqueEntries);
Expand Down Expand Up @@ -326,7 +326,8 @@ public static Set<TestResourceManager.TestResourceComparisonInfo> testResourceCo
}
Set<TestResourceManager.TestResourceComparisonInfo> result = new HashSet<>(uniqueEntries.size());
for (TestResourceClassEntry entry : uniqueEntries) {
result.add(new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope()));
result.add(new TestResourceComparisonInfo(entry.testResourceLifecycleManagerClass().getName(), entry.getScope(),
entry.args));
}
return result;
}
Expand Down Expand Up @@ -518,7 +519,7 @@ public static boolean testResourcesRequireReload(Set<TestResourceComparisonInfo>
return false;
}

if (hasRestrictedToClassScope(existing) || hasRestrictedToClassScope(next)) {
if (anyResourceRestrictedToClass(existing) || anyResourceRestrictedToClass(next)) {
return true;
}

Expand All @@ -540,8 +541,8 @@ public static boolean testResourcesRequireReload(Set<TestResourceComparisonInfo>
return false;
}

private static boolean hasRestrictedToClassScope(Set<TestResourceComparisonInfo> existing) {
for (TestResourceComparisonInfo info : existing) {
private static boolean anyResourceRestrictedToClass(Set<TestResourceComparisonInfo> testResources) {
for (TestResourceComparisonInfo info : testResources) {
if (info.scope == RESTRICTED_TO_CLASS) {
return true;
}
Expand Down Expand Up @@ -605,7 +606,8 @@ public TestResourceScope getScope() {
}
}

public record TestResourceComparisonInfo(String testResourceLifecycleManagerClass, TestResourceScope scope) {
public record TestResourceComparisonInfo(String testResourceLifecycleManagerClass, TestResourceScope scope,
Map<String, String> args) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;
Expand All @@ -22,66 +23,87 @@ public void emptyResources() {
@Test
public void differentCount() {
assertTrue(testResourcesRequireReload(Collections.emptySet(),
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS))));
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of()))));

assertTrue(testResourcesRequireReload(Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)),
assertTrue(testResourcesRequireReload(Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())),
Collections.emptySet()));
}

@Test
public void sameSingleRestrictedToClassResource() {
assertTrue(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS)),
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS))));
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of())),
Set.of(new TestResourceComparisonInfo("test", RESTRICTED_TO_CLASS, Map.of()))));
}

@Test
public void sameSingleMatchingResource() {
assertFalse(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES)),
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES))));
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())),
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()))));
}

@Test
public void sameSingleMatchingResourceWithArgs() {
assertFalse(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))),
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b")))));
}

@Test
public void sameSingleResourceDifferentArgs() {
assertTrue(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())),
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b")))));
}

@Test
public void sameSingleResourceDifferentArgValues() {
assertTrue(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "b"))),
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of("a", "c")))));
}

@Test
public void differentSingleMatchingResource() {
assertTrue(testResourcesRequireReload(
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES)),
Set.of(new TestResourceComparisonInfo("test2", MATCHING_RESOURCES))));
Set.of(new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of())),
Set.of(new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()))));
}

@Test
public void sameMultipleMatchingResource() {
assertFalse(testResourcesRequireReload(
Set.of(
new TestResourceComparisonInfo("test", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test3", GLOBAL)),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test", MATCHING_RESOURCES))));
new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test3", GLOBAL, Map.of())),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()))));
}

@Test
public void differentMultipleMatchingResource() {
assertTrue(testResourcesRequireReload(
Set.of(
new TestResourceComparisonInfo("test", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test3", GLOBAL)),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES))));
new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test3", GLOBAL, Map.of())),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES, Map.of()))));
}

@Test
public void differentGlobalMultipleMatchingResource() {
assertTrue(testResourcesRequireReload(
Set.of(
new TestResourceComparisonInfo("test", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("test4", GLOBAL)),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES),
new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES))));
new TestResourceComparisonInfo("test", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("test4", GLOBAL, Map.of())),
Set.of(new TestResourceComparisonInfo("test3", GLOBAL, Map.of()),
new TestResourceComparisonInfo("test2", MATCHING_RESOURCES, Map.of()),
new TestResourceComparisonInfo("TEST", MATCHING_RESOURCES, Map.of()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ static Set<TestResourceManager.TestResourceComparisonInfo> testResourceCompariso
if (originalTestResourceScope != null) {
testResourceScope = TestResourceScope.valueOf(originalTestResourceScope.toString());
}
Object originalArgs = entry.getClass().getMethod("args").invoke(entry);
Map<String, String> args = (Map<String, String>) originalArgs;
result.add(new TestResourceManager.TestResourceComparisonInfo(testResourceLifecycleManagerClass,
testResourceScope));
testResourceScope, args));
}

return result;
Expand Down

0 comments on commit ac87489

Please sign in to comment.