Skip to content

Commit 6ae364d

Browse files
authored
Fix FailedRequests_ConnectionClosedWhileReceivingHeaders_Recorded (#89047)
1 parent cdff572 commit 6ae364d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,10 @@ protected override void Dispose(bool disposing)
530530
base.Dispose(disposing);
531531
}
532532

533-
protected Task<HttpResponseMessage> SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request) =>
533+
protected Task<HttpResponseMessage> SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken = default) =>
534534
TestHttpMessageInvoker ?
535-
invoker.SendAsync(request, default) :
536-
((HttpClient)invoker).SendAsync(TestAsync, request);
535+
invoker.SendAsync(request, cancellationToken) :
536+
((HttpClient)invoker).SendAsync(TestAsync, request, cancellationToken);
537537

538538
protected HttpMessageInvoker CreateHttpMessageInvoker(HttpMessageHandler? handler = null) =>
539539
TestHttpMessageInvoker ?
@@ -617,24 +617,32 @@ await Assert.ThrowsAsync<HttpRequestException>(async () =>
617617
[Fact]
618618
public async Task FailedRequests_ConnectionClosedWhileReceivingHeaders_Recorded()
619619
{
620-
TimeSpan timeout = TimeSpan.FromSeconds(30);
620+
using CancellationTokenSource cancelServerCts = new CancellationTokenSource();
621621
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
622622
{
623623
using HttpMessageInvoker client = CreateHttpMessageInvoker();
624624
using InstrumentRecorder<long> recorder = SetupInstrumentRecorder<long>(InstrumentNames.FailedRequests);
625625
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
626626

627-
await Assert.ThrowsAsync<HttpRequestException>(async () =>
627+
Exception ex = await Assert.ThrowsAnyAsync<Exception>(async () =>
628628
{
629-
using HttpResponseMessage response = await SendAsync(client, request);
630-
}).WaitAsync(timeout);
629+
// Getting a cancellation is also good if we are unable to detect the peer shutdown.
630+
using CancellationTokenSource cts = new CancellationTokenSource(10_000);
631+
using HttpResponseMessage response = await SendAsync(client, request, cts.Token);
632+
});
633+
cancelServerCts.Cancel();
634+
Assert.True(ex is HttpRequestException or TaskCanceledException);
631635

632636
Measurement<long> m = recorder.GetMeasurements().Single();
633637
VerifyFailedRequests(m, 1, uri, null, null);
634638
}, async server =>
635639
{
636-
var connection = (LoopbackServer.Connection)await server.EstablishGenericConnectionAsync().WaitAsync(timeout);
637-
connection.Socket.Close();
640+
try
641+
{
642+
var connection = (LoopbackServer.Connection)await server.EstablishGenericConnectionAsync().WaitAsync(cancelServerCts.Token);
643+
connection.Socket.Close();
644+
}
645+
catch { }
638646
});
639647
}
640648
}

0 commit comments

Comments
 (0)