Skip to content

Commit 5afe4d2

Browse files
authored
Improve error handling in hibernate reactive tests (#9551)
1 parent f050188 commit 5afe4d2

File tree

2 files changed

+44
-26
lines changed
  • instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src

2 files changed

+44
-26
lines changed

instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive1Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v1_0/HibernateReactiveTest.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
2121
import io.vertx.core.Vertx;
2222
import java.time.Duration;
23-
import java.util.concurrent.CountDownLatch;
23+
import java.util.concurrent.CompletableFuture;
2424
import java.util.concurrent.TimeUnit;
2525
import javax.persistence.EntityManagerFactory;
2626
import javax.persistence.Persistence;
@@ -119,7 +119,7 @@ void testMutiny() {
119119

120120
@Test
121121
void testStage() throws Exception {
122-
CountDownLatch latch = new CountDownLatch(1);
122+
CompletableFuture<Object> result = new CompletableFuture<>();
123123
testing.runWithSpan(
124124
"parent",
125125
() ->
@@ -136,15 +136,15 @@ void testStage() throws Exception {
136136
.find(Value.class, 1L)
137137
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
138138
})
139-
.thenAccept(unused -> latch.countDown())));
140-
latch.await(30, TimeUnit.SECONDS);
139+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
140+
result.get(30, TimeUnit.SECONDS);
141141

142142
assertTrace();
143143
}
144144

145145
@Test
146146
void testStageWithStatelessSession() throws Exception {
147-
CountDownLatch latch = new CountDownLatch(1);
147+
CompletableFuture<Object> result = new CompletableFuture<>();
148148
testing.runWithSpan(
149149
"parent",
150150
() ->
@@ -161,15 +161,15 @@ void testStageWithStatelessSession() throws Exception {
161161
.get(Value.class, 1L)
162162
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
163163
})
164-
.thenAccept(unused -> latch.countDown())));
165-
latch.await(30, TimeUnit.SECONDS);
164+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
165+
result.get(30, TimeUnit.SECONDS);
166166

167167
assertTrace();
168168
}
169169

170170
@Test
171171
void testStageSessionWithTransaction() throws Exception {
172-
CountDownLatch latch = new CountDownLatch(1);
172+
CompletableFuture<Object> result = new CompletableFuture<>();
173173
testing.runWithSpan(
174174
"parent",
175175
() ->
@@ -186,15 +186,15 @@ void testStageSessionWithTransaction() throws Exception {
186186
.withTransaction(transaction -> session.find(Value.class, 1L))
187187
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
188188
})
189-
.thenAccept(unused -> latch.countDown())));
190-
latch.await(30, TimeUnit.SECONDS);
189+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
190+
result.get(30, TimeUnit.SECONDS);
191191

192192
assertTrace();
193193
}
194194

195195
@Test
196196
void testStageStatelessSessionWithTransaction() throws Exception {
197-
CountDownLatch latch = new CountDownLatch(1);
197+
CompletableFuture<Object> result = new CompletableFuture<>();
198198
testing.runWithSpan(
199199
"parent",
200200
() ->
@@ -211,15 +211,15 @@ void testStageStatelessSessionWithTransaction() throws Exception {
211211
.withTransaction(transaction -> session.get(Value.class, 1L))
212212
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
213213
})
214-
.thenAccept(unused -> latch.countDown())));
215-
latch.await(30, TimeUnit.SECONDS);
214+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
215+
result.get(30, TimeUnit.SECONDS);
216216

217217
assertTrace();
218218
}
219219

220220
@Test
221221
void testStageOpenSession() throws Exception {
222-
CountDownLatch latch = new CountDownLatch(1);
222+
CompletableFuture<Object> result = new CompletableFuture<>();
223223
testing.runWithSpan(
224224
"parent",
225225
() ->
@@ -237,15 +237,15 @@ void testStageOpenSession() throws Exception {
237237
.find(Value.class, 1L)
238238
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
239239
})
240-
.thenAccept(unused -> latch.countDown())));
241-
latch.await(30, TimeUnit.SECONDS);
240+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
241+
result.get(30, TimeUnit.SECONDS);
242242

243243
assertTrace();
244244
}
245245

246246
@Test
247247
void testStageOpenStatelessSession() throws Exception {
248-
CountDownLatch latch = new CountDownLatch(1);
248+
CompletableFuture<Object> result = new CompletableFuture<>();
249249
testing.runWithSpan(
250250
"parent",
251251
() ->
@@ -263,8 +263,8 @@ void testStageOpenStatelessSession() throws Exception {
263263
.get(Value.class, 1L)
264264
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
265265
})
266-
.thenAccept(unused -> latch.countDown())));
267-
latch.await(30, TimeUnit.SECONDS);
266+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
267+
result.get(30, TimeUnit.SECONDS);
268268

269269
assertTrace();
270270
}
@@ -273,6 +273,15 @@ private static void runWithVertx(Runnable runnable) {
273273
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
274274
}
275275

276+
private static void complete(
277+
CompletableFuture<Object> completableFuture, Object result, Throwable throwable) {
278+
if (throwable != null) {
279+
completableFuture.completeExceptionally(throwable);
280+
} else {
281+
completableFuture.complete(result);
282+
}
283+
}
284+
276285
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
277286
private static void assertTrace() {
278287
testing.waitAndAssertTraces(

instrumentation/hibernate/hibernate-reactive-1.0/javaagent/src/hibernateReactive2Test/java/io/opentelemetry/javaagent/instrumentation/hibernate/reactive/v2_0/HibernateReactiveTest.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import jakarta.persistence.EntityManagerFactory;
2323
import jakarta.persistence.Persistence;
2424
import java.time.Duration;
25-
import java.util.concurrent.CountDownLatch;
25+
import java.util.concurrent.CompletableFuture;
2626
import java.util.concurrent.TimeUnit;
2727
import org.hibernate.reactive.mutiny.Mutiny;
2828
import org.hibernate.reactive.stage.Stage;
@@ -211,7 +211,7 @@ void testStageStatelessSessionWithTransaction() throws Exception {
211211

212212
@Test
213213
void testStageOpenSession() throws Exception {
214-
CountDownLatch latch = new CountDownLatch(1);
214+
CompletableFuture<Object> result = new CompletableFuture<>();
215215
testing.runWithSpan(
216216
"parent",
217217
() ->
@@ -229,15 +229,15 @@ void testStageOpenSession() throws Exception {
229229
.find(Value.class, 1L)
230230
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
231231
})
232-
.thenAccept(unused -> latch.countDown())));
233-
latch.await(30, TimeUnit.SECONDS);
232+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
233+
result.get(30, TimeUnit.SECONDS);
234234

235235
assertTrace();
236236
}
237237

238238
@Test
239239
void testStageOpenStatelessSession() throws Exception {
240-
CountDownLatch latch = new CountDownLatch(1);
240+
CompletableFuture<Object> result = new CompletableFuture<>();
241241
testing.runWithSpan(
242242
"parent",
243243
() ->
@@ -255,8 +255,8 @@ void testStageOpenStatelessSession() throws Exception {
255255
.get(Value.class, 1L)
256256
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
257257
})
258-
.thenAccept(unused -> latch.countDown())));
259-
latch.await(30, TimeUnit.SECONDS);
258+
.whenComplete((value, throwable) -> complete(result, value, throwable))));
259+
result.get(30, TimeUnit.SECONDS);
260260

261261
assertTrace();
262262
}
@@ -265,6 +265,15 @@ private static void runWithVertx(Runnable runnable) {
265265
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
266266
}
267267

268+
private static void complete(
269+
CompletableFuture<Object> completableFuture, Object result, Throwable throwable) {
270+
if (throwable != null) {
271+
completableFuture.completeExceptionally(throwable);
272+
} else {
273+
completableFuture.complete(result);
274+
}
275+
}
276+
268277
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
269278
private static void assertTrace() {
270279
testing.waitAndAssertTraces(

0 commit comments

Comments
 (0)