(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 | () -> { |
| 42 | // Fixture being null is handled by the exception check. |
| 43 | try { |
| 44 | fixture.tearDown(); |
| 45 | } catch (Exception ignored) { |
| 46 | // nothing to see here. |
| 47 | } |
| 48 | check.complete(null); |
| 49 | }); |
| 50 | futures.add(check); |
| 51 | } |
| 52 | |
| 53 | executor.shutdown(); |
| 54 | |
| 55 | try { |
| 56 | CompletableFuture.allOf(futures.toArray(new CompletableFuture[] {})).get(2, TimeUnit.MINUTES); |
| 57 | } catch (TimeoutException ex) { |
| 58 | LOG.log(Level.WARNING, "tear down timed out: {}", ex.toString()); |
| 59 | } catch (Exception ex) { |
| 60 | LOG.log(Level.WARNING, "tear down failed", ex); |
| 61 | } |
| 62 | } |
| 63 | } |
no test coverage detected