-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix test proxy network calls #45130
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
Draft
billwert
wants to merge
18
commits into
Azure:main
Choose a base branch
from
billwert:fix-test-proxy-network-calls
base: main
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.
+158
−49
Draft
Fix test proxy network calls #45130
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
81765bf
Properly handle closeable HttpResponses in test proxy paths.
billwert ab64835
spotless
billwert 2340045
audit log
billwert bdaacf8
remove extra shutdown
billwert c31183b
more loogging
billwert c2248d1
wiretap
billwert 4a3b617
move extra logging to parent pom
billwert 4498878
try okhttp for everything just to see
billwert d944459
also ignore Connect header
billwert 9e1bb9c
one more header diff
billwert f353fb3
ever more logging
billwert 0c65528
need to be clever-er about tests with no matchers and also tests with…
billwert 37e381d
swap back to netty to gather more log data
billwert d775c4d
try handling the inactive state
billwert e5f0de4
add a logger
billwert f43f8d8
remove the handler
billwert 2ca4325
fix build for testing
billwert 9ceb1a5
more build fixes
billwert 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
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import com.azure.core.test.utils.HttpURLConnectionHttpClient; | ||
import com.azure.core.test.utils.TestProxyUtils; | ||
import com.azure.core.util.Context; | ||
import com.azure.core.util.logging.ClientLogger; | ||
import com.azure.json.JsonProviders; | ||
import com.azure.json.JsonWriter; | ||
import reactor.core.publisher.Mono; | ||
|
@@ -32,6 +33,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.azure.core.test.utils.TestProxyUtils.checkForTestProxyErrors; | ||
import static com.azure.core.test.utils.TestProxyUtils.createAddSanitizersRequest; | ||
|
@@ -43,6 +45,7 @@ | |
* A {@link HttpPipelinePolicy} for redirecting traffic through the test proxy for recording. | ||
*/ | ||
public class TestProxyRecordPolicy implements HttpPipelinePolicy { | ||
private static final ClientLogger LOGGER = new ClientLogger(TestProxyRecordPolicy.class); | ||
private static final HttpHeaderName X_RECORDING_ID = HttpHeaderName.fromString("x-recording-id"); | ||
private final HttpClient client; | ||
private final URL proxyUrl; | ||
|
@@ -79,7 +82,7 @@ public void startRecording(File recordFile, Path testClassPath) { | |
.setBody(new RecordFilePayload(recordFile.toString(), assetJsonPath).toJsonString()) | ||
.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"); | ||
|
||
try (HttpResponse response = client.sendSync(request, Context.NONE)) { | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
if (response.getStatusCode() != 200) { | ||
throw new RuntimeException(response.getBodyAsBinaryData().toString()); | ||
|
@@ -99,7 +102,9 @@ private void setDefaultRecordingOptions() { | |
HttpRequest request = new HttpRequest(HttpMethod.POST, proxyUrl + "/Admin/SetRecordingOptions") | ||
.setBody("{\"HandleRedirects\": false}") | ||
.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"); | ||
client.sendSync(request, Context.NONE).close(); | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -113,7 +118,7 @@ public void stopRecording(Queue<String> variables) { | |
.setHeader(X_RECORDING_ID, xRecordingId) | ||
.setBody(serializeVariables(variables)); | ||
|
||
try (HttpResponse response = client.sendSync(request, Context.NONE)) { | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
if (response.getStatusCode() != 200) { | ||
throw new RuntimeException(response.getBodyAsBinaryData().toString()); | ||
|
@@ -197,7 +202,9 @@ public void addProxySanitization(List<TestProxySanitizer> sanitizers) { | |
HttpRequest request | ||
= createAddSanitizersRequest(sanitizers, proxyUrl).setHeader(X_RECORDING_ID, xRecordingId); | ||
|
||
client.sendSync(request, Context.NONE).close(); | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
} | ||
} else { | ||
this.sanitizers.addAll(sanitizers); | ||
} | ||
|
@@ -227,7 +234,9 @@ public void removeProxySanitization(List<String> sanitizers) { | |
throw new RuntimeException(e); | ||
} | ||
|
||
client.sendSync(request, Context.NONE).close(); | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -241,9 +250,48 @@ public void setRecordingOptions(TestProxyRecordingOptions testProxyRecordingOpti | |
HttpRequest request = new HttpRequest(HttpMethod.POST, proxyUrl + "/admin/setrecordingoptions") | ||
.setBody(testProxyRecordingOptions.toJsonString()) | ||
.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"); | ||
client.sendSync(request, Context.NONE).close(); | ||
try (HttpResponse response = sendRequestWithRetries(request)) { | ||
checkForTestProxyErrors(response); | ||
} | ||
} catch (IOException ex) { | ||
throw new IllegalArgumentException("Failed to process JSON input", ex); | ||
} | ||
} | ||
|
||
private HttpResponse sendRequestWithRetries(HttpRequest request) { | ||
int retries = 0; | ||
while (true) { | ||
try { | ||
HttpResponse response = client.sendSync(request, Context.NONE); | ||
if (response.getStatusCode() / 100 != 2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a helper since it's used multiple times |
||
String body = response.getBodyAsString().block(); | ||
int statusCode = response.getStatusCode(); | ||
// We don't generally want to close the response here as the caller will need it, | ||
// but if we're throwing we should clean it up. | ||
response.close(); | ||
throw new RuntimeException( | ||
"Test proxy returned a non-successful status code. " + statusCode + "; response: " + body); | ||
} | ||
return response; | ||
} catch (Exception e) { | ||
|
||
LOGGER.error("Failed in the retry loop. RecordingID " + xRecordingId + " message: " + e.getMessage()); | ||
|
||
retries++; | ||
if (retries >= 3) { | ||
throw e; | ||
} | ||
sleep(1); | ||
LOGGER.warning("Retrying request to test proxy. Retry attempt: " + retries); | ||
} | ||
} | ||
} | ||
|
||
private void sleep(int durationInSeconds) { | ||
try { | ||
TimeUnit.SECONDS.sleep(durationInSeconds); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to remove this commented out code or if not add another comment above explaining when someone would want to make a swap here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely. This is really a draft PR at this point as we continue to chase down the problems. I'll clean a lot of things up when I'm done.