From 4cc97d149355c20b45c64ca633e90e448f36138d Mon Sep 17 00:00:00 2001 From: Brennan Date: Tue, 15 Oct 2024 15:39:42 -0700 Subject: [PATCH] Fix flaky Http2WebSocket Kestrel test --- .../Http2/Http2WebSocketTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2WebSocketTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2WebSocketTests.cs index acd7c805e2f8..0774fec645b1 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2WebSocketTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2WebSocketTests.cs @@ -516,11 +516,18 @@ await InitializeConnectionAsync(async context => [Fact] public async Task ExtendedCONNECTMethod_CanHaveNon200ResponseWithBody() { + var finishedSendingTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + await InitializeConnectionAsync(async context => { var connectFeature = context.Features.Get(); Assert.True(connectFeature.IsExtendedConnect); + // The EndStreamReceived flag might not have been sent let alone received by the server by the time application code completes + // which would result in a RST_STREAM being sent to the client. We wait for the data frame to finish sending + // before allowing application code to complete (relies on inline Pipe completions which we use for tests) + await finishedSendingTcs.Task; + context.Response.StatusCode = Http.StatusCodes.Status418ImATeapot; context.Response.ContentLength = 2; await context.Response.Body.WriteAsync(new byte[1] { 0x01 }); @@ -543,6 +550,8 @@ await InitializeConnectionAsync(async context => await SendDataAsync(1, new byte[10241], endStream: true); + finishedSendingTcs.SetResult(); + var headersFrame = await ExpectAsync(Http2FrameType.HEADERS, withLength: 40, withFlags: (byte)Http2HeadersFrameFlags.END_HEADERS,