|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 9 | + |
| 10 | +import com.ning.http.client.AsyncCompletionHandler; |
| 11 | +import com.ning.http.client.AsyncHttpClient; |
| 12 | +import com.ning.http.client.AsyncHttpClientConfig; |
| 13 | +import com.ning.http.client.Request; |
| 14 | +import com.ning.http.client.RequestBuilder; |
| 15 | +import com.ning.http.client.Response; |
| 16 | +import com.ning.http.client.uri.Uri; |
| 17 | +import io.opentelemetry.api.common.AttributeKey; |
| 18 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 19 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; |
| 20 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; |
| 21 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult; |
| 22 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; |
| 23 | +import java.net.URI; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 29 | + |
| 30 | +class AsyncHttpClientTest extends AbstractHttpClientTest<Request> { |
| 31 | + |
| 32 | + @RegisterExtension |
| 33 | + public static final InstrumentationExtension testing = |
| 34 | + HttpClientInstrumentationExtension.forAgent(); |
| 35 | + |
| 36 | + private static final int CONNECTION_TIMEOUT_MS = (int) CONNECTION_TIMEOUT.toMillis(); |
| 37 | + private static final int READ_TIMEOUT_MS = (int) READ_TIMEOUT.toMillis(); |
| 38 | + |
| 39 | + private static final AsyncHttpClient client = |
| 40 | + new AsyncHttpClient( |
| 41 | + new AsyncHttpClientConfig.Builder() |
| 42 | + .setConnectTimeout(CONNECTION_TIMEOUT_MS) |
| 43 | + .setReadTimeout(READ_TIMEOUT_MS) |
| 44 | + .build()); |
| 45 | + |
| 46 | + @Override |
| 47 | + public Request buildRequest(String method, URI uri, Map<String, String> headers) { |
| 48 | + RequestBuilder requestBuilder = new RequestBuilder(method).setUri(Uri.create(uri.toString())); |
| 49 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 50 | + requestBuilder.setHeader(entry.getKey(), entry.getValue()); |
| 51 | + } |
| 52 | + return requestBuilder.build(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int sendRequest(Request request, String method, URI uri, Map<String, String> headers) |
| 57 | + throws ExecutionException, InterruptedException { |
| 58 | + return client.executeRequest(request).get().getStatusCode(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void sendRequestWithCallback( |
| 63 | + Request request, |
| 64 | + String method, |
| 65 | + URI uri, |
| 66 | + Map<String, String> headers, |
| 67 | + HttpClientResult requestResult) { |
| 68 | + client.executeRequest( |
| 69 | + request, |
| 70 | + new AsyncCompletionHandler<Void>() { |
| 71 | + @Override |
| 72 | + public Void onCompleted(Response response) throws Exception { |
| 73 | + requestResult.complete(response.getStatusCode()); |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onThrowable(Throwable throwable) { |
| 79 | + requestResult.complete(throwable); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { |
| 86 | + optionsBuilder.disableTestRedirects(); |
| 87 | + |
| 88 | + // disable read timeout test for non latest because it is flaky with 1.9.0 |
| 89 | + if (!Boolean.getBoolean("testLatestDeps")) { |
| 90 | + optionsBuilder.disableTestReadTimeout(); |
| 91 | + } |
| 92 | + optionsBuilder.setHttpAttributes( |
| 93 | + endpoint -> { |
| 94 | + Set<AttributeKey<?>> attributes = |
| 95 | + new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES); |
| 96 | + attributes.remove(stringKey("net.protocol.name")); |
| 97 | + attributes.remove(stringKey("net.protocol.version")); |
| 98 | + return attributes; |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
0 commit comments