-
-
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.
Allow declaring exclusive resources for child nodes (#4151)
Allow declaring "shared resources" for direct child nodes via the new `@ResourceLock(target = CHILDREN)` attribute. Using the `@ResourceLock(target = CHILDREN)` in a class-level annotation has the same semantics as adding an annotation with the same value and mode to each test method and nested test class declared in this class. This may improve parallelization when a test class declares a `READ` lock, but only a few methods hold a `READ_WRITE` lock. Resolves #3102. --------- Co-authored-by: Marc Philipp <[email protected]>
- Loading branch information
1 parent
b8b5dc4
commit af6b502
Showing
10 changed files
with
385 additions
and
61 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
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
54 changes: 54 additions & 0 deletions
54
documentation/src/test/java/example/sharedresources/ChildrenSharedResourcesDemo.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,54 @@ | ||
/* | ||
* 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 example.sharedresources; | ||
|
||
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; | ||
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ; | ||
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE; | ||
import static org.junit.jupiter.api.parallel.ResourceLockTarget.CHILDREN; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ResourceLock; | ||
|
||
// tag::user_guide[] | ||
@Execution(CONCURRENT) | ||
@ResourceLock(value = "a", mode = READ, target = CHILDREN) | ||
public class ChildrenSharedResourcesDemo { | ||
|
||
@ResourceLock(value = "a", mode = READ_WRITE) | ||
@Test | ||
void test1() throws InterruptedException { | ||
Thread.sleep(2000L); | ||
} | ||
|
||
@Test | ||
void test2() throws InterruptedException { | ||
Thread.sleep(2000L); | ||
} | ||
|
||
@Test | ||
void test3() throws InterruptedException { | ||
Thread.sleep(2000L); | ||
} | ||
|
||
@Test | ||
void test4() throws InterruptedException { | ||
Thread.sleep(2000L); | ||
} | ||
|
||
@Test | ||
void test5() throws InterruptedException { | ||
Thread.sleep(2000L); | ||
} | ||
|
||
} | ||
// end::user_guide[] |
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
49 changes: 49 additions & 0 deletions
49
junit-jupiter-api/src/main/java/org/junit/jupiter/api/parallel/ResourceLockTarget.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,49 @@ | ||
/* | ||
* 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.api.parallel; | ||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* {@code ResourceLockTarget} is used to define the target of a shared resource. | ||
* | ||
* @since 5.12 | ||
* @see ResourceLock#target() | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "5.12") | ||
public enum ResourceLockTarget { | ||
|
||
/** | ||
* Add a shared resource to the current node. | ||
*/ | ||
SELF, | ||
|
||
/** | ||
* Add a shared resource to the <em>direct</em> children of the current node. | ||
* | ||
* <p>Examples of "parent - child" relationship in the context of | ||
* {@link ResourceLockTarget}: | ||
* <ul> | ||
* <li>a test class | ||
* - test methods and nested test classes declared in the class.</li> | ||
* <li>a nested test class | ||
* - test methods and nested test classes declared in the nested class. | ||
* </li> | ||
* <li>a test method | ||
* - considered to have no children. Using {@code CHILDREN} for | ||
* a test method results in an exception.</li> | ||
* </ul> | ||
*/ | ||
CHILDREN | ||
|
||
} |
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
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.