diff --git a/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive1Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v1_0/HibernateReactiveTest.java b/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive1Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v1_0/HibernateReactiveTest.java index c5acf36cdd..cd046ef547 100644 --- a/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive1Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v1_0/HibernateReactiveTest.java +++ b/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive1Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v1_0/HibernateReactiveTest.java @@ -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 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 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 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 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 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 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 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( diff --git a/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive2Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v2_0/HibernateReactiveTest.java b/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive2Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v2_0/HibernateReactiveTest.java index dc506f3bf1..5f237b6bc4 100644 --- a/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive2Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v2_0/HibernateReactiveTest.java +++ b/instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive2Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v2_0/HibernateReactiveTest.java @@ -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 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 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 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(