Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed futures should not park #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.vertx.await;

import io.netty.channel.EventLoop;
import io.vertx.await.impl.DefaultScheduler;
import io.vertx.await.impl.EventLoopScheduler;
import io.vertx.await.impl.Scheduler;
import io.vertx.await.impl.VirtualThreadContext;
import io.vertx.await.impl.DefaultScheduler;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.Lock;

public class Async {
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void lock(Lock lock) {
ctx.lock(lock);
}

public static <T> T await(CompletionStage<T> future) {
public static <T> T await(CompletableFuture<T> future) {
VirtualThreadContext ctx = virtualThreadContext();
return ctx.await(future);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ public void lock(Lock lock) {
}
}

public <T> T await(CompletionStage<T> fut) {
public <T> T await(CompletableFuture<T> fut) {
if (fut.state() == java.util.concurrent.Future.State.SUCCESS) {
return fut.resultNow();
}
if (fut.state() == java.util.concurrent.Future.State.FAILED) {
throwAsUnchecked(fut.exceptionNow());
return null;
}
inThread.remove();
Consumer<Runnable> cont = scheduler.unschedule();
CompletableFuture<T> latch = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public abstract class VirtualThreadContextTestBase extends VertxTestBase {
Expand Down Expand Up @@ -109,14 +110,49 @@ public void testAwaitCompoundFuture() {
await();
}

@Test
public void testImmediateCompletedFuture() {
var flag = new AtomicInteger();
async.run(v -> {
var completed = Future.succeededFuture("HELLO");
vertx.getOrCreateContext().runOnContext(v2 -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this we can simply call testComplete after the await

assertEquals(2, flag.incrementAndGet());
testComplete();
});
Async.await(completed);
assertEquals(1, flag.incrementAndGet());
});
await();
}

@Test
public void testImmediateFailedFuture() {
var flag = new AtomicInteger();
async.run(v -> {
var completed = Future.failedFuture("FAILED");
vertx.getOrCreateContext().runOnContext(v2 -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this, we can simply call testComplete in the catch block

assertEquals(2, flag.incrementAndGet());
testComplete();
});
try {
Async.await(completed);
} catch (Throwable t) {
assertEquals(1, flag.incrementAndGet());
return;
}
fail("shouldn't reach");
});
await();
}

@Test
public void testDuplicateUseSameThread() {
int num = 1000;
waitFor(num);
async.run(v -> {
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
Thread th = Thread.currentThread();
for (int i = 0;i < num;i++) {
for (int i = 0; i < num; i++) {
ContextInternal duplicate = context.duplicate();
duplicate.runOnContext(v2 -> {
// assertSame(th, Thread.currentThread());
Expand All @@ -135,7 +171,7 @@ public void testDuplicateConcurrentAwait() {
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
Object lock = new Object();
List<Promise<Void>> list = new ArrayList<>();
for (int i = 0;i < num;i++) {
for (int i = 0; i < num; i++) {
ContextInternal duplicate = context.duplicate();
duplicate.runOnContext(v2 -> {
Promise<Void> promise = duplicate.promise();
Expand Down