Skip to content

Commit 80f2abd

Browse files
authored
Merge pull request #57 from ladeak/flaky_tests
Flaky MultipleWrites_HeaderFrame* tests
2 parents 302a3ba + 88f9987 commit 80f2abd

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

tests/CHttpServer.Tests/Http3/Http3StreamTests.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Quic;
1+
using System.Diagnostics;
2+
using System.Net.Quic;
23
using CHttpServer.Http3;
34
using Microsoft.AspNetCore.Http.Features;
45

@@ -55,22 +56,24 @@ public async Task MultipleWrites_HeaderFrame()
5556

5657
TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
5758
sut.Initialize(null, await serverStreamTask);
58-
var testApp = new TestBase.TestApplication(ctx =>
59+
var testApp = new TestBase.TestApplication(async ctx =>
5960
{
6061
Assert.Equal("/", ctx.Request.Path);
6162
Assert.Equal("https", ctx.Request.Scheme);
6263
Assert.Equal("localhost", ctx.Request.Host.ToString());
6364
Assert.Equal(HttpMethod.Get.ToString(), ctx.Request.Method);
64-
tcs.SetResult();
65-
return Task.CompletedTask;
65+
66+
// This callback shall not complete before the 2nd flush, otherwise the
67+
// server might close the stream before the flush succeeds.
68+
await tcs.Task;
6669
});
6770
var processing = sut.ProcessStreamAsync(testApp, TestContext.Current.CancellationToken);
6871

6972
// Second Write
7073
await clientStream.WriteAsync(headersFrame[i..], TestContext.Current.CancellationToken);
7174
await clientStream.FlushAsync(TestContext.Current.CancellationToken);
75+
tcs.SetResult();
7276

73-
await tcs.Task.WaitAsync(DefaultTimeout, TestContext.Current.CancellationToken);
7477
clientStream.Close();
7578
await processing;
7679
await quicConnection.DisposeAsync();
@@ -112,37 +115,38 @@ public async Task SingleWrite_HeaderFrame_ReservedFrame()
112115
public async Task MultipleWrites_HeaderFrame_ReservedFrame()
113116
{
114117
byte[] data = [.. Http3FrameFixture.GetHeadersFrame(), .. Http3FrameFixture.GetReservedFrame(10)];
115-
var quicConnection = await QuicConnectionFixture.SetupConnectionAsync(Port, TestContext.Current.CancellationToken);
118+
await using var quicConnection = await QuicConnectionFixture.SetupConnectionAsync(Port, TestContext.Current.CancellationToken);
116119
for (int i = 1; i < data.Length; i++)
117120
{
118121
var serverStreamTask = Task.Run(async () => await quicConnection.ServerConnection.AcceptInboundStreamAsync());
119122
var sut = new Http3Stream([]);
120123

121-
var clientStream = await quicConnection.ClientConnection.OpenOutboundStreamAsync(QuicStreamType.Bidirectional, TestContext.Current.CancellationToken);
124+
await using var clientStream = await quicConnection.ClientConnection.OpenOutboundStreamAsync(QuicStreamType.Bidirectional, TestContext.Current.CancellationToken);
122125
await clientStream.WriteAsync(data.AsMemory(0, i), TestContext.Current.CancellationToken);
123126
await clientStream.FlushAsync(TestContext.Current.CancellationToken);
124127

125-
TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
128+
TaskCompletionSource secondFlushCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously);
126129
sut.Initialize(null, await serverStreamTask);
127-
var testApp = new TestBase.TestApplication(ctx =>
130+
var testApp = new TestBase.TestApplication(async ctx =>
128131
{
129132
Assert.Equal("/", ctx.Request.Path);
130133
Assert.Equal("https", ctx.Request.Scheme);
131134
Assert.Equal("localhost", ctx.Request.Host.ToString());
132135
Assert.Equal(HttpMethod.Get.ToString(), ctx.Request.Method);
133-
tcs.SetResult();
134-
return Task.CompletedTask;
136+
137+
// This callback shall not complete before the 2nd flush, otherwise the
138+
// server might close the stream before the flush succeeds.
139+
await secondFlushCompleted.Task;
135140
});
136141
var processing = sut.ProcessStreamAsync(testApp, TestContext.Current.CancellationToken);
137142

138143
await clientStream.WriteAsync(data.AsMemory(i), TestContext.Current.CancellationToken);
139144
await clientStream.FlushAsync(TestContext.Current.CancellationToken);
145+
secondFlushCompleted.SetResult();
140146

141-
await tcs.Task.WaitAsync(DefaultTimeout, TestContext.Current.CancellationToken);
142-
clientStream.Close();
143147
await processing;
148+
clientStream.Close();
144149
}
145-
await quicConnection.DisposeAsync();
146150
}
147151

148152
[Fact]

0 commit comments

Comments
 (0)