### Question:
ThriftUtils { public static <T> CompletableFuture<T> firstSuccess(Stream<CompletableFuture<T>> futures) throws NoSuchElementException{ CompletableFuture<T> result = new CompletableFuture<>(); CompletableFuture<Void>[] array = futures .map(future -> future.thenAccept(result::complete)) .toArray(CompletableFuture[]::new); if (array.length == 0) { throw new NoSuchElementException("No result available."); } CompletableFuture.allOf(array).exceptionally(ex -> { result.completeExceptionally(ex); return null; }); return result; }  static Class<?> getClassByName(Class<?> root, String name); static Class<?> getThriftRootClass(Class<?> generatedChildClass); static boolean parseBoolean(String val); static String getThriftServiceName(Class<?> generatedChildClass, Optional<String> subServiceName); static String getThriftServiceName(Class<?> generatedChildClass); static String getThriftFullServiceName(Class<?> generatedChildClass, Optional<String> subServiceName); static String getThriftFullServiceName(Class<?> generatedChildClass); static boolean isApplicationLevelException(Throwable ex); static String getHostName(); static CompletableFuture<T> firstSuccess(Stream<CompletableFuture<T>> futures); static boolean empty(String str); static long parseLong(byte[] bytes); static long parseLong(byte[] bytes, int offset); static final String QIWI_APP_HOST_NAME; }### Answer:
@Test(groups = "unit") public void firstSuccess() throws Exception { CompletableFuture<String> future1 = new CompletableFuture<>(); CompletableFuture<String> future2 = new CompletableFuture<>(); CompletableFuture<String> future3 = new CompletableFuture<>(); CompletableFuture<String> result = ThriftUtils.firstSuccess(Stream.of(future1, future2, future3)); assertFalse(result.isDone()); future3.completeExceptionally(new RuntimeException()); assertFalse(result.isDone()); future2.complete("Test"); assertTrue(result.isDone()); assertEquals(result.join(), "Test"); }
@Test(groups = "unit") public void firstSuccessOnError() throws Exception { CompletableFuture<String> future1 = new CompletableFuture<>(); CompletableFuture<String> future2 = new CompletableFuture<>(); CompletableFuture<String> future3 = new CompletableFuture<>(); CompletableFuture<String> result = ThriftUtils.firstSuccess(Stream.of(future1, future2, future3)); assertFalse(result.isDone()); future3.completeExceptionally(new RuntimeException("Test_msg")); assertFalse(result.isDone()); future1.completeExceptionally(new RuntimeException("Test_msg")); assertFalse(result.isDone()); future2.completeExceptionally(new RuntimeException("Test_msg")); assertTrue(result.isDone()); try { result.get(); fail(); } catch (ExecutionException e) { assertEquals(e.getCause().getMessage(), "Test_msg"); } }