From e0312cc21aaf992cf268573b832bea3c627619fa Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Thu, 5 Dec 2024 16:47:42 +0900 Subject: [PATCH] Add configuration parameter Issue: #2229 Signed-off-by: yongjunhong --- .../vintage/engine/VintageTestEngine.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java index c5325fcb3c8e..f3dfb4df0ab3 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java @@ -51,6 +51,8 @@ public final class VintageTestEngine implements TestEngine { private static final int DEFAULT_THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors(); private static final int SHUTDOWN_TIMEOUT_SECONDS = 30; + private static final String PARALLEL_EXECUTION_ENABLED = "junit.jupiter.execution.parallel.enabled"; + private static final String PARALLEL_POOL_SIZE = "junit.vintage.execution.parallel.pool-size"; @Override public String getId() { @@ -84,16 +86,16 @@ public void execute(ExecutionRequest request) { EngineExecutionListener engineExecutionListener = request.getEngineExecutionListener(); VintageEngineDescriptor engineDescriptor = (VintageEngineDescriptor) request.getRootTestDescriptor(); engineExecutionListener.executionStarted(engineDescriptor); - executeAllChildren(engineDescriptor, engineExecutionListener); + executeAllChildren(engineDescriptor, engineExecutionListener, request); engineExecutionListener.executionFinished(engineDescriptor, successful()); } private void executeAllChildren(VintageEngineDescriptor engineDescriptor, - EngineExecutionListener engineExecutionListener) { - boolean parallelExecutionEnabled = getParallelExecutionEnabled(); + EngineExecutionListener engineExecutionListener, ExecutionRequest request) { + boolean parallelExecutionEnabled = getParallelExecutionEnabled(request); if (parallelExecutionEnabled) { - executeInParallel(engineDescriptor, engineExecutionListener); + executeInParallel(engineDescriptor, engineExecutionListener, request); } else { executeSequentially(engineDescriptor, engineExecutionListener); @@ -101,8 +103,8 @@ private void executeAllChildren(VintageEngineDescriptor engineDescriptor, } private void executeInParallel(VintageEngineDescriptor engineDescriptor, - EngineExecutionListener engineExecutionListener) { - ExecutorService executorService = Executors.newFixedThreadPool(getThreadPoolSize()); + EngineExecutionListener engineExecutionListener, ExecutionRequest request) { + ExecutorService executorService = Executors.newFixedThreadPool(getThreadPoolSize(request)); RunnerExecutor runnerExecutor = new RunnerExecutor(engineExecutionListener); List> futures = new ArrayList<>(); @@ -161,13 +163,20 @@ private void executeSequentially(VintageEngineDescriptor engineDescriptor, } } - private boolean getParallelExecutionEnabled() { - // get parallel execution enabled from configuration - return true; + private boolean getParallelExecutionEnabled(ExecutionRequest request) { + return request.getConfigurationParameters().getBoolean(PARALLEL_EXECUTION_ENABLED).orElse(false); } - private int getThreadPoolSize() { - // get thread pool size from configuration + private int getThreadPoolSize(ExecutionRequest request) { + Optional poolSize = request.getConfigurationParameters().get(PARALLEL_POOL_SIZE); + if (poolSize.isPresent()) { + try { + return Integer.parseInt(poolSize.get()); + } + catch (NumberFormatException e) { + logger.warn(() -> "Invalid value for parallel pool size: " + poolSize.get()); + } + } return DEFAULT_THREAD_POOL_SIZE; }