Skip to content

Commit 4644198

Browse files
authored
add waiting hook in HttpServerTest to try to solve Pekko's flakiness (#9493)
1 parent c5b6e46 commit 4644198

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

dd-java-agent/instrumentation-testing/src/main/groovy/datadog/trace/agent/test/base/HttpServerTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ abstract class HttpServerTest<SERVER> extends WithHttpServer<SERVER> {
174174
}
175175
}
176176

177+
/**
178+
* Hook method to allow subclasses to wait for request processing to complete.
179+
* This is particularly useful for asynchronous HTTP server implementations
180+
* where spans may still not be closed after the HTTP response is sent.
181+
*/
182+
protected void waitForRequestToComplete() {
183+
// Default implementation does nothing - override in subclasses as needed
184+
}
185+
177186
// used in blocking tests to check if the handler was skipped
178187
volatile boolean handlerRan
179188

@@ -1195,6 +1204,8 @@ abstract class HttpServerTest<SERVER> extends WithHttpServer<SERVER> {
11951204
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
11961205
}
11971206

1207+
waitForRequestToComplete()
1208+
11981209
expect:
11991210
response.code() == EXCEPTION.status
12001211
if (testExceptionBody()) {

dd-java-agent/instrumentation/pekko-http-1.0/src/baseTest/groovy/PekkoHttpServerInstrumentationTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ abstract class PekkoHttpServerInstrumentationTest extends HttpServerTest<PekkoHt
4040
true
4141
}
4242

43+
@Override
44+
protected void waitForRequestToComplete() {
45+
// Pekko HTTP is asynchronous, and spans may not be completed
46+
// immediately after the HTTP response is sent. Wait for the trace
47+
// to be written to avoid race conditions in trace assertions.
48+
try {
49+
// Wait up to 1 sec for the trace to be written
50+
TEST_WRITER.waitForTracesMax(1, 1)
51+
} catch (InterruptedException e) {
52+
Thread.currentThread().interrupt()
53+
// If interrupted, proceed anyway - test may still pass
54+
}
55+
}
56+
57+
4358
//@Ignore("https://github.com/DataDog/dd-trace-java/pull/5213")
4459
@Override
4560
boolean testBadUrl() {

0 commit comments

Comments
 (0)