Improve error handling in hibernate reactive tests (#9551)

This commit is contained in:
Lauri Tulmin 2023-09-26 09:18:16 +03:00 committed by GitHub
parent f0501888b4
commit 5afe4d2035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 26 deletions

View File

@ -20,7 +20,7 @@ import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtens
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.vertx.core.Vertx;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@ -119,7 +119,7 @@ class HibernateReactiveTest {
@Test
void testStage() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -136,15 +136,15 @@ class HibernateReactiveTest {
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageWithStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -161,15 +161,15 @@ class HibernateReactiveTest {
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageSessionWithTransaction() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -186,15 +186,15 @@ class HibernateReactiveTest {
.withTransaction(transaction -> session.find(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageStatelessSessionWithTransaction() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -211,15 +211,15 @@ class HibernateReactiveTest {
.withTransaction(transaction -> session.get(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageOpenSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -237,15 +237,15 @@ class HibernateReactiveTest {
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageOpenStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -263,8 +263,8 @@ class HibernateReactiveTest {
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@ -273,6 +273,15 @@ class HibernateReactiveTest {
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
}
private static void complete(
CompletableFuture<Object> completableFuture, Object result, Throwable throwable) {
if (throwable != null) {
completableFuture.completeExceptionally(throwable);
} else {
completableFuture.complete(result);
}
}
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
private static void assertTrace() {
testing.waitAndAssertTraces(

View File

@ -22,7 +22,7 @@ import io.vertx.core.Vertx;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.hibernate.reactive.mutiny.Mutiny;
import org.hibernate.reactive.stage.Stage;
@ -211,7 +211,7 @@ class HibernateReactiveTest {
@Test
void testStageOpenSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -229,15 +229,15 @@ class HibernateReactiveTest {
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@Test
void testStageOpenStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<Object> result = new CompletableFuture<>();
testing.runWithSpan(
"parent",
() ->
@ -255,8 +255,8 @@ class HibernateReactiveTest {
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);
.whenComplete((value, throwable) -> complete(result, value, throwable))));
result.get(30, TimeUnit.SECONDS);
assertTrace();
}
@ -265,6 +265,15 @@ class HibernateReactiveTest {
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
}
private static void complete(
CompletableFuture<Object> completableFuture, Object result, Throwable throwable) {
if (throwable != null) {
completableFuture.completeExceptionally(throwable);
} else {
completableFuture.complete(result);
}
}
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
private static void assertTrace() {
testing.waitAndAssertTraces(