mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
Merge pull request #3278 from tsegismont/completed-futures-context
Completed futures should operate on provided context
This commit is contained in:
@@ -52,7 +52,11 @@ public class FailedFuture<T> implements Future<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
|
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
|
||||||
handler.handle(this);
|
if (context != null) {
|
||||||
|
context.dispatch(this, handler);
|
||||||
|
} else {
|
||||||
|
handler.handle(this);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,11 @@ class SucceededFuture<T> implements Future<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
|
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
|
||||||
handler.handle(this);
|
if (context != null) {
|
||||||
|
context.dispatch(this, handler);
|
||||||
|
} else {
|
||||||
|
handler.handle(this);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
@@ -1570,4 +1573,45 @@ public class FutureTest extends VertxTestBase {
|
|||||||
|
|
||||||
await();
|
await();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompletedFuturesContext() throws Exception {
|
||||||
|
waitFor(4);
|
||||||
|
|
||||||
|
Thread testThread = Thread.currentThread();
|
||||||
|
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
|
||||||
|
|
||||||
|
|
||||||
|
CompletableFuture<Thread> cf = new CompletableFuture<>();
|
||||||
|
context.runOnContext(v -> cf.complete(Thread.currentThread()));
|
||||||
|
Thread contextThread = cf.get();
|
||||||
|
|
||||||
|
Future.succeededFuture().onSuccess(v -> {
|
||||||
|
assertSame(testThread, Thread.currentThread());
|
||||||
|
assertNull(Vertx.currentContext());
|
||||||
|
complete();
|
||||||
|
});
|
||||||
|
|
||||||
|
context.succeededFuture().onSuccess(v -> {
|
||||||
|
assertNotSame(testThread, Thread.currentThread());
|
||||||
|
assertSame(context, Vertx.currentContext());
|
||||||
|
assertSame(contextThread, Thread.currentThread());
|
||||||
|
complete();
|
||||||
|
});
|
||||||
|
|
||||||
|
Future.failedFuture(new Exception()).onFailure(v -> {
|
||||||
|
assertSame(testThread, Thread.currentThread());
|
||||||
|
assertNull(Vertx.currentContext());
|
||||||
|
complete();
|
||||||
|
});
|
||||||
|
|
||||||
|
context.failedFuture(new Exception()).onFailure(v -> {
|
||||||
|
assertNotSame(testThread, Thread.currentThread());
|
||||||
|
assertSame(context, Vertx.currentContext());
|
||||||
|
assertSame(contextThread, Thread.currentThread());
|
||||||
|
complete();
|
||||||
|
});
|
||||||
|
|
||||||
|
await();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user