Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 26 additions & 9 deletions src/main/java/io/vertx/junit5/RunTestOnContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,43 @@ public class RunTestOnContext implements BeforeAllCallback, InvocationIntercepto

private final Supplier<Future<Vertx>> supplier;
private final Function<Vertx, Future<Void>> shutdown;
private final ThreadingModel threadingModel;

/**
* Create an instance of this extension that builds a {@link Vertx} object using default options.
*/
public RunTestOnContext() {
this(new VertxOptions(), false);
this(new VertxOptions(), false, ThreadingModel.EVENT_LOOP);
}

/**
* Create an instance of this extension that builds a {@link Vertx} object using default options.
*
* @param threadingModel the threading model used to run the test
*/
public RunTestOnContext(ThreadingModel threadingModel) {
this(new VertxOptions(), false, threadingModel);
}

/**
* Create an instance of this extension that builds a {@link Vertx} object using the specified {@code options}.
* <p>
* When the options hold a {@link io.vertx.core.spi.cluster.ClusterManager} instance, a clustered {@link Vertx} object is created.
*
* @param options the vertx options
* @param clustered indicate if a clustered {@link Vertx} object will be created
* @param threadingModel the threading model used to run the test
*/
public RunTestOnContext(VertxOptions options, boolean clustered) {
this(() -> clustered ? Vertx.clusteredVertx(options) : Future.succeededFuture(Vertx.vertx(options)));
public RunTestOnContext(VertxOptions options, boolean clustered, ThreadingModel threadingModel) {
this(() -> clustered ? Vertx.clusteredVertx(options) : Future.succeededFuture(Vertx.vertx(options)), threadingModel);
}

/**
* Create an instance of this extension that gets a {@link Vertx} object using the specified asynchronous {@code supplier}.
*
* @param supplier the asynchronous supplier
* @param threadingModel the threading model used to run the test
*/
public RunTestOnContext(Supplier<Future<Vertx>> supplier) {
this(supplier, Vertx::close);
public RunTestOnContext(Supplier<Future<Vertx>> supplier, ThreadingModel threadingModel) {
this(supplier, Vertx::close, threadingModel);
}

/**
Expand All @@ -78,10 +89,12 @@ public RunTestOnContext(Supplier<Future<Vertx>> supplier) {
*
* @param supplier the asynchronous supplier
* @param shutdown the asynchronous shutdown function
* @param threadingModel the threading model used to run the test
*/
public RunTestOnContext(Supplier<Future<Vertx>> supplier, Function<Vertx, Future<Void>> shutdown) {
public RunTestOnContext(Supplier<Future<Vertx>> supplier, Function<Vertx, Future<Void>> shutdown, ThreadingModel threadingModel) {
this.supplier = supplier;
this.shutdown = shutdown;
this.threadingModel = threadingModel;
}

/**
Expand Down Expand Up @@ -143,7 +156,11 @@ private <T> T runOnContext(Invocation<T> invocation) throws Throwable {
} else {
vertxFuture = supplier.get().onSuccess(vertx -> {
this.vertx = vertx;
context = ((VertxInternal) vertx).createEventLoopContext();
if (ThreadingModel.VIRTUAL_THREAD.equals(this.threadingModel)) {
context = ((VertxInternal) vertx).createVirtualThreadContext();
} else {
context = ((VertxInternal) vertx).createEventLoopContext();
}
});
}
CompletableFuture<T> cf = vertxFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import io.vertx.junit5.RunTestOnContext;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -43,7 +44,7 @@ public class CustomizedRunOnContextExtensionTest {
destroyMethodInvocations.incrementAndGet();
assertSame(expectedVertx, vertx);
return vertx.close();
});
}, ThreadingModel.EVENT_LOOP);

@BeforeEach
void beforeTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2021 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.vertx.junit5.tests;

import io.vertx.core.Context;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import io.vertx.junit5.RunTestOnContext;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.concurrent.atomic.AtomicReference;

import static io.vertx.junit5.tests.StaticRunOnContextExtensionTest.checkContext;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

public class RunOnContextExtensionThreadingModelTest {

@RegisterExtension
RunTestOnContext testOnContext = new RunTestOnContext(ThreadingModel.VIRTUAL_THREAD);

AtomicReference<Context> ctxRef = new AtomicReference<>();

@BeforeAll
static void beforeAll() {
assertNull(Vertx.currentContext());
}

public RunOnContextExtensionThreadingModelTest() {
assertNull(Vertx.currentContext());
}

@BeforeEach
void beforeTest() {
Context ctx = Vertx.currentContext();
assertNotNull(ctx);
assertSame(ctx.owner(), testOnContext.vertx());
assertNotSame(ctx, ctxRef.getAndSet(ctx));
}

@Test
void testMethod1() {
checkContext(testOnContext.vertx(), ctxRef.get());
}

@Test
void testMethod2() {
checkContext(testOnContext.vertx(), ctxRef.get());
}

@AfterEach
void tearDown() {
checkContext(testOnContext.vertx(), ctxRef.get());
}

@AfterAll
static void afterAll() {
assertNull(Vertx.currentContext());
}
}