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

Conversation

anuraaga
Copy link
Contributor

While there are some issues with verbosity of Java assertions, I think we do have the goal of presenting our test framework, the base classes, for use from Java tests, especially since we can expect external Java instrumentation to need them. Hopefully it's ok.

The Groovy API is mostly preserved, but the big difference is the assertion failures. The assertions print for each layer in the stack (traces, trace, span, attributes for example) and tend to have the most important information at the bottom. This isn't so terrible except that if there is an exception event in the data, then a huge exception stack trace gets printed multiple times. I'd like to continue to improve on this in the sdk-testing package (and am glad to have fixed a few gaps through the process of migrating this test). On the bright side, at least for me, an assertion failure like "NET_PEER_PORT expected to be <443> but was <-1>" is much easier to understand how to fix than the Spock assertions that dump a lot of information and have dashes trying to point out what is wrong.

A big cleanup I'll follow up with is replacing all the Test subclass method knobs with a single HttpClientOptions / Builder class which will reduce the forwarding, and also improve the readability of the subclasses I think.

I'll also look through the TestRunner code a bit, I moved some code around in this PR but think there is still a bit more shuffling to make it consistent (some logic that could have been in runner was duplicated in the JUnit and Spock calling classes).

Copy link
Member

@trask trask left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there are some issues with verbosity of Java assertions, I think we do have the goal of presenting our test framework, the base classes, for use from Java tests, especially since we can expect external Java instrumentation to need them. Hopefully it's ok.

👍

I'd like to continue to improve on this in the sdk-testing package (and am glad to have fixed a few gaps through the process of migrating this test).

all of these improvements in sdk-testing is great 👍

SemanticAttributes.HTTP_USER_AGENT
]
}

def "basic #method request #url"() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def "basic #method request #url"() {
def "basic GET request #path"() {

Anuraag Agrawal added 3 commits July 26, 2021 13:12
@anuraaga
Copy link
Contributor Author

Thanks @trask. @mateuszrzeszutek would be great to get your review too since this touches the test runner stuff :)

Comment on lines +46 to +50
/** Return a list of all captured traces, where each trace is a sorted list of spans. */
default List<List<SpanData>> traces() {
return TelemetryDataUtil.groupTraces(getExportedSpans());
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this method @Deprecated? It won't be used after sdk-testing 1.5.0 releases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards not bothering for these alpha modules - I figure there is lots of similar cruft that could be marked deprecated but we'll just remove :P

* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.testing.junit;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: WDYT about putting all http test related classes into ...testing.junit.http package?

import io.opentelemetry.instrumentation.testing.LibraryTestRunner;
import org.junit.jupiter.api.extension.ExtensionContext;

public final class HttpClientLibraryInstrumentationExtension extends InstrumentationExtension {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpClientLibraryInstrumentationExtension is very similar to HttpClientAgentInstrumentationExtension - I'm wondering if we can have sth like this instead:

public static InstrumentationExtension agent() {
  return new HttpClientInstrumentationExtension(AgentTestRunner.instance());
}

public static InstrumentationExtension library() {
  return new HttpClientInstrumentationExtension(LibraryTestRunner.instance());
}

The same could be done for AgentInstrumentationExtension/LibraryInstrumentationExtension. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Will do the same for the current extensions in a different PR

});
}

testing.waitAndAssertTraces(assertions.toArray(new Consumer[0]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a waitAndAssertTraces(Collection< TraceAssert>) variant?

return sendRequest(request, method, uri, headers);
}

// ideally private, but then groovy closures in this class cannot find them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment no longer applies?

Suggested change
// ideally private, but then groovy closures in this class cannot find them

where:
method = "GET"
expect:
junitTest.basicRequestWith2Redirects()
}

def "basic #method request with circular redirects"() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def "basic #method request with circular redirects"() {
def "basic request with circular redirects"() {

assumeTrue(testRedirects())
assumeTrue(testCircularRedirects())
expect:
junitTest.circularRedirects()
}

def "redirect #method to secured endpoint copies auth header"() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def "redirect #method to secured endpoint copies auth header"() {
def "redirect to secured endpoint copies auth header"() {

}

def "high concurrency test with callback"() {
setup:
assumeTrue(testCausality())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to duplicate assumptions? The JUnit delegate test already calls them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the jupiter assumption exception is not recognized by junit4 / spock :(

//which is guaranteed to use the same connection for all requests
SingleConnection createSingleConnection(String host, int port) {
return null
// ideally private, but then groovy closures in this class cannot find them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still valid? This class doesn't call doRequest(), tests extending this one call it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think so - going to look in a followup on whether this method can be removed

return exception
}

// parent span must be cast otherwise it breaks debugging classloading (junit loads it early)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still valid? Now that we have a separate testing java agent there should be no problem with loading that stuff early (probably...)

Anuraag Agrawal added 3 commits July 27, 2021 15:24
@anuraaga anuraaga merged commit 47be4a1 into open-telemetry:main Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants