Fix flaky okhttp read timeout tests
MODIFY
instrumentation/okhttp/okhttp-2.2/javaagent/src/test/groovy/OkHttp2Test.groovy
instrumentation/okhttp/okhttp-2.2/javaagent/src/test/groovy/OkHttp2Test.groovy
@@ -22,10 +22,13 @@ import java.util.concurrent.TimeUnit
class OkHttp2Test extends HttpClientTest<Request> implements AgentTestTrait {
@Shared
def client = new OkHttpClient()
+ @Shared
+ def clientWithReadTimeout = new OkHttpClient()
def setupSpec() {
client.setConnectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
- client.setReadTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ clientWithReadTimeout.setConnectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ clientWithReadTimeout.setReadTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
}
@Override
@@ -40,12 +43,12 @@ class OkHttp2Test extends HttpClientTest<Request> implements AgentTestTrait {
@Override
int sendRequest(Request request, String method, URI uri, Map<String, String> headers) {
- return client.newCall(request).execute().code()
+ return getClient(uri).newCall(request).execute().code()
}
@Override
void sendRequestWithCallback(Request request, String method, URI uri, Map<String, String> headers, AbstractHttpClientTest.RequestResult requestResult) {
- client.newCall(request).enqueue(new Callback() {
+ getClient(uri).newCall(request).enqueue(new Callback() {
@Override
void onFailure(Request req, IOException e) {
requestResult.complete(e)
@@ -58,6 +61,13 @@ class OkHttp2Test extends HttpClientTest<Request> implements AgentTestTrait {
})
}
+ OkHttpClient getClient(URI uri) {
+ if (uri.toString().contains("/read-timeout")) {
+ return clientWithReadTimeout
+ }
+ return client
+ }
+
@Override
Set<AttributeKey<?>> httpAttributes(URI uri) {
Set<AttributeKey<?>> extra = [

MODIFY
instrumentation/okhttp/okhttp-3.0/testing/src/main/groovy/io/opentelemetry/instrumentation/okhttp/v3_0/AbstractOkHttp3Test.groovy
instrumentation/okhttp/okhttp-3.0/testing/src/main/groovy/io/opentelemetry/instrumentation/okhttp/v3_0/AbstractOkHttp3Test.groovy
@@ -28,12 +28,20 @@ abstract class AbstractOkHttp3Test extends HttpClientTest<Request> {
abstract Call.Factory createCallFactory(OkHttpClient.Builder clientBuilder)
@Shared
- Call.Factory client = createCallFactory(
- new OkHttpClient.Builder()
+ Call.Factory client = createCallFactory(getClientBuilder(false))
+ @Shared
+ Call.Factory clientWithReadTimeout = createCallFactory(getClientBuilder(true))
+
+ OkHttpClient.Builder getClientBuilder(boolean withReadTimeout) {
+ OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
- .readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.protocols(Arrays.asList(Protocol.HTTP_1_1))
- .retryOnConnectionFailure(false))
+ .retryOnConnectionFailure(false)
+ if (withReadTimeout) {
+ builder.readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ }
+ return builder
+ }
@Override
Request buildRequest(String method, URI uri, Map<String, String> headers) {
@@ -46,7 +54,7 @@ abstract class AbstractOkHttp3Test extends HttpClientTest<Request> {
@Override
int sendRequest(Request request, String method, URI uri, Map<String, String> headers) {
- def response = client.newCall(request).execute()
+ def response = getClient(uri).newCall(request).execute()
response.body().withCloseable {
return response.code()
}
@@ -54,7 +62,7 @@ abstract class AbstractOkHttp3Test extends HttpClientTest<Request> {
@Override
void sendRequestWithCallback(Request request, String method, URI uri, Map<String, String> headers, AbstractHttpClientTest.RequestResult requestResult) {
- client.newCall(request).enqueue(new Callback() {
+ getClient(uri).newCall(request).enqueue(new Callback() {
@Override
void onFailure(Call call, IOException e) {
requestResult.complete(e)
@@ -69,6 +77,13 @@ abstract class AbstractOkHttp3Test extends HttpClientTest<Request> {
})
}
+ Call.Factory getClient(URI uri) {
+ if (uri.toString().contains("/read-timeout")) {
+ return clientWithReadTimeout
+ }
+ return client
+ }
+
@Override
boolean testCircularRedirects() {
false

Fix flaky okhttp read timeout tests (#4499)