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

TransactionalUniAsserter never fails with Hibernate Reactive #36599

Merged
merged 4 commits into from
Nov 2, 2023
Merged
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
Expand Up @@ -85,9 +85,7 @@ static <T> Uni<T> withSessionOnDemand(Supplier<Uni<T>> work) {
* @return a new {@link Uni}
*/
public static <T> Uni<T> withTransaction(Supplier<Uni<T>> work) {
return withSession(s -> {
return s.withTransaction(t -> work.get());
});
return withSession(s -> s.withTransaction(t -> work.get()));
}

/**
Expand All @@ -98,9 +96,7 @@ public static <T> Uni<T> withTransaction(Supplier<Uni<T>> work) {
* @return a new {@link Uni}
*/
public static <T> Uni<T> withTransaction(Function<Transaction, Uni<T>> work) {
return withSession(s -> {
return s.withTransaction(t -> work.apply(t));
});
return withSession(s -> s.withTransaction(work));
}

/**
Expand All @@ -122,8 +118,8 @@ public static <T> Uni<T> withSession(Function<Mutiny.Session, Uni<T>> work) {
return getSessionFactory()
.openSession()
.invoke(s -> context.putLocal(key, s))
.chain(s -> work.apply(s))
.eventually(() -> closeSession());
.chain(work::apply)
.eventually(SessionOperations::closeSession);
}
}

Expand Down Expand Up @@ -152,7 +148,7 @@ public static Uni<Mutiny.Session> getSession() {
if (context.getLocal(SESSION_ON_DEMAND_KEY) != null) {
if (context.getLocal(SESSION_ON_DEMAND_OPENED_KEY) != null) {
// a new reactive session is opened in a previous stage
return Uni.createFrom().item(() -> getCurrentSession());
return Uni.createFrom().item(SessionOperations::getCurrentSession);
} else {
// open a new reactive session and store it in the vertx duplicated context
// the context was marked as "lazy" which means that the session will be eventually closed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class TransactionalUniAsserterTest {
@Test
public void testTransactionalUniAsserter(TransactionalUniAsserter asserter) {
assertNotNull(asserter);
asserter.assertThat(() -> Panache.currentTransaction(), transaction -> {
asserter.assertThat(Panache::currentTransaction, transaction -> {
assertNotNull(transaction);
assertFalse(transaction.isMarkedForRollback());
asserter.putData("tx", transaction);
});
asserter.assertThat(() -> Panache.currentTransaction(), transaction -> {
asserter.assertThat(Panache::currentTransaction, transaction -> {
assertNotNull(transaction);
assertFalse(transaction.isMarkedForRollback());
assertNotEquals(transaction, asserter.getData("tx"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class TransactionalUniAsserter extends UniAsserterInterceptor {

@Override
protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
return () -> SessionOperations.withTransaction(() -> uniSupplier.get());
return () -> SessionOperations.withTransaction(uniSupplier);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@

public class TransactionalUniAsserterTestMethodInvoker extends RunOnVertxContextTestMethodInvoker {

private UniAsserter uniAsserter;

@Override
public boolean handlesMethodParamType(String paramClassName) {
return TransactionalUniAsserter.class.getName().equals(paramClassName);
}

@Override
public Object methodParamInstance(String paramClassName) {
if (!handlesMethodParamType(paramClassName)) {
throw new IllegalStateException(
"TransactionalUniAsserterTestMethodInvoker does not handle '" + paramClassName + "' method param types");
}
uniAsserter = new TransactionalUniAsserter(new DefaultUniAsserter());
return uniAsserter;
protected UniAsserter createUniAsserter() {
return new TransactionalUniAsserter(new DefaultUniAsserter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import io.smallrye.mutiny.Uni;

public final class DefaultUniAsserter implements UniAsserter {
public final class DefaultUniAsserter implements UnwrappableUniAsserter {

private final ConcurrentMap<String, Object> data;

Expand All @@ -22,6 +22,11 @@ public DefaultUniAsserter() {
this.data = new ConcurrentHashMap<>();
}

@Override
public Uni<?> asUni() {
return execution;
}

@SuppressWarnings("unchecked")
@Override
public <T> UniAsserter assertThat(Supplier<Uni<T>> uni, Consumer<T> asserter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class RunOnVertxContextTestMethodInvoker implements TestMethodInvoker {

private DefaultUniAsserter uniAsserter;
private UniAsserter uniAsserter;

@Override
public boolean handlesMethodParamType(String paramClassName) {
Expand All @@ -32,12 +32,16 @@ public boolean handlesMethodParamType(String paramClassName) {
public Object methodParamInstance(String paramClassName) {
if (!handlesMethodParamType(paramClassName)) {
throw new IllegalStateException(
"RunOnVertxContextTestMethodInvoker does not handle '" + paramClassName + "' method param types");
this.getClass().getName() + " does not handle '" + paramClassName + "' method param types");
}
uniAsserter = new DefaultUniAsserter();
uniAsserter = createUniAsserter();
return uniAsserter;
}

protected UniAsserter createUniAsserter() {
return new DefaultUniAsserter();
}

@Override
public boolean supportsMethod(Class<?> originalTestClass, Method originalTestMethod) {
return hasSupportedAnnotation(originalTestClass, originalTestMethod)
Expand Down Expand Up @@ -152,16 +156,16 @@ public void run() {
private final Object testInstance;
private final Method targetMethod;
private final List<Object> methodArgs;
private final DefaultUniAsserter uniAsserter;
private final UnwrappableUniAsserter uniAsserter;
private final CompletableFuture<Object> future;

public RunTestMethodOnVertxEventLoopContextHandler(Object testInstance, Method targetMethod, List<Object> methodArgs,
DefaultUniAsserter uniAsserter, CompletableFuture<Object> future) {
UniAsserter uniAsserter, CompletableFuture<Object> future) {
this.testInstance = testInstance;
this.future = future;
this.targetMethod = targetMethod;
this.methodArgs = methodArgs;
this.uniAsserter = uniAsserter;
this.uniAsserter = (UnwrappableUniAsserter) uniAsserter;
}

@Override
Expand All @@ -184,7 +188,7 @@ private void doRun(Runnable onTerminate) {
try {
Object testMethodResult = targetMethod.invoke(testInstance, methodArgs.toArray(new Object[0]));
if (uniAsserter != null) {
uniAsserter.execution.subscribe().with(new Consumer<Object>() {
uniAsserter.asUni().subscribe().with(new Consumer<Object>() {
@Override
public void accept(Object o) {
onTerminate.run();
Expand All @@ -209,23 +213,20 @@ public void accept(Throwable t) {
}

public static class RunTestMethodOnVertxBlockingContextHandler implements Handler<Promise<Object>> {
private static final Runnable DO_NOTHING = new Runnable() {
@Override
public void run() {
}
private static final Runnable DO_NOTHING = () -> {
};

private final Object testInstance;
private final Method targetMethod;
private final List<Object> methodArgs;
private final DefaultUniAsserter uniAsserter;
private final UnwrappableUniAsserter uniAsserter;

public RunTestMethodOnVertxBlockingContextHandler(Object testInstance, Method targetMethod, List<Object> methodArgs,
DefaultUniAsserter uniAsserter) {
UniAsserter uniAsserter) {
this.testInstance = testInstance;
this.targetMethod = targetMethod;
this.methodArgs = methodArgs;
this.uniAsserter = uniAsserter;
this.uniAsserter = (UnwrappableUniAsserter) uniAsserter;
}

@Override
Expand All @@ -248,7 +249,7 @@ private void doRun(Promise<Object> promise, Runnable onTerminate) {
try {
Object testMethodResult = targetMethod.invoke(testInstance, methodArgs.toArray(new Object[0]));
if (uniAsserter != null) {
uniAsserter.execution.subscribe().with(new Consumer<Object>() {
uniAsserter.asUni().subscribe().with(new Consumer<Object>() {
@Override
public void accept(Object o) {
onTerminate.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* </pre>
*
*/
public abstract class UniAsserterInterceptor implements UniAsserter {
public abstract class UniAsserterInterceptor implements UnwrappableUniAsserter {

private final UniAsserter delegate;

Expand Down Expand Up @@ -190,4 +190,8 @@ public void clearData() {
delegate.clearData();
}

@Override
public Uni<?> asUni() {
return ((UnwrappableUniAsserter) delegate).asUni();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.test.vertx;

import io.smallrye.mutiny.Uni;

/**
* A {@link UniAsserter} that exposes the internal {@link Uni}.
* <p>
* We've added this interface so that we don't expose the method {@link #asUni()} to the user
* </p>
*/
interface UnwrappableUniAsserter extends UniAsserter {

/**
* @return a {@link Uni} representing the operations pipeline up to this point
*/
Uni<?> asUni();
}
Loading