Skip to content

Commit 010c1f0

Browse files
authored
Call StopAsync before disposing (#6189)
1 parent b12c33d commit 010c1f0

25 files changed

+471
-236
lines changed

src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ await connection.SendAll(
166166
"");
167167
await ReceiveBadRequestResponse(connection, "400 Bad Request", server.Context.DateHeaderValue);
168168
}
169+
170+
await server.StopAsync();
169171
}
170172

171173
Assert.All(TestSink.Writes, w => Assert.InRange(w.LogLevel, LogLevel.Trace, LogLevel.Information));
@@ -185,6 +187,8 @@ await client.SendAll(
185187

186188
await client.Receive("HTTP/1.1 400");
187189
}
190+
191+
await server.StopAsync();
188192
}
189193
}
190194

@@ -206,6 +210,8 @@ private async Task TestBadRequest(string request, string expectedResponseStatusC
206210
await connection.SendAll(request);
207211
await ReceiveBadRequestResponse(connection, expectedResponseStatusCode, server.Context.DateHeaderValue, expectedAllowHeader);
208212
}
213+
214+
await server.StopAsync();
209215
}
210216

211217
mockKestrelTrace.Verify(trace => trace.ConnectionBadRequest(It.IsAny<string>(), It.IsAny<BadHttpRequestException>()));

src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ await connection.ReceiveEnd(
114114
"",
115115
"Goodbye");
116116
}
117+
118+
await server.StopAsync();
117119
}
118120
}
119121

@@ -168,6 +170,7 @@ await connection.Receive(
168170
"",
169171
"Hello World");
170172
}
173+
await server.StopAsync();
171174
}
172175
}
173176

@@ -253,6 +256,8 @@ public async Task TrailingHeadersAreParsed()
253256
await connection.Send(fullRequest);
254257
await connection.Receive(expectedFullResponse);
255258
}
259+
260+
await server.StopAsync();
256261
}
257262
}
258263

@@ -297,6 +302,7 @@ await connection.ReceiveEnd(
297302
"",
298303
"");
299304
}
305+
await server.StopAsync();
300306
}
301307
}
302308

@@ -338,6 +344,7 @@ await connection.ReceiveEnd(
338344
"",
339345
"");
340346
}
347+
await server.StopAsync();
341348
}
342349
}
343350

@@ -424,6 +431,7 @@ public async Task ExtensionsAreIgnored()
424431
await connection.Send(fullRequest);
425432
await connection.Receive(expectedFullResponse);
426433
}
434+
await server.StopAsync();
427435
}
428436
}
429437

@@ -467,6 +475,7 @@ await connection.ReceiveEnd(
467475
"",
468476
"");
469477
}
478+
await server.StopAsync();
470479
}
471480
}
472481

@@ -511,6 +520,7 @@ await connection.ReceiveEnd(
511520
"",
512521
"");
513522
}
523+
await server.StopAsync();
514524
}
515525
}
516526

@@ -615,6 +625,7 @@ await connection.ReceiveEnd(
615625
"",
616626
"");
617627
}
628+
await server.StopAsync();
618629
}
619630
}
620631

@@ -662,6 +673,8 @@ await connection.SendAll(
662673
var badReqEx = await exTcs.Task.TimeoutAfter(TestConstants.DefaultTimeout);
663674
Assert.Equal(RequestRejectionReason.UnexpectedEndOfRequestContent, badReqEx.Reason);
664675
}
676+
677+
await server.StopAsync();
665678
}
666679
}
667680
}

src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedResponseTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ await connection.Receive(
4747
"",
4848
"");
4949
}
50+
await server.StopAsync();
5051
}
5152
}
5253

@@ -75,6 +76,7 @@ await connection.ReceiveEnd(
7576
"",
7677
"Hello World!");
7778
}
79+
await server.StopAsync();
7880
}
7981
}
8082

@@ -111,6 +113,7 @@ await connection.ReceiveEnd(
111113
"",
112114
"");
113115
}
116+
await server.StopAsync();
114117
}
115118
}
116119

@@ -147,6 +150,7 @@ await connection.ReceiveEnd(
147150
"",
148151
"");
149152
}
153+
await server.StopAsync();
150154
}
151155
}
152156

@@ -183,6 +187,7 @@ await connection.Receive(
183187
"",
184188
"");
185189
}
190+
await server.StopAsync();
186191
}
187192
}
188193

@@ -227,6 +232,7 @@ await connection.Receive(
227232
"",
228233
"");
229234
}
235+
await server.StopAsync();
230236
}
231237
}
232238

@@ -257,6 +263,7 @@ await connection.Receive(
257263
"",
258264
"");
259265
}
266+
await server.StopAsync();
260267
}
261268
}
262269

@@ -290,6 +297,7 @@ await connection.ReceiveEnd(
290297
"Hello World!",
291298
"");
292299
}
300+
await server.StopAsync();
293301
}
294302
}
295303

@@ -322,6 +330,7 @@ await connection.ReceiveEnd(
322330
"",
323331
"");
324332
}
333+
await server.StopAsync();
325334
}
326335
}
327336

@@ -368,6 +377,7 @@ await connection.Receive(
368377
"",
369378
"");
370379
}
380+
await server.StopAsync();
371381
}
372382
}
373383

@@ -406,6 +416,8 @@ await connection.Receive(
406416
"",
407417
"");
408418
}
419+
420+
await server.StopAsync();
409421
}
410422
}
411423
}

src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionAdapterTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ await connection.ReceiveEnd(
4343
"",
4444
"Hello World!");
4545
}
46+
await server.StopAsync();
4647
}
4748

4849
Assert.Equal(sendString.Length, adapter.BytesRead);
@@ -74,6 +75,7 @@ await connection.ReceiveEnd(
7475
"",
7576
"Hello World!");
7677
}
78+
await server.StopAsync();
7779
}
7880
}
7981

@@ -95,6 +97,7 @@ public async Task ImmediateFinAfterOnConnectionAsyncClosesGracefully()
9597
connection.ShutdownSend();
9698
await connection.WaitForConnectionClose();
9799
}
100+
await server.StopAsync();
98101
}
99102
}
100103

@@ -116,6 +119,7 @@ public async Task ImmediateFinAfterThrowingClosesGracefully()
116119
connection.ShutdownSend();
117120
await connection.WaitForConnectionClose();
118121
}
122+
await server.StopAsync();
119123
}
120124
}
121125

@@ -162,6 +166,7 @@ await connection.Send(
162166

163167
await connection.WaitForConnectionClose();
164168
}
169+
await server.StopAsync();
165170
}
166171

167172
Assert.Contains(TestApplicationErrorLogger.Messages, m => m.Message.Contains($"Uncaught exception from the {nameof(IConnectionAdapter.OnConnectionAsync)} method of an {nameof(IConnectionAdapter)}."));
@@ -197,6 +202,7 @@ await connection.ReceiveEnd(
197202
"",
198203
"Hello World!");
199204
}
205+
await server.StopAsync();
200206
}
201207
}
202208

src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionLimitTests.cs

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -36,12 +36,16 @@ public async Task ResetsCountWhenConnectionClosed()
3636
await context.Response.WriteAsync("Hello");
3737
await requestTcs.Task;
3838
}, counter))
39-
using (var connection = server.CreateConnection())
4039
{
41-
await connection.SendEmptyGetAsKeepAlive(); ;
42-
await connection.Receive("HTTP/1.1 200 OK");
43-
Assert.True(await lockedTcs.Task.DefaultTimeout());
44-
requestTcs.TrySetResult(null);
40+
using (var connection = server.CreateConnection())
41+
{
42+
await connection.SendEmptyGetAsKeepAlive(); ;
43+
await connection.Receive("HTTP/1.1 200 OK");
44+
Assert.True(await lockedTcs.Task.DefaultTimeout());
45+
requestTcs.TrySetResult(null);
46+
}
47+
48+
await server.StopAsync();
4549
}
4650

4751
await releasedTcs.Task.DefaultTimeout();
@@ -63,33 +67,36 @@ public async Task UpgradedConnectionsCountsAgainstDifferentLimit()
6367
}
6468
}
6569
}, max: 1))
66-
using (var disposables = new DisposableStack<InMemoryConnection>())
6770
{
68-
var upgraded = server.CreateConnection();
69-
disposables.Push(upgraded);
71+
using (var disposables = new DisposableStack<InMemoryConnection>())
72+
{
73+
var upgraded = server.CreateConnection();
74+
disposables.Push(upgraded);
7075

71-
await upgraded.SendEmptyGetWithUpgrade();
72-
await upgraded.Receive("HTTP/1.1 101");
73-
// once upgraded, normal connection limit is decreased to allow room for more "normal" connections
76+
await upgraded.SendEmptyGetWithUpgrade();
77+
await upgraded.Receive("HTTP/1.1 101");
78+
// once upgraded, normal connection limit is decreased to allow room for more "normal" connections
7479

75-
var connection = server.CreateConnection();
76-
disposables.Push(connection);
80+
var connection = server.CreateConnection();
81+
disposables.Push(connection);
7782

78-
await connection.SendEmptyGetAsKeepAlive();
79-
await connection.Receive("HTTP/1.1 200 OK");
83+
await connection.SendEmptyGetAsKeepAlive();
84+
await connection.Receive("HTTP/1.1 200 OK");
8085

81-
using (var rejected = server.CreateConnection())
82-
{
83-
try
86+
using (var rejected = server.CreateConnection())
8487
{
85-
// this may throw IOException, depending on how fast Kestrel closes the socket
86-
await rejected.SendEmptyGetAsKeepAlive();
87-
}
88-
catch { }
88+
try
89+
{
90+
// this may throw IOException, depending on how fast Kestrel closes the socket
91+
await rejected.SendEmptyGetAsKeepAlive();
92+
}
93+
catch { }
8994

90-
// connection should close without sending any data
91-
await rejected.WaitForConnectionClose();
95+
// connection should close without sending any data
96+
await rejected.WaitForConnectionClose();
97+
}
9298
}
99+
await server.StopAsync();
93100
}
94101
}
95102

@@ -104,35 +111,38 @@ public async Task RejectsConnectionsWhenLimitReached()
104111
await context.Response.WriteAsync("Hello");
105112
await requestTcs.Task;
106113
}, max))
107-
using (var disposables = new DisposableStack<InMemoryConnection>())
108114
{
109-
for (var i = 0; i < max; i++)
115+
using (var disposables = new DisposableStack<InMemoryConnection>())
110116
{
111-
var connection = server.CreateConnection();
112-
disposables.Push(connection);
117+
for (var i = 0; i < max; i++)
118+
{
119+
var connection = server.CreateConnection();
120+
disposables.Push(connection);
113121

114-
await connection.SendEmptyGetAsKeepAlive();
115-
await connection.Receive("HTTP/1.1 200 OK");
116-
}
122+
await connection.SendEmptyGetAsKeepAlive();
123+
await connection.Receive("HTTP/1.1 200 OK");
124+
}
117125

118-
// limit has been reached
119-
for (var i = 0; i < 10; i++)
120-
{
121-
using (var connection = server.CreateConnection())
126+
// limit has been reached
127+
for (var i = 0; i < 10; i++)
122128
{
123-
try
129+
using (var connection = server.CreateConnection())
124130
{
125-
// this may throw IOException, depending on how fast Kestrel closes the socket
126-
await connection.SendEmptyGetAsKeepAlive();
131+
try
132+
{
133+
// this may throw IOException, depending on how fast Kestrel closes the socket
134+
await connection.SendEmptyGetAsKeepAlive();
135+
}
136+
catch { }
137+
138+
// connection should close without sending any data
139+
await connection.WaitForConnectionClose();
127140
}
128-
catch { }
129-
130-
// connection should close without sending any data
131-
await connection.WaitForConnectionClose();
132141
}
133-
}
134142

135-
requestTcs.TrySetResult(null);
143+
requestTcs.TrySetResult(null);
144+
}
145+
await server.StopAsync();
136146
}
137147
}
138148

@@ -189,6 +199,7 @@ public async Task ConnectionCountingReturnsToZero()
189199
await closedTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(120));
190200
Assert.Equal(count, opened);
191201
Assert.Equal(count, closed);
202+
await server.StopAsync();
192203
}
193204
}
194205

0 commit comments

Comments
 (0)