ParallelKubernetesLander implements Lander { @Override public CompletableFuture<LanderConfiguration> run() { CompletableFuture<?> anyException = new CompletableFuture<>(); List<CompletableFuture<LanderConfiguration>> landerFutures = landers.stream().map(Lander::run).collect(toList()); landerFutures.forEach(f -> f.exceptionally(t -> { landerFutures.forEach(lander -> lander.cancel(true)); anyException.completeExceptionally(t); return null; })); CompletableFuture<Void> allSucceed = runAsync(() -> landerFutures.forEach(CompletableFuture::join)); return anyOf(anyException, allSucceed).thenApply(x -> landerConfig); }  @Override CompletableFuture<LanderConfiguration> run();  }
@Test public void success() throws Exception { future1.complete(config.withOffsets(ImmutableMap.of(0, range))); future2.complete(config.withOffsets(ImmutableMap.of(1, range))); future3.complete(config.withOffsets(ImmutableMap.of(2, range))); LanderConfiguration result = underTest.run().join(); assertThat(result, is(config)); }
@Test public void failFast() throws Exception { future1.completeExceptionally(new Exception()); future2.completeExceptionally(new Exception()); try { underTest.run().join(); fail(); } catch (Exception ignore) {} assertThat(future1.isCancelled(), is(false)); assertThat(future2.isCancelled(), is(false)); assertThat(future3.isCancelled(), is(true)); }