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

fix #4201: generalizing sendAsync support #4430

Merged
merged 10 commits into from Nov 29, 2022
Merged

Conversation

shawkins
Copy link
Contributor

@shawkins shawkins commented Sep 19, 2022

Description

Fix #4201
Fix #4564

This is to address #4201. It keeps the calling contract the same and introduces default logic built on top of the consumeBytes method. We need two new handlers for that - one for collecting all of the bytes, and one to act as a ReadableByteChannel (which is convertable to input stream). We may later want to refine this so that the HttpResponseAdapter isn't necessary by separating the responsibility of the body representation from the HttpResponse.

Most of the logic related to sendAsync in the client implementations was removed.

To cut down on the variation in the implementations, we'll forcibly use the UTF-8 charset (jetty was looking at the encoding, which doesn't typically specify the charset, while okhttp and jdk are parsing the content-type).

We also have an issue with the jetty consumeLines implementation - it isn't actually splitting the lines. Since this is only used for http watches, I just converted it to unsupported for now.

This will also help smooth over the vert.x client support a little bit - it will no longer need to directly support InputStream/Reader.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@shawkins
Copy link
Contributor Author

shawkins commented Oct 5, 2022

Here's where things stand - sendAsync is now implemented by SendAsyncUtils and several helper classes designed to run over top of consumeBytes - HttpClientReadableByteChannel (ReadableByteChannel / InputStream / Reader) and ByteArrayBodyHandler (byte[] and String).

The consumeLines and asyncBody tests were refined to span multiple result consumptions - this highlighted that the JDK implementation needs to immediately deliver the AsyncBody result for the InputStream / Reader handling, which was the general expectation for sendAsync calls returing InputStream/Reader.

Jetty support was refined to remove blocking logic - we will use flow consumption, just like with the JDK implementation. The JDK.

…alls

also converting jetty to fully non-blocking

and adapting the body delivery for jdk for use with sendAsync logic - we
need the AsyncBody to be available immediately
@shawkins
Copy link
Contributor Author

@manusa I think this pr addresses some of the test inconsistency seen before. The jdk logic had an issue with consumeBytes not immediately returning the AsyncBody that is addressed by https://github.com/fabric8io/kubernetes-client/pull/4430/files#diff-291f7471cf12ec2bdc6787de6331b77befeca706eaf6ebfe6fdcd5110f3a3560R67 - which I think may have accounted for some of the timing issues with not seeing log results.

adding missing test timeouts
correcting the wait in HttpClientReadableByteChannel
@@ -115,6 +115,7 @@ public void onWebSocketClose(int statusCode, String reason) {

@Override
public void onWebSocketConnect(Session session) {
this.webSocketSession = session;
Copy link
Member

Choose a reason for hiding this comment

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

Could you please elaborate on this change?
I have a fade memory having trouble doing sth similar (but can't remember exactly right now, maybe accessing the session before it was set)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I started looking at my failed jetty test, I reviewed the implementation more. This change mirrors the logic Jetty's WebSocketAdapter - in particular making the session volatile.

Copy link
Member

Choose a reason for hiding this comment

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

I cannot recall what the problem was at the time, but seems to be working fine with the tests 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also onWebSocketConnect is called before the websocketclient.connect future is completed.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, as I said I can't remember exactly what or why I placed that there.
Maybe it was just an issue with the future chaining method, dunno, anyway doesn't matter.

Comment on lines 181 to 182
.extracting(HttpResponse::code).isEqualTo(400);
assertThat(result)
Copy link
Member

Choose a reason for hiding this comment

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

did you change this because it failed in Java17? or is there any other reason?

Copy link
Member

Choose a reason for hiding this comment

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

a single assertion can be preserved using .returns(400, HttpResponse::code)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was needed due to the response wrapping - with some additional refactoring that could be avoided.

Copy link
Member

Choose a reason for hiding this comment

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

OK, then I'll replace with the returns DSL method that should work

manusa and others added 2 commits November 29, 2022 15:55
- implementation of JettyHttpClient consumeLines
- additional tests
- centralization of HttpResponse body/async consumption logic
- uncalled-for nitpicks 😅😇

Signed-off-by: Marc Nuri <marc@marcnuri.com>
review: HttpClient implementation refactor
@manusa manusa added this to the 6.3.0 milestone Nov 29, 2022
@sonarcloud
Copy link

sonarcloud bot commented Nov 29, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug B 2 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

35.9% 35.9% Coverage
0.0% 0.0% Duplication

@manusa manusa merged commit 432c48a into fabric8io:master Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants