-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid multiple class-level annotation lookups
Instead of looking up annotations on the declaring and all enclosing classes for each test method, each `ResourceLockAware` test descriptor now delegates to the `ExclusiveResourceCollector` of its ancestors which cache the class-level annotations and `ResourceLocksProvider` instances. Resolves #2677.
- Loading branch information
1 parent
8e9094d
commit d696878
Showing
8 changed files
with
232 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...-engine/src/main/java/org/junit/jupiter/engine/descriptor/ExclusiveResourceCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2015-2024 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.descriptor; | ||
|
||
import static org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations; | ||
import static org.junit.platform.commons.util.CollectionUtils.toUnmodifiableList; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.parallel.ResourceAccessMode; | ||
import org.junit.jupiter.api.parallel.ResourceLock; | ||
import org.junit.jupiter.api.parallel.ResourceLocksProvider; | ||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
import org.junit.platform.commons.util.StringUtils; | ||
import org.junit.platform.engine.support.hierarchical.ExclusiveResource; | ||
|
||
/** | ||
* @since 5.12 | ||
*/ | ||
abstract class ExclusiveResourceCollector { | ||
|
||
private static final ExclusiveResourceCollector NO_EXCLUSIVE_RESOURCES = new ExclusiveResourceCollector() { | ||
|
||
@Override | ||
Stream<ExclusiveResource> getAllExclusiveResources( | ||
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) { | ||
return Stream.empty(); | ||
} | ||
|
||
@Override | ||
public Stream<ExclusiveResource> getStaticResources() { | ||
return Stream.empty(); | ||
} | ||
|
||
@Override | ||
Stream<ExclusiveResource> getDynamicResources( | ||
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) { | ||
return Stream.empty(); | ||
} | ||
}; | ||
|
||
Stream<ExclusiveResource> getAllExclusiveResources( | ||
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) { | ||
return Stream.concat(getStaticResources(), getDynamicResources(providerToLocks)); | ||
} | ||
|
||
abstract Stream<ExclusiveResource> getStaticResources(); | ||
|
||
abstract Stream<ExclusiveResource> getDynamicResources( | ||
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks); | ||
|
||
static ExclusiveResourceCollector from(AnnotatedElement element) { | ||
List<ResourceLock> annotations = findRepeatableAnnotations(element, ResourceLock.class); | ||
return annotations.isEmpty() ? NO_EXCLUSIVE_RESOURCES : new DefaultExclusiveResourceCollector(annotations); | ||
} | ||
|
||
private static class DefaultExclusiveResourceCollector extends ExclusiveResourceCollector { | ||
|
||
private final List<ResourceLock> annotations; | ||
private List<ResourceLocksProvider> providers; | ||
|
||
DefaultExclusiveResourceCollector(List<ResourceLock> annotations) { | ||
this.annotations = annotations; | ||
} | ||
|
||
@Override | ||
public Stream<ExclusiveResource> getStaticResources() { | ||
return annotations.stream() // | ||
.filter(annotation -> StringUtils.isNotBlank(annotation.value())) // | ||
.map(annotation -> new ExclusiveResource(annotation.value(), toLockMode(annotation.mode()))); | ||
} | ||
|
||
@Override | ||
Stream<ExclusiveResource> getDynamicResources( | ||
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) { | ||
List<ResourceLocksProvider> providers = getProviders(); | ||
if (providers.isEmpty()) { | ||
return Stream.empty(); | ||
} | ||
return providers.stream() // | ||
.map(providerToLocks) // | ||
.flatMap(Collection::stream) // | ||
.map(lock -> new ExclusiveResource(lock.getKey(), toLockMode(lock.getAccessMode()))); | ||
} | ||
|
||
private List<ResourceLocksProvider> getProviders() { | ||
if (this.providers == null) { | ||
this.providers = annotations.stream() // | ||
.flatMap(annotation -> Stream.of(annotation.providers()).map(ReflectionUtils::newInstance)) // | ||
.collect(toUnmodifiableList()); | ||
} | ||
return providers; | ||
} | ||
|
||
private static ExclusiveResource.LockMode toLockMode(ResourceAccessMode mode) { | ||
switch (mode) { | ||
case READ: | ||
return ExclusiveResource.LockMode.READ; | ||
case READ_WRITE: | ||
return ExclusiveResource.LockMode.READ_WRITE; | ||
} | ||
throw new JUnitException("Unknown ResourceAccessMode: " + mode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.