Skip to content

Commit a61229a

Browse files
Deflake Http2WebSocket tests (#48742)
1 parent 239b00d commit a61229a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2WebSocketTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ await InitializeConnectionAsync(async context =>
155155
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/42133")]
156156
public async Task HEADERS_Received_SecondRequest_Accepted()
157157
{
158-
// Add stream to Http2Connection._completedStreams inline with SetResult().
159158
var appDelegateTcs = new TaskCompletionSource();
160159
await InitializeConnectionAsync(async context =>
161160
{
@@ -181,6 +180,19 @@ await InitializeConnectionAsync(async context =>
181180
await appDelegateTcs.Task;
182181
});
183182

183+
var originalHandler = _connection._streamLifetimeHandler;
184+
var tcs = new TaskCompletionSource();
185+
var streamLifetimeHandler = new Mock<IHttp2StreamLifetimeHandler>();
186+
streamLifetimeHandler.Setup(o => o.OnStreamCompleted(It.IsAny<Http2Stream>())).Callback((Http2Stream stream) =>
187+
{
188+
// Add stream to Http2Connection._completedStreams.
189+
originalHandler.OnStreamCompleted(stream);
190+
191+
// Unblock test code that will call TriggerTick and return the stream to the pool
192+
tcs.TrySetResult();
193+
});
194+
_connection._streamLifetimeHandler = streamLifetimeHandler.Object;
195+
184196
// HEADERS + END_HEADERS
185197
// :method = CONNECT
186198
// :protocol = websocket
@@ -224,6 +236,7 @@ await InitializeConnectionAsync(async context =>
224236
Assert.Equal(0x01, dataFrame.Payload.Span[0]);
225237

226238
appDelegateTcs.TrySetResult();
239+
await tcs.Task.DefaultTimeout();
227240

228241
dataFrame = await ExpectAsync(Http2FrameType.DATA,
229242
withLength: 0,
@@ -595,8 +608,23 @@ await InitializeConnectionAsync(async context =>
595608
// We've done the test. Now just return the normal echo server behavior.
596609
await _echoApplication(context);
597610
}
611+
});
598612

613+
var originalHandler = _connection._streamLifetimeHandler;
614+
var tcs = new TaskCompletionSource();
615+
var streamLifetimeHandler = new Mock<IHttp2StreamLifetimeHandler>();
616+
streamLifetimeHandler.Setup(o => o.OnStreamCompleted(It.IsAny<Http2Stream>())).Callback((Http2Stream stream) =>
617+
{
618+
// Add stream to Http2Connection._completedStreams.
619+
originalHandler.OnStreamCompleted(stream);
620+
621+
if (requestCount == 1)
622+
{
623+
// Unblock test code that will call TriggerTick and return the stream to the pool
624+
tcs.TrySetResult();
625+
}
599626
});
627+
_connection._streamLifetimeHandler = streamLifetimeHandler.Object;
600628

601629
// HEADERS + END_HEADERS
602630
// :method = CONNECT
@@ -636,6 +664,8 @@ await InitializeConnectionAsync(async context =>
636664
Assert.Equal(0x01, dataFrame.Payload.Span[0]);
637665

638666
appDelegateTcs.TrySetResult();
667+
await tcs.Task.DefaultTimeout();
668+
639669
dataFrame = await ExpectAsync(Http2FrameType.DATA,
640670
withLength: 0,
641671
withFlags: (byte)Http2DataFrameFlags.END_STREAM,

0 commit comments

Comments
 (0)