Skip to content

Commit e0312cc

Browse files
committed
Add configuration parameter
Issue: #2229 Signed-off-by: yongjunhong <[email protected]>
1 parent 56ea4b9 commit e0312cc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public final class VintageTestEngine implements TestEngine {
5151

5252
private static final int DEFAULT_THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();
5353
private static final int SHUTDOWN_TIMEOUT_SECONDS = 30;
54+
private static final String PARALLEL_EXECUTION_ENABLED = "junit.jupiter.execution.parallel.enabled";
55+
private static final String PARALLEL_POOL_SIZE = "junit.vintage.execution.parallel.pool-size";
5456

5557
@Override
5658
public String getId() {
@@ -84,25 +86,25 @@ public void execute(ExecutionRequest request) {
8486
EngineExecutionListener engineExecutionListener = request.getEngineExecutionListener();
8587
VintageEngineDescriptor engineDescriptor = (VintageEngineDescriptor) request.getRootTestDescriptor();
8688
engineExecutionListener.executionStarted(engineDescriptor);
87-
executeAllChildren(engineDescriptor, engineExecutionListener);
89+
executeAllChildren(engineDescriptor, engineExecutionListener, request);
8890
engineExecutionListener.executionFinished(engineDescriptor, successful());
8991
}
9092

9193
private void executeAllChildren(VintageEngineDescriptor engineDescriptor,
92-
EngineExecutionListener engineExecutionListener) {
93-
boolean parallelExecutionEnabled = getParallelExecutionEnabled();
94+
EngineExecutionListener engineExecutionListener, ExecutionRequest request) {
95+
boolean parallelExecutionEnabled = getParallelExecutionEnabled(request);
9496

9597
if (parallelExecutionEnabled) {
96-
executeInParallel(engineDescriptor, engineExecutionListener);
98+
executeInParallel(engineDescriptor, engineExecutionListener, request);
9799
}
98100
else {
99101
executeSequentially(engineDescriptor, engineExecutionListener);
100102
}
101103
}
102104

103105
private void executeInParallel(VintageEngineDescriptor engineDescriptor,
104-
EngineExecutionListener engineExecutionListener) {
105-
ExecutorService executorService = Executors.newFixedThreadPool(getThreadPoolSize());
106+
EngineExecutionListener engineExecutionListener, ExecutionRequest request) {
107+
ExecutorService executorService = Executors.newFixedThreadPool(getThreadPoolSize(request));
106108
RunnerExecutor runnerExecutor = new RunnerExecutor(engineExecutionListener);
107109

108110
List<CompletableFuture<Void>> futures = new ArrayList<>();
@@ -161,13 +163,20 @@ private void executeSequentially(VintageEngineDescriptor engineDescriptor,
161163
}
162164
}
163165

164-
private boolean getParallelExecutionEnabled() {
165-
// get parallel execution enabled from configuration
166-
return true;
166+
private boolean getParallelExecutionEnabled(ExecutionRequest request) {
167+
return request.getConfigurationParameters().getBoolean(PARALLEL_EXECUTION_ENABLED).orElse(false);
167168
}
168169

169-
private int getThreadPoolSize() {
170-
// get thread pool size from configuration
170+
private int getThreadPoolSize(ExecutionRequest request) {
171+
Optional<String> poolSize = request.getConfigurationParameters().get(PARALLEL_POOL_SIZE);
172+
if (poolSize.isPresent()) {
173+
try {
174+
return Integer.parseInt(poolSize.get());
175+
}
176+
catch (NumberFormatException e) {
177+
logger.warn(() -> "Invalid value for parallel pool size: " + poolSize.get());
178+
}
179+
}
171180
return DEFAULT_THREAD_POOL_SIZE;
172181
}
173182

0 commit comments

Comments
 (0)