|
| 1 | +package com.intuit.karate; |
| 2 | + |
| 3 | +import com.intuit.karate.core.*; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.util.*; |
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | +import java.util.concurrent.ExecutorService; |
| 10 | +import java.util.concurrent.Executors; |
| 11 | + |
| 12 | +public class RunnerBuilder { |
| 13 | + |
| 14 | + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RunnerBuilder.class); |
| 15 | + |
| 16 | + private int threadCount = 1; |
| 17 | + private Class<?> testClass; |
| 18 | + private List<String> paths; |
| 19 | + private String reportDir = FileUtils.getBuildDir() + File.separator + ScriptBindings.SUREFIRE_REPORTS; |
| 20 | + private List<String> tags; |
| 21 | + private Collection<ExecutionHook> hooks = Collections.emptyList();; |
| 22 | + private String scenarioName; |
| 23 | + |
| 24 | + private RunnerBuilder(){} |
| 25 | + |
| 26 | + public RunnerBuilder(Class<?> testClass){ |
| 27 | + this.testClass = testClass; |
| 28 | + } |
| 29 | + |
| 30 | + public RunnerBuilder(String... paths){ |
| 31 | + this.paths = Arrays.asList(paths); |
| 32 | + } |
| 33 | + |
| 34 | + public RunnerBuilder(List<String> tags, String... paths){ |
| 35 | + this.paths = Arrays.asList(paths); |
| 36 | + this.tags = tags; |
| 37 | + } |
| 38 | + public RunnerBuilder threadCount(int threadCount) { |
| 39 | + this.threadCount = threadCount; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + public RunnerBuilder reportDir(String reportDir) { |
| 44 | + this.reportDir = reportDir; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public RunnerBuilder hooks(Collection<ExecutionHook> hooks) { |
| 49 | + this.hooks.addAll(hooks); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public RunnerBuilder hook(ExecutionHook hook){ |
| 54 | + this.hooks.add(hook); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public RunnerBuilder scenarioName(String scenarioName) { |
| 59 | + this.scenarioName = scenarioName; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public Results runParallel() { |
| 64 | + String tagSelector; |
| 65 | + List<Resource> resources; |
| 66 | + // check if ambiguous configuration provided |
| 67 | + if (testClass != null) { |
| 68 | + RunnerOptions options = RunnerOptions.fromAnnotationAndSystemProperties(testClass); |
| 69 | + tagSelector = options.getTags() == null ? null : Tags.fromKarateOptionsTags(options.getTags()); |
| 70 | + resources = FileUtils.scanForFeatureFiles(options.getFeatures(), Thread.currentThread().getContextClassLoader()); |
| 71 | + }else { |
| 72 | + tagSelector = Tags.fromKarateOptionsTags(tags); |
| 73 | + resources = FileUtils.scanForFeatureFiles(paths, Thread.currentThread().getContextClassLoader()); |
| 74 | + } |
| 75 | + |
| 76 | + new File(reportDir).mkdirs(); |
| 77 | + |
| 78 | + final String finalReportDir = reportDir; |
| 79 | + Results results = Results.startTimer(threadCount); |
| 80 | + ExecutorService featureExecutor = Executors.newFixedThreadPool(threadCount); |
| 81 | + ExecutorService scenarioExecutor = Executors.newWorkStealingPool(threadCount); |
| 82 | + int executedFeatureCount = 0; |
| 83 | + try { |
| 84 | + int count = resources.size(); |
| 85 | + CountDownLatch latch = new CountDownLatch(count); |
| 86 | + List<FeatureResult> featureResults = new ArrayList(count); |
| 87 | + for (int i = 0; i < count; i++) { |
| 88 | + Resource resource = resources.get(i); |
| 89 | + int index = i + 1; |
| 90 | + Feature feature = FeatureParser.parse(resource); |
| 91 | + feature.setCallName(scenarioName); |
| 92 | + feature.setCallLine(resource.getLine()); |
| 93 | + FeatureContext featureContext = new FeatureContext(null, feature, tagSelector); |
| 94 | + CallContext callContext = CallContext.forAsync(feature, hooks, null, false); |
| 95 | + ExecutionContext execContext = new ExecutionContext(results.getStartTime(), featureContext, callContext, reportDir, |
| 96 | + r -> featureExecutor.submit(r), scenarioExecutor); |
| 97 | + featureResults.add(execContext.result); |
| 98 | + FeatureExecutionUnit unit = new FeatureExecutionUnit(execContext); |
| 99 | + unit.setNext(() -> { |
| 100 | + FeatureResult result = execContext.result; |
| 101 | + if (result.getScenarioCount() > 0) { // possible that zero scenarios matched tags |
| 102 | + File file = Engine.saveResultJson(finalReportDir, result, null); |
| 103 | + if (result.getScenarioCount() < 500) { |
| 104 | + // TODO this routine simply cannot handle that size |
| 105 | + Engine.saveResultXml(finalReportDir, result, null); |
| 106 | + } |
| 107 | + String status = result.isFailed() ? "fail" : "pass"; |
| 108 | + logger.info("<<{}>> feature {} of {}: {}", status, index, count, feature.getRelativePath()); |
| 109 | + result.printStats(file.getPath()); |
| 110 | + } else { |
| 111 | + results.addToSkipCount(1); |
| 112 | + if (logger.isTraceEnabled()) { |
| 113 | + logger.trace("<<skip>> feature {} of {}: {}", index, count, feature.getRelativePath()); |
| 114 | + } |
| 115 | + } |
| 116 | + latch.countDown(); |
| 117 | + }); |
| 118 | + featureExecutor.submit(unit); |
| 119 | + } |
| 120 | + latch.await(); |
| 121 | + results.stopTimer(); |
| 122 | + for (FeatureResult result : featureResults) { |
| 123 | + int scenarioCount = result.getScenarioCount(); |
| 124 | + results.addToScenarioCount(scenarioCount); |
| 125 | + if (scenarioCount != 0) { |
| 126 | + executedFeatureCount++; |
| 127 | + } |
| 128 | + results.addToFailCount(result.getFailedCount()); |
| 129 | + results.addToTimeTaken(result.getDurationMillis()); |
| 130 | + if (result.isFailed()) { |
| 131 | + results.addToFailedList(result.getPackageQualifiedName(), result.getErrorMessages()); |
| 132 | + } |
| 133 | + results.addScenarioResults(result.getScenarioResults()); |
| 134 | + } |
| 135 | + } catch (Exception e) { |
| 136 | + logger.error("karate parallel runner failed: ", e.getMessage()); |
| 137 | + results.setFailureReason(e); |
| 138 | + } finally { |
| 139 | + featureExecutor.shutdownNow(); |
| 140 | + scenarioExecutor.shutdownNow(); |
| 141 | + } |
| 142 | + results.setFeatureCount(executedFeatureCount); |
| 143 | + results.printStats(threadCount); |
| 144 | + Engine.saveStatsJson(reportDir, results, null); |
| 145 | + Engine.saveTimelineHtml(reportDir, results, null); |
| 146 | + results.setReportDir(reportDir); |
| 147 | + return results; |
| 148 | + } |
| 149 | +} |
0 commit comments