Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert HttpClientTest to JUnit #3652

Merged
merged 14 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
…nstrumentation into java-httpclient-test
  • Loading branch information
Anuraag Agrawal committed Jul 22, 2021
commit 3720f4f5d4cba147ad2e9a93bb51b1962721d824
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class RatpackHttpClientTest extends HttpClientTest<Void> implements AgentTestTra

@Override
Throwable clientSpanError(URI uri, Throwable exception) {
switch (uri.toString()) {
case "https://192.0.2.1/": // non routable address
if (uri.toString() == "https://192.0.2.1/") {
return new ConnectTimeoutException("connection timed out: /192.0.2.1:443")
return
} else if (uri.getPath() == "/read-timeout") {
return new ReadTimeoutException()
}
return exception
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
return HttpClientTest.this.testRemoteConnection()
}

@Override
protected boolean testReadTimeout() {
return HttpClientTest.this.testReadTimeout()
}

@Override
protected boolean testHttps() {
return HttpClientTest.this.testHttps()
Expand Down Expand Up @@ -443,14 +448,14 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
true
}

boolean testReadTimeout() {
false
}

boolean testRemoteConnection() {
true
}

boolean testReadTimeout() {
false
}

boolean testHttps() {
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ protected final Duration connectTimeout() {
return Duration.ofSeconds(5);
}

protected final Duration readTimeout() {
return Duration.ofSeconds(2);
}

private InstrumentationTestRunner testing;
private HttpClientTestServer server;

Expand Down Expand Up @@ -699,6 +703,70 @@ void connectionErrorNonRoutableAddress() {
});
}

@Test
void readTimeout() {
assumeTrue(testReadTimeout());

String method = "GET";
URI uri = resolveAddress("/read-timeout");

Throwable thrown =
catchThrowable(() -> testing.runWithSpan("parent", () -> doRequest(method, uri)));
final Throwable ex;
if (thrown instanceof ExecutionException) {
ex = thrown.getCause();
} else {
ex = thrown;
}
Throwable clientError = clientSpanError(uri, ex);

testing.waitAndAssertTraces(
trace -> {
// Workaroud until release of
// https://github.com/open-telemetry/opentelemetry-java/pull/3386
// in 1.5
List<List<SpanData>> traces = testing.traces();
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("parent")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasStatus(StatusData.error())
// Workaround until release of
// https://github.com/open-telemetry/opentelemetry-java/pull/3409
// in 1.5
.hasEventsSatisfyingExactly(
event ->
event
.hasName(SemanticAttributes.EXCEPTION_EVENT_NAME)
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.EXCEPTION_TYPE,
ex.getClass().getCanonicalName())
.containsEntry(
SemanticAttributes.EXCEPTION_MESSAGE,
ex.getMessage()))),
span ->
assertClientSpan(span, uri, method, null)
.hasParentSpanId(traces.get(0).get(0).getSpanId())
.hasEventsSatisfyingExactly(
event ->
event
.hasName(SemanticAttributes.EXCEPTION_EVENT_NAME)
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
.containsEntry(
SemanticAttributes.EXCEPTION_TYPE,
clientError.getClass().getCanonicalName())
.containsEntry(
SemanticAttributes.EXCEPTION_MESSAGE,
clientError.getMessage()))));
});
}

@DisabledIfSystemProperty(
named = "java.vm.name",
matches = ".*IBM J9 VM.*",
Expand Down Expand Up @@ -1133,6 +1201,10 @@ protected boolean testConnectionFailure() {
return true;
}

protected boolean testReadTimeout() {
return false;
}

protected boolean testRemoteConnection() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileInputStream;
import java.net.URI;
import java.security.KeyStore;
import java.time.Duration;
import javax.net.ssl.KeyManagerFactory;

public final class HttpClientTestServer extends ServerExtension {
Expand Down Expand Up @@ -83,6 +84,10 @@ protected void configure(ServerBuilder sb) throws Exception {
return HttpResponse.of(HttpStatus.UNAUTHORIZED, PLAIN_TEXT_UTF_8, "Unauthorized");
})
.service("/to-secured", (ctx, req) -> HttpResponse.ofRedirect(HttpStatus.FOUND, "/secured"))
.service(
"/read-timeout",
(ctx, req) ->
HttpResponse.delayed(HttpResponse.of(HttpStatus.OK), Duration.ofSeconds(20)))
.decorator(
(delegate, ctx, req) -> {
for (String field : openTelemetry.getPropagators().getTextMapPropagator().fields()) {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.