Skip to content

Instantly share code, notes, and snippets.

@barancev
Last active April 8, 2020 20:52
Show Gist options
  • Select an option

  • Save barancev/df05d44c58ee9a5f3c711c301cec844c to your computer and use it in GitHub Desktop.

Select an option

Save barancev/df05d44c58ee9a5f3c711c301cec844c to your computer and use it in GitHub Desktop.
public class SimpleTest {
@Test
public void testIt() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
String exe = "C:\\tools\\server.exe";
CommandLine process = new CommandLine(exe, "--port=4545");
process.copyOutputTo(System.out);
process.executeAsync();
System.out.println("waitUntilAvailable " + waitUntilAvailable(process));
Thread.sleep(30000);
}
private boolean waitUntilAvailable(CommandLine process) throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
URL statusUrl = new URL("http://localhost:4545/status");
CompletableFuture<Boolean> serverStarted = CompletableFuture.supplyAsync(() -> {
try {
new UrlChecker().waitUntilAvailable(10000, TimeUnit.MILLISECONDS, statusUrl);
System.out.println("serverStarted return");
return true;
} catch (UrlChecker.TimeoutException e) {
System.out.println("serverStarted timeout");
throw new WebDriverException("Timed out waiting for driver server to start.", e);
}
});
CompletableFuture<Boolean> processFinished = CompletableFuture.supplyAsync(() -> {
process.waitFor(10000);
System.out.println("processFinished return");
return false;
});
boolean result = (Boolean) CompletableFuture.anyOf(serverStarted, processFinished).get(20, TimeUnit.SECONDS);
if (result) {
processFinished.cancel(true);
} else {
serverStarted.cancel(true);
}
return result;
}
}
public class SimpleTest {
@Test
public void testIt() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
String exe = "C:\\tools\\server.exe";
CommandLine process = new CommandLine(exe, "--port=4545");
process.copyOutputTo(System.out);
process.executeAsync();
System.out.println("waitUntilAvailable " + waitUntilAvailable(process));
Thread.sleep(30000);
}
private boolean waitUntilAvailable(CommandLine process) throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
ExecutorService service = Executors.newFixedThreadPool(2);
URL statusUrl = new URL("http://localhost:4545/status");
CompletableFuture<Boolean> status = new CompletableFuture<>();
Future<?> serverStarted = service.submit(() -> {
try {
new UrlChecker().waitUntilAvailable(10000, TimeUnit.MILLISECONDS, statusUrl);
System.out.println("serverStarted done");
status.complete(true);
} catch (UrlChecker.TimeoutException e) {
System.out.println("serverStarted timeout");
throw new WebDriverException("Timed out waiting for driver server to start.", e);
}
});
Future<?> processFinished = service.submit(() -> {
try {
process.waitFor(10000);
System.out.println("processFinished done");
status.complete(false);
} catch (WebDriverException ex) {
ex.printStackTrace();
}
});
boolean result = status.get(20, TimeUnit.SECONDS);
if (result) {
processFinished.cancel(true);
} else {
serverStarted.cancel(true);
}
service.shutdown();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment