Skip to content

Commit

Permalink
CompositeFuture should support custom future implementations (#5401)
Browse files Browse the repository at this point in the history
See #5399

CompositeFuture expected every future to extend FutureBase.
But sometimes users may come with their own implementation.

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Nov 21, 2024
1 parent b8d0699 commit 0b41047
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/main/java/io/vertx/core/impl/future/CompositeFutureImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
package io.vertx.core.impl.future;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;

/**
Expand Down Expand Up @@ -64,8 +64,12 @@ private CompositeFutureImpl(int op, boolean initializing, Future<?>... results)

private void init() {
for (Future<?> result : results) {
FutureInternal internal = (FutureInternal<?>) result;
internal.addListener(this);
if (result instanceof FutureInternal) {
FutureInternal internal = (FutureInternal<?>) result;
internal.addListener(this);
} else {
result.onComplete(this::onSuccess, this::onFailure);
}
}
Object o;
synchronized (this) {
Expand Down Expand Up @@ -201,8 +205,10 @@ public int size() {

private void complete(Object result) {
for (Future<?> r : results) {
FutureInternal internal = (FutureInternal<?>) r;
internal.removeListener(this);
if (r instanceof FutureInternal) {
FutureInternal internal = (FutureInternal<?>) r;
internal.removeListener(this);
}
}
if (result == this) {
tryComplete(this);
Expand Down
104 changes: 101 additions & 3 deletions src/test/java/io/vertx/core/CompositeFutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

package io.vertx.core;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import io.vertx.core.impl.future.FutureImpl;
import io.vertx.core.impl.future.Listener;
import io.vertx.test.core.Repeat;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;

Expand All @@ -33,6 +30,8 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
Expand Down Expand Up @@ -574,4 +573,103 @@ public void testAnyRemovesListeners2() {
Future.any(f, Future.succeededFuture());
assertEquals(Collections.emptySet(), f.listeners);
}

@Test
public void testCustomFuture() {
Promise<Void> p1 = Promise.promise();
Promise<Void> p2 = Promise.promise();
Promise<Void> p3 = Promise.promise();

CompositeFuture cf = Future.all(p1.future(), new MyFuture(p2), p3.future());

p1.complete(null);
p2.complete(null);
p3.complete(null);

assertTrue(cf.isComplete());
}

private static class MyFuture implements Future<Void> {

private final Future<Void> delegate;

private MyFuture(Promise<Void> promise) {
delegate = promise.future();
}

@Override
public boolean isComplete() {
return delegate.isComplete();
}

@Override
public Future<Void> onComplete(Handler<AsyncResult<Void>> handler) {
return delegate.onComplete(handler);
}

@Override
public Void result() {
return delegate.result();
}

@Override
public Throwable cause() {
return delegate.cause();
}

@Override
public boolean succeeded() {
return delegate.succeeded();
}

@Override
public boolean failed() {
return delegate.failed();
}

@Override
public <U> Future<U> compose(Function<Void, Future<U>> successMapper, Function<Throwable, Future<U>> failureMapper) {
return delegate.compose(successMapper, failureMapper);
}

@Override
public <U> Future<U> transform(Function<AsyncResult<Void>, Future<U>> mapper) {
return delegate.transform(mapper);
}

@Override
public <U> Future<Void> eventually(Function<Void, Future<U>> function) {
return delegate.eventually(function);
}

@Override
public <U> Future<U> map(Function<Void, U> mapper) {
return delegate.map(mapper);
}

@Override
public <V> Future<V> map(V value) {
return delegate.map(value);
}

@Override
public Future<Void> otherwise(Function<Throwable, Void> mapper) {
return delegate.otherwise(mapper);
}

@Override
public Future<Void> otherwise(Void value) {
return delegate.otherwise(value);
}

@Override
public Future<Void> expecting(Expectation<? super Void> expectation) {
return delegate.expecting(expectation);
}

@Override
public Future<Void> timeout(long delay, TimeUnit unit) {
return delegate.timeout(delay, unit);
}
}
}

0 comments on commit 0b41047

Please sign in to comment.