Skip to content

Commit

Permalink
[java] ensure all tear downs are executed after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Aug 26, 2023
1 parent 58d7f3c commit 06ee06b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions java/test/org/openqa/selenium/testing/Safely.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Safely {

private static final Logger LOG = Logger.getLogger(Safely.class.getName());

public static void safelyCall(TearDownFixture... fixtures) {
ExecutorService executor = Executors.newFixedThreadPool(fixtures.length);
List<CompletableFuture<Void>> futures = new LinkedList<>();
Expand All @@ -44,10 +50,14 @@ public static void safelyCall(TearDownFixture... fixtures) {
futures.add(check);
}

executor.shutdown();

try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[] {}));
} finally {
executor.shutdownNow();
CompletableFuture.allOf(futures.toArray(new CompletableFuture[] {})).get(2, TimeUnit.MINUTES);
} catch (TimeoutException ex) {
LOG.log(Level.WARNING, "tear down timed out");
} catch (Exception ex) {
LOG.log(Level.WARNING, "tear down failed", ex);
}
}
}

0 comments on commit 06ee06b

Please sign in to comment.