(TearDownFixture... fixtures)
| 32 | private static final Logger LOG = Logger.getLogger(Safely.class.getName()); |
| 33 | |
| 34 | public static void safelyCall(TearDownFixture... fixtures) { |
| 35 | ExecutorService executor = Executors.newFixedThreadPool(fixtures.length); |
| 36 | List<CompletableFuture<Void>> futures = new LinkedList<>(); |
| 37 | |
| 38 | for (TearDownFixture fixture : fixtures) { |
| 39 | CompletableFuture<Void> check = new CompletableFuture<>(); |
| 40 | executor.submit(() -> { |
| 41 | // Fixture being null is handled by the exception check. |
| 42 | try { |
| 43 | fixture.tearDown(); |
| 44 | } catch (Exception ignored) { |
| 45 | // nothing to see here. |
| 46 | } |
| 47 | check.complete(null); |
| 48 | }); |
| 49 | futures.add(check); |
| 50 | } |
| 51 | |
| 52 | executor.shutdown(); |
| 53 | |
| 54 | try { |
| 55 | CompletableFuture.allOf(futures.toArray(new CompletableFuture[] {})).get(2, TimeUnit.MINUTES); |
| 56 | } catch (TimeoutException ex) { |
| 57 | LOG.log(Level.WARNING, "tear down timed out: {}", ex.toString()); |
| 58 | } catch (Exception ex) { |
| 59 | LOG.log(Level.WARNING, "tear down failed", ex); |
| 60 | } |
| 61 | } |
| 62 | } |
no test coverage detected