-
Notifications
You must be signed in to change notification settings - Fork 361
Fix Java HTTP async continuation lifecycle #12498
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
Open
amarziali
wants to merge
4
commits into
master
Choose a base branch
from
andrea.marziali/diag-fix-java-http-async
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...t-11.0/src/test/java/datadog/trace/instrumentation/httpclient/BodyHandlerWrapperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| package datadog.trace.instrumentation.httpclient; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.context.ContextContinuation; | ||
| import datadog.context.ContextKey; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import java.lang.reflect.Proxy; | ||
| import java.net.http.HttpResponse.BodySubscriber; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionStage; | ||
| import java.util.concurrent.Flow; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class BodyHandlerWrapperTest { | ||
|
|
||
| @Test | ||
| void holdsContextAcrossCallbacksUntilCompletion() { | ||
| Context original = Context.current(); | ||
| Context captured = original.with(ContextKey.named("response-body"), new Object()); | ||
| ContextContinuation continuation = captured.capture(); | ||
| RecordingSubscriber subscriber = new RecordingSubscriber(); | ||
| BodySubscriber<Void> wrapper = wrap(subscriber, continuation); | ||
|
|
||
| try { | ||
| wrapper.onNext(List.of()); | ||
| assertSame(captured, subscriber.callbackContexts.get(0)); | ||
| assertSame(original, Context.current()); | ||
|
|
||
| wrapper.onNext(List.of()); | ||
| assertSame(captured, subscriber.callbackContexts.get(1)); | ||
| assertSame(original, Context.current()); | ||
|
|
||
| wrapper.onComplete(); | ||
| assertSame(captured, subscriber.callbackContexts.get(2)); | ||
| assertSame(original, Context.current()); | ||
|
|
||
| // A released continuation must no longer reactivate the captured context. | ||
| try (ContextScope ignored = continuation.resume()) { | ||
| assertSame(original, Context.current()); | ||
| } | ||
| } finally { | ||
| continuation.release(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void releasesContinuationWhenOnSubscribeThrows() { | ||
| RecordingContinuation continuation = new RecordingContinuation(); | ||
| RecordingSubscriber subscriber = new RecordingSubscriber(); | ||
| subscriber.throwOnSubscribe = true; | ||
| BodySubscriber<Void> wrapper = wrap(subscriber, continuation); | ||
|
|
||
| assertThrows( | ||
| IllegalStateException.class, () -> wrapper.onSubscribe(new RecordingSubscription())); | ||
| assertEquals(1, continuation.released); | ||
| wrapper.onComplete(); | ||
|
|
||
| assertEquals(1, continuation.released); | ||
| } | ||
|
|
||
| @Test | ||
| void releasesContinuationWhenOnNextThrows() { | ||
| RecordingContinuation continuation = new RecordingContinuation(); | ||
| RecordingSubscriber subscriber = new RecordingSubscriber(); | ||
| subscriber.throwOnNext = true; | ||
| BodySubscriber<Void> wrapper = wrap(subscriber, continuation); | ||
|
|
||
| assertThrows(IllegalStateException.class, () -> wrapper.onNext(List.of())); | ||
| assertEquals(1, continuation.released); | ||
| wrapper.onComplete(); | ||
|
|
||
| assertEquals(1, continuation.released); | ||
| } | ||
|
|
||
| @Test | ||
| void releasesContinuationWhenSubscriptionIsCancelled() { | ||
| RecordingContinuation continuation = new RecordingContinuation(); | ||
| RecordingSubscriber subscriber = new RecordingSubscriber(); | ||
| BodySubscriber<Void> wrapper = wrap(subscriber, continuation); | ||
| RecordingSubscription subscription = new RecordingSubscription(); | ||
|
|
||
| wrapper.onSubscribe(subscription); | ||
| subscriber.subscription.request(3); | ||
| subscriber.subscription.cancel(); | ||
| wrapper.onComplete(); | ||
|
|
||
| assertEquals(3, subscription.requested); | ||
| assertEquals(1, subscription.cancelled); | ||
| assertEquals(1, continuation.released); | ||
| } | ||
|
|
||
| private static BodySubscriber<Void> wrap( | ||
| RecordingSubscriber subscriber, ContextContinuation continuation) { | ||
| AgentSpan span = | ||
| (AgentSpan) | ||
| Proxy.newProxyInstance( | ||
| AgentSpan.class.getClassLoader(), | ||
| new Class<?>[] {AgentSpan.class}, | ||
| (proxy, method, args) -> continuation); | ||
| return new BodyHandlerWrapper<>(ignored -> subscriber, span).apply(null); | ||
| } | ||
|
|
||
| private static final class RecordingSubscriber | ||
| implements java.net.http.HttpResponse.BodySubscriber<Void> { | ||
| private final CompletableFuture<Void> body = new CompletableFuture<>(); | ||
| private final List<Context> callbackContexts = new ArrayList<>(); | ||
| private Flow.Subscription subscription; | ||
| private boolean throwOnSubscribe; | ||
| private boolean throwOnNext; | ||
|
|
||
| @Override | ||
| public CompletionStage<Void> getBody() { | ||
| return body; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSubscribe(Flow.Subscription subscription) { | ||
| this.subscription = subscription; | ||
| if (throwOnSubscribe) { | ||
| throw new IllegalStateException("onSubscribe"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(List<ByteBuffer> item) { | ||
| callbackContexts.add(Context.current()); | ||
| if (throwOnNext) { | ||
| throw new IllegalStateException("onNext"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
| body.completeExceptionally(throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| callbackContexts.add(Context.current()); | ||
| body.complete(null); | ||
| } | ||
| } | ||
|
|
||
| private static final class RecordingSubscription implements Flow.Subscription { | ||
| private long requested; | ||
| private int cancelled; | ||
|
|
||
| @Override | ||
| public void request(long count) { | ||
| requested += count; | ||
| } | ||
|
|
||
| @Override | ||
| public void cancel() { | ||
| cancelled++; | ||
| } | ||
| } | ||
|
|
||
| private static final class RecordingContinuation implements ContextContinuation { | ||
| private int released; | ||
|
|
||
| @Override | ||
| public ContextContinuation hold() { | ||
|
amarziali marked this conversation as resolved.
|
||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Context context() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public ContextScope resume() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| released++; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.