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 FailedRequests_ConnectionClosedWhileReceivingHeaders_Recorded #89047

Merged
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
Next Next commit
fix FailedRequests_ConnectionClosedWhileReceivingHeaders_Recorded
  • Loading branch information
antonfirsov committed Jul 17, 2023
commit b61859e232a05dcf2bc68435a5c498e21999ec03
24 changes: 15 additions & 9 deletions src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,10 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

protected Task<HttpResponseMessage> SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request) =>
protected Task<HttpResponseMessage> SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken = default) =>
TestHttpMessageInvoker ?
invoker.SendAsync(request, default) :
((HttpClient)invoker).SendAsync(TestAsync, request);
invoker.SendAsync(request, cancellationToken) :
((HttpClient)invoker).SendAsync(TestAsync, request, cancellationToken);

protected HttpMessageInvoker CreateHttpMessageInvoker(HttpMessageHandler? handler = null) =>
TestHttpMessageInvoker ?
Expand Down Expand Up @@ -617,24 +617,30 @@ await Assert.ThrowsAsync<HttpRequestException>(async () =>
[Fact]
public async Task FailedRequests_ConnectionClosedWhileReceivingHeaders_Recorded()
{
TimeSpan timeout = TimeSpan.FromSeconds(30);
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<long> recorder = SetupInstrumentRecorder<long>(InstrumentNames.FailedRequests);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };

await Assert.ThrowsAsync<HttpRequestException>(async () =>
Exception ex = await Assert.ThrowsAnyAsync<Exception>(async () =>
{
using HttpResponseMessage response = await SendAsync(client, request);
}).WaitAsync(timeout);
// Getting a cancellation is also good if we are unable to detect the peer shutdown.
using CancellationTokenSource cts = new CancellationTokenSource(1000);
Copy link
Member

Choose a reason for hiding this comment

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

preferably, we should not use magic numbers. Can we find some known passing/failing constant? .

Copy link
Member Author

@antonfirsov antonfirsov Jul 17, 2023

Choose a reason for hiding this comment

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

We only have TestHelper.PassingTestTimeoutMilliseconds = 60sec. This is too much, if we don't receive an RST from the server, I want the cancellation to fire relatively quickly while still making sure MetricsHandler is reached in the handler chain. Changed it to 10sec. I wasn't able to find a constant with similar semantics.

using HttpResponseMessage response = await SendAsync(client, request, cts.Token);
});
Assert.True(ex is HttpRequestException or TaskCanceledException);

Measurement<long> m = recorder.GetMeasurements().Single();
VerifyFailedRequests(m, 1, uri, null, null);
}, async server =>
{
var connection = (LoopbackServer.Connection)await server.EstablishGenericConnectionAsync().WaitAsync(timeout);
connection.Socket.Close();
try
{
var connection = (LoopbackServer.Connection)await server.EstablishGenericConnectionAsync();
connection.Socket.Close();
}
catch { }
});
}
}
Expand Down