Skip to content

Commit c3d1fc1

Browse files
committed
Fix apache-httpasyncclient test flakiness with httpasyncclient 4.1.5
Move CloseableHttpAsyncClient creation from @BeforeAll (shared across all tests in the class) to @beforeeach (fresh per test). httpasyncclient 4.1.5 with httpcore-nio 4.4.15 can leave the underlying I/O reactor in a STOPPED state after certain connection-error tests, which causes every subsequent request on the same client to fail with 'Request execution cancelled'. Using a fresh client per test isolates the reactor so one test's failure mode can't cascade.
1 parent 837193a commit c3d1fc1

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

  • instrumentation/apache-httpasyncclient-4.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/v4_1

instrumentation/apache-httpasyncclient-4.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/v4_1/ApacheHttpAsyncClientTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,40 @@
2424
import org.apache.http.impl.nio.client.HttpAsyncClients;
2525
import org.apache.http.message.BasicHeader;
2626
import org.apache.http.nio.client.HttpAsyncClient;
27-
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Nested;
29-
import org.junit.jupiter.api.TestInstance;
3029
import org.junit.jupiter.api.extension.RegisterExtension;
3130

32-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
3331
class ApacheHttpAsyncClientTest {
3432

3533
@RegisterExtension
3634
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
3735

3836
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
3937

40-
private final RequestConfig requestConfig =
38+
private static final RequestConfig requestConfig =
4139
RequestConfig.custom()
4240
.setConnectTimeout((int) AbstractHttpClientTest.CONNECTION_TIMEOUT.toMillis())
4341
.build();
4442

45-
private final RequestConfig requestWithReadTimeoutConfig =
43+
private static final RequestConfig requestWithReadTimeoutConfig =
4644
RequestConfig.copy(requestConfig)
4745
.setSocketTimeout((int) AbstractHttpClientTest.READ_TIMEOUT.toMillis())
4846
.build();
4947

50-
private final CloseableHttpAsyncClient client =
51-
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
52-
private final CloseableHttpAsyncClient clientWithReadTimeout =
53-
HttpAsyncClients.custom().setDefaultRequestConfig(requestWithReadTimeoutConfig).build();
48+
private CloseableHttpAsyncClient client;
49+
private CloseableHttpAsyncClient clientWithReadTimeout;
5450

55-
@BeforeAll
51+
// Use a fresh client per test: with httpasyncclient 4.1.5 a failing test can leave the
52+
// shared I/O reactor in a STOPPED state, cascading "Request execution cancelled" failures
53+
// into every subsequent test.
54+
@BeforeEach
5655
void setUp() {
57-
cleanup.deferAfterAll(client);
58-
cleanup.deferAfterAll(clientWithReadTimeout);
56+
client = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
57+
clientWithReadTimeout =
58+
HttpAsyncClients.custom().setDefaultRequestConfig(requestWithReadTimeoutConfig).build();
59+
cleanup.deferCleanup(client);
60+
cleanup.deferCleanup(clientWithReadTimeout);
5961
client.start();
6062
clientWithReadTimeout.start();
6163
}

0 commit comments

Comments
 (0)