|
| 1 | +package io.cucumber.junit.platform.engine; |
| 2 | + |
| 3 | +import net.thucydides.model.environment.SystemEnvironmentVariables; |
| 4 | +import net.thucydides.model.util.EnvironmentVariables; |
| 5 | + |
| 6 | +import org.junit.jupiter.engine.config.DefaultJupiterConfiguration; |
| 7 | +import org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor; |
| 8 | +import org.junit.jupiter.engine.discovery.DiscoverySelectorResolver; |
| 9 | +import org.junit.platform.engine.ConfigurationParameters; |
| 10 | +import org.junit.platform.engine.EngineDiscoveryRequest; |
| 11 | +import org.junit.platform.engine.ExecutionRequest; |
| 12 | +import org.junit.platform.engine.TestDescriptor; |
| 13 | +import org.junit.platform.engine.UniqueId; |
| 14 | +import org.junit.platform.engine.support.config.PrefixedConfigurationParameters; |
| 15 | +import org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService; |
| 16 | +import org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine; |
| 17 | +import org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutorService; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.IntStream; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import static io.cucumber.core.options.Constants.FILTER_TAGS_PROPERTY_NAME; |
| 30 | +import static io.cucumber.junit.platform.engine.Constants.PARALLEL_CONFIG_PREFIX; |
| 31 | +import static io.cucumber.junit.platform.engine.Constants.PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME; |
| 32 | +import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_BATCH_COUNT; |
| 33 | +import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_BATCH_NUMBER; |
| 34 | +import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_FORK_COUNT; |
| 35 | +import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_FORK_NUMBER; |
| 36 | + |
| 37 | + |
| 38 | +public final class CucumberBatchTestEngine extends HierarchicalTestEngine<CucumberEngineExecutionContext> { |
| 39 | + |
| 40 | + static final Logger LOGGER = LoggerFactory.getLogger(CucumberBatchTestEngine.class); |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getId() { |
| 44 | + return "cucumber-batch"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) { |
| 49 | + CucumberEngineDescriptor engineDescriptor = new CucumberEngineDescriptor(uniqueId); |
| 50 | + DefaultJupiterConfiguration jupiterConfiguration = new DefaultJupiterConfiguration(null); |
| 51 | + JupiterEngineDescriptor dd = new JupiterEngineDescriptor(uniqueId, jupiterConfiguration); |
| 52 | + new DiscoverySelectorResolver().resolveSelectors(discoveryRequest, dd); |
| 53 | + return engineDescriptor; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected HierarchicalTestExecutorService createExecutorService(ExecutionRequest request) { |
| 58 | + ConfigurationParameters config = request.getConfigurationParameters(); |
| 59 | + if (config.getBoolean(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME).orElse(false)) { |
| 60 | + return new ForkJoinPoolHierarchicalTestExecutorService( |
| 61 | + new PrefixedConfigurationParameters(config, PARALLEL_CONFIG_PREFIX)); |
| 62 | + } |
| 63 | + |
| 64 | + if (!request.getRootTestDescriptor().getChildren().isEmpty()) { |
| 65 | + processRequestIfBatched(request); |
| 66 | + } |
| 67 | + |
| 68 | + return super.createExecutorService(request); |
| 69 | + } |
| 70 | + |
| 71 | + static void processRequestIfBatched(ExecutionRequest request) { |
| 72 | + //populate list |
| 73 | + String tagFilter = request.getConfigurationParameters().get(FILTER_TAGS_PROPERTY_NAME) |
| 74 | + .orElse(System.getProperty(FILTER_TAGS_PROPERTY_NAME)); |
| 75 | + List<WeightedTest> scenarioList = request.getRootTestDescriptor().getChildren().stream() |
| 76 | + .map(TestDescriptor::getChildren) |
| 77 | + .flatMap(Set::stream) |
| 78 | + .map(WeightedTest::new) |
| 79 | + .collect(Collectors.toList()); |
| 80 | + int total = scenarioList.size(); |
| 81 | + List<WeightedTest> tagFilteredScenarioList = scenarioList.stream() |
| 82 | + .filter(scenario -> scenario.isTagMatchingFilter(tagFilter)) |
| 83 | + .collect(Collectors.toList()); |
| 84 | + LOGGER.info("Found {} scenarios in classpath, {} match(es) tag filter {}", total, tagFilteredScenarioList.size(), tagFilter); |
| 85 | + |
| 86 | + EnvironmentVariables envs = SystemEnvironmentVariables.currentEnvironmentVariables(); |
| 87 | + int batchCount = envs.getPropertyAsInteger(SERENITY_BATCH_COUNT, 1); |
| 88 | + int batchNumber = envs.getPropertyAsInteger(SERENITY_BATCH_NUMBER, 1); |
| 89 | + int forkCount = envs.getPropertyAsInteger(SERENITY_FORK_COUNT, 1); |
| 90 | + int forkNumber = envs.getPropertyAsInteger(SERENITY_FORK_NUMBER, 1); |
| 91 | + |
| 92 | + LOGGER.info("Parameters: \n{}", request.getConfigurationParameters()); |
| 93 | + LOGGER.info("Running partitioning for batch {} of {} and fork {} of {}", batchNumber, |
| 94 | + batchCount, forkNumber, forkCount); |
| 95 | + |
| 96 | + List<WeightedTest> batch = getPartition(tagFilteredScenarioList, batchCount, batchNumber); |
| 97 | + List<WeightedTest> testToRun = getPartition(batch, forkCount, forkNumber); |
| 98 | + |
| 99 | + //prune and keep only test to run |
| 100 | + scenarioList.removeAll(testToRun); |
| 101 | + scenarioList.forEach(WeightedTest::removeFromHierarchy); |
| 102 | + |
| 103 | + LOGGER.info("Running {} of {} scenarios", testToRun.size(), total); |
| 104 | + LOGGER.info("Test to run: {}", testToRun); |
| 105 | + LOGGER.info("Root test descriptor has {} feature(s)", |
| 106 | + request.getRootTestDescriptor().getChildren().size()); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected CucumberEngineExecutionContext createExecutionContext(ExecutionRequest request) { |
| 111 | + return new CucumberEngineExecutionContext(request.getConfigurationParameters()); |
| 112 | + } |
| 113 | + |
| 114 | + static List<WeightedTest> getPartition(List<WeightedTest> list, int partitions, int index) { |
| 115 | + if (partitions == 1 && index == 1) { |
| 116 | + return new ArrayList<>(list); |
| 117 | + } |
| 118 | + return getPartitionedTests(list, partitions).get(index - 1); |
| 119 | + } |
| 120 | + |
| 121 | + static List<List<WeightedTest>> getPartitionedTests(List<WeightedTest> list, int partitions) { |
| 122 | + List<List<WeightedTest>> result = Stream.generate(ArrayList<WeightedTest>::new) |
| 123 | + .limit(partitions) |
| 124 | + .collect(Collectors.toList()); |
| 125 | + |
| 126 | + //sort all scenarios from large to small |
| 127 | + list.sort(Comparator.comparing(WeightedTest::getWeight).reversed()); |
| 128 | + int[] weights = new int[partitions]; |
| 129 | + |
| 130 | + for (WeightedTest test : list) { |
| 131 | + int minPartition = getMinPartition(weights); |
| 132 | + result.get(minPartition).add(test); |
| 133 | + weights[minPartition] += test.getWeight(); |
| 134 | + } |
| 135 | + |
| 136 | + for (int i = 0; i < result.size(); i++) { |
| 137 | + LOGGER.info("{} of {}, weight = {}", i + 1, partitions, |
| 138 | + result.get(i).stream().mapToInt(WeightedTest::getWeight).sum()); |
| 139 | + LOGGER.info(print(result.get(i))); |
| 140 | + } |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + private static String print(List<WeightedTest> list) { |
| 145 | + return list.stream().map(WeightedTest::toString).collect(Collectors.joining("\n")); |
| 146 | + } |
| 147 | + |
| 148 | + private static int getMinPartition(int[] weights) { |
| 149 | + return IntStream.range(0, weights.length) |
| 150 | + .boxed() |
| 151 | + .min(Comparator.comparingInt(i -> weights[i])) |
| 152 | + .orElse(-1); |
| 153 | + } |
| 154 | +} |
0 commit comments