-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add thread-per-test-class execution model #3941
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
133 changes: 133 additions & 0 deletions
133
...t/platform/engine/support/hierarchical/ThreadPerClassHierarchicalTestExecutorService.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,133 @@ | ||
/* | ||
* 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.platform.engine.support.hierarchical; | ||
|
||
import static java.lang.String.format; | ||
import static java.time.Duration.ofMinutes; | ||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static org.apiguardian.api.API.Status.STABLE; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.engine.ConfigurationParameters; | ||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.UniqueId; | ||
|
||
/** | ||
* A {@linkplain HierarchicalTestExecutorService executor service} that creates a new thread for | ||
* each test class, all {@linkplain TestTask test tasks}. | ||
* | ||
* <p>This execution model is useful to prevent some kinds of class / class-loader leaks. For | ||
* example, if a test creates {@link ClassLoader}s and the tests or any of the code and libraries | ||
* create {@link ThreadLocal}s, those thread locals would accumulate in the single {@link | ||
* SameThreadHierarchicalTestExecutorService} causing a class-(loader)-leak. | ||
* | ||
* @since 5.12 | ||
*/ | ||
@API(status = STABLE, since = "5.12") | ||
public class ThreadPerClassHierarchicalTestExecutorService implements HierarchicalTestExecutorService { | ||
|
||
private final AtomicInteger threadCount = new AtomicInteger(); | ||
private final Duration interruptWaitDuration; | ||
|
||
static final Duration DEFAULT_INTERRUPT_WAIT_DURATION = ofMinutes(5); | ||
static final String THREAD_PER_CLASS_INTERRUPTED_WAIT_TIME_SECONDS = "junit.jupiter.execution.threadperclass.interrupted.waittime.seconds"; | ||
|
||
public ThreadPerClassHierarchicalTestExecutorService(ConfigurationParameters config) { | ||
interruptWaitDuration = config.get(THREAD_PER_CLASS_INTERRUPTED_WAIT_TIME_SECONDS).map(Integer::parseInt).map( | ||
Duration::ofSeconds).orElse(DEFAULT_INTERRUPT_WAIT_DURATION); | ||
} | ||
|
||
@Override | ||
public Future<Void> submit(TestTask testTask) { | ||
executeTask(testTask); | ||
return completedFuture(null); | ||
} | ||
|
||
@Override | ||
public void invokeAll(List<? extends TestTask> tasks) { | ||
tasks.forEach(this::executeTask); | ||
} | ||
|
||
protected void executeTask(TestTask testTask) { | ||
NodeTestTask<?> nodeTestTask = (NodeTestTask<?>) testTask; | ||
TestDescriptor testDescriptor = nodeTestTask.getTestDescriptor(); | ||
|
||
UniqueId.Segment lastSegment = testDescriptor.getUniqueId().getLastSegment(); | ||
|
||
if ("class".equals(lastSegment.getType())) { | ||
executeOnDifferentThread(testTask, lastSegment); | ||
} | ||
else { | ||
testTask.execute(); | ||
} | ||
} | ||
|
||
private void executeOnDifferentThread(TestTask testTask, UniqueId.Segment lastSegment) { | ||
CompletableFuture<Object> future = new CompletableFuture<>(); | ||
Thread threadPerClass = new Thread(() -> { | ||
try { | ||
testTask.execute(); | ||
future.complete(null); | ||
} | ||
catch (Exception e) { | ||
future.completeExceptionally(e); | ||
} | ||
}, threadName(lastSegment)); | ||
threadPerClass.setDaemon(true); | ||
threadPerClass.start(); | ||
|
||
try { | ||
try { | ||
future.get(); | ||
} | ||
catch (InterruptedException e) { | ||
// propagate a thread-interrupt to the executing class | ||
threadPerClass.interrupt(); | ||
try { | ||
future.get(interruptWaitDuration.toMillis(), MILLISECONDS); | ||
} | ||
catch (InterruptedException ie) { | ||
threadPerClass.interrupt(); | ||
} | ||
catch (TimeoutException to) { | ||
throw new JUnitException(format("Test class %s was interrupted but did not terminate within %s", | ||
lastSegment.getValue(), interruptWaitDuration), to); | ||
} | ||
} | ||
} | ||
catch (ExecutionException e) { | ||
Throwable cause = e.getCause(); | ||
if (cause instanceof RuntimeException) { | ||
throw (RuntimeException) cause; | ||
} | ||
throw new JUnitException("TestTask execution failure", cause); | ||
} | ||
} | ||
|
||
private String threadName(UniqueId.Segment lastSegment) { | ||
return format("TEST THREAD #%d FOR %s", threadCount.incrementAndGet(), lastSegment.getValue()); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing to do | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an implementation detail of the JUnit Jupiter Engine so it should not be used in the platform module which is test engine agnostic. Perhaps you can use
TestDescriptor.getSource
instead to potentially obtain the class source?Note: I'm not part of the JUnit Team, just commenting.