Skip to content

Commit 950c2e6

Browse files
authored
CompositeFuture should support custom future implementations (#5401)
See #5399 CompositeFuture expected every future to extend FutureBase. But sometimes users may come with their own implementation. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 1a28a6f commit 950c2e6

File tree

2 files changed

+113
-15
lines changed

2 files changed

+113
-15
lines changed

vertx-core/src/main/java/io/vertx/core/impl/future/CompositeFutureImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ private CompositeFutureImpl(int op, boolean initializing, Future<?>... results)
6161

6262
private void init() {
6363
for (Future<?> result : results) {
64-
FutureBase internal = (FutureBase<?>) result;
65-
internal.addListener(this);
64+
if (result instanceof FutureBase) {
65+
FutureBase internal = (FutureBase<?>) result;
66+
internal.addListener(this);
67+
} else {
68+
result.onComplete(this);
69+
}
6670
}
6771
Object o;
6872
synchronized (this) {
@@ -205,8 +209,10 @@ public int size() {
205209

206210
private void doComplete(Object result) {
207211
for (Future<?> r : results) {
208-
FutureBase internal = (FutureBase<?>) r;
209-
internal.removeListener(this);
212+
if (r instanceof FutureBase) {
213+
FutureBase internal = (FutureBase<?>) r;
214+
internal.removeListener(this);
215+
}
210216
}
211217
if (result == this) {
212218
tryComplete(this);

vertx-core/src/test/java/io/vertx/tests/future/CompositeFutureTest.java

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@
1111

1212
package io.vertx.tests.future;
1313

14-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
15-
16-
import io.vertx.core.Completable;
17-
import io.vertx.core.CompositeFuture;
18-
import io.vertx.core.Future;
19-
import io.vertx.core.Promise;
14+
import io.vertx.core.*;
2015
import io.vertx.core.impl.future.FutureImpl;
2116
import io.vertx.test.core.Repeat;
22-
2317
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
2418
import org.junit.Test;
2519

@@ -29,13 +23,12 @@
2923
import java.util.concurrent.Executors;
3024
import java.util.concurrent.TimeUnit;
3125
import java.util.concurrent.atomic.AtomicInteger;
32-
import java.util.function.BiConsumer;
33-
import java.util.function.BiFunction;
34-
import java.util.function.Consumer;
35-
import java.util.function.Function;
26+
import java.util.function.*;
3627
import java.util.stream.Collectors;
3728
import java.util.stream.IntStream;
3829

30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
3932
/**
4033
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
4134
*/
@@ -577,4 +570,103 @@ public void testAnyRemovesListeners2() {
577570
Future.any(f, Future.succeededFuture());
578571
assertEquals(Collections.emptySet(), f.listeners);
579572
}
573+
574+
@Test
575+
public void testCustomFuture() {
576+
Promise<Void> p1 = Promise.promise();
577+
Promise<Void> p2 = Promise.promise();
578+
Promise<Void> p3 = Promise.promise();
579+
580+
CompositeFuture cf = Future.all(p1.future(), new MyFuture(p2), p3.future());
581+
582+
p1.complete(null);
583+
p2.complete(null);
584+
p3.complete(null);
585+
586+
assertTrue(cf.isComplete());
587+
}
588+
589+
private static class MyFuture implements Future<Void> {
590+
591+
private final Future<Void> delegate;
592+
593+
private MyFuture(Promise<Void> promise) {
594+
delegate = promise.future();
595+
}
596+
597+
@Override
598+
public boolean isComplete() {
599+
return delegate.isComplete();
600+
}
601+
602+
@Override
603+
public Future<Void> onComplete(Handler<AsyncResult<Void>> handler) {
604+
return delegate.onComplete(handler);
605+
}
606+
607+
@Override
608+
public Void result() {
609+
return delegate.result();
610+
}
611+
612+
@Override
613+
public Throwable cause() {
614+
return delegate.cause();
615+
}
616+
617+
@Override
618+
public boolean succeeded() {
619+
return delegate.succeeded();
620+
}
621+
622+
@Override
623+
public boolean failed() {
624+
return delegate.failed();
625+
}
626+
627+
@Override
628+
public <U> Future<U> compose(Function<? super Void, Future<U>> successMapper, Function<Throwable, Future<U>> failureMapper) {
629+
return delegate.compose(successMapper, failureMapper);
630+
}
631+
632+
@Override
633+
public <U> Future<U> transform(Function<AsyncResult<Void>, Future<U>> mapper) {
634+
return delegate.transform(mapper);
635+
}
636+
637+
@Override
638+
public <U> Future<Void> eventually(Supplier<Future<U>> mapper) {
639+
return delegate.eventually(mapper);
640+
}
641+
642+
@Override
643+
public <U> Future<U> map(Function<? super Void, U> mapper) {
644+
return delegate.map(mapper);
645+
}
646+
647+
@Override
648+
public <V> Future<V> map(V value) {
649+
return delegate.map(value);
650+
}
651+
652+
@Override
653+
public Future<Void> otherwise(Function<Throwable, Void> mapper) {
654+
return delegate.otherwise(mapper);
655+
}
656+
657+
@Override
658+
public Future<Void> otherwise(Void value) {
659+
return delegate.otherwise(value);
660+
}
661+
662+
@Override
663+
public Future<Void> expecting(Expectation<? super Void> expectation) {
664+
return delegate.expecting(expectation);
665+
}
666+
667+
@Override
668+
public Future<Void> timeout(long delay, TimeUnit unit) {
669+
return delegate.timeout(delay, unit);
670+
}
671+
}
580672
}

0 commit comments

Comments
 (0)