Skip to content

Commit

Permalink
Merge pull request #340 from DerekJLeggett/master
Browse files Browse the repository at this point in the history
Run multiple test files in parallel.
  • Loading branch information
vmi authored Mar 22, 2023
2 parents cf6acf4 + 4dd9b6c commit 5b21451
Showing 1 changed file with 61 additions and 5 deletions.
66 changes: 61 additions & 5 deletions src/main/java/jp/vmi/selenium/selenese/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -15,6 +22,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import jp.vmi.html.result.HtmlResult;
import jp.vmi.selenium.runner.converter.Converter;
import jp.vmi.selenium.selenese.command.ICommandFactory;
import jp.vmi.selenium.selenese.config.DefaultConfig;
Expand All @@ -30,6 +38,7 @@
import jp.vmi.selenium.webdriver.DriverOptions;
import jp.vmi.selenium.webdriver.DriverOptions.DriverOption;
import jp.vmi.selenium.webdriver.WebDriverManager;
import static jp.vmi.selenium.selenese.result.Unexecuted.*;

import static jp.vmi.selenium.selenese.config.DefaultConfig.*;

Expand Down Expand Up @@ -78,6 +87,26 @@ private void help(String... msgs) {
exit(Level.USAGE);
}

/**
* New object to hold result and selenese
*/
private class ResultObject{
Result result;
Selenese selenese;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
public Selenese getSelenese() {
return selenese;
}
public void setSelenese(Selenese selenese) {
this.selenese = selenese;
}
}

/**
* Start Selenese Runner.
*
Expand All @@ -91,11 +120,38 @@ public void run(String[] args) {
if (filenames.length == 0)
help();
log.info("Start: " + PROG_TITLE + " {}", getVersion());
Runner runner = new Runner();
runner.setCommandLineArgs(args);
setupRunner(runner, config, filenames);
Result totalResult = runner.run(filenames);
runner.finish();
Result totalResult = UNEXECUTED;
ExecutorService executor = Executors.newFixedThreadPool(filenames.length);
CompletionService<ResultObject> completionService = new ExecutorCompletionService<ResultObject>(executor);
for (String fileName : filenames) {
completionService.submit(new Callable<ResultObject>() {
public ResultObject call() {
Runner runner = new Runner();
Selenese selenese = Parser.parse(fileName, runner.getCommandFactory());
runner.setCommandLineArgs(args);
setupRunner(runner, config, fileName);
Result result = runner.run(fileName);
runner.finish();
ResultObject resultObject = new ResultObject();
resultObject.setResult(result);
resultObject.setSelenese(selenese);
return resultObject;
}
});
}
int received = 0;
boolean errors = false;

while (received < filenames.length && !errors) {
Future<ResultObject> resultFuture = completionService.take(); //blocks if none available
try {
ResultObject resultObject = resultFuture.get();
received++;
totalResult = totalResult.updateWithChildResult(resultObject.getSelenese(), resultObject.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
exitLevel = totalResult.getLevel();
} catch (IllegalArgumentException e) {
help("Error: " + e.getMessage());
Expand Down

0 comments on commit 5b21451

Please sign in to comment.