@@ -102,18 +102,17 @@ public async Task BlockingMiddlewareShouldNotBlockClient()
102
102
[ Fact ]
103
103
public async Task HeadersAvailableBeforeSyncBodyFinished ( )
104
104
{
105
- var block = new ManualResetEvent ( false ) ;
105
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
106
106
var builder = new WebHostBuilder ( ) . Configure ( app =>
107
107
{
108
- app . Run ( c =>
108
+ app . Run ( async c =>
109
109
{
110
110
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
111
111
var bytes = Encoding . UTF8 . GetBytes ( "BodyStarted" + Environment . NewLine ) ;
112
112
c . Response . Body . Write ( bytes , 0 , bytes . Length ) ;
113
- Assert . True ( block . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) ) ;
113
+ await block . Task ;
114
114
bytes = Encoding . UTF8 . GetBytes ( "BodyFinished" ) ;
115
115
c . Response . Body . Write ( bytes , 0 , bytes . Length ) ;
116
- return Task . CompletedTask ;
117
116
} ) ;
118
117
} ) ;
119
118
var server = new TestServer ( builder ) ;
@@ -122,21 +121,21 @@ public async Task HeadersAvailableBeforeSyncBodyFinished()
122
121
Assert . Equal ( "TestValue" , context . Response . Headers [ "TestHeader" ] ) ;
123
122
var reader = new StreamReader ( context . Response . Body ) ;
124
123
Assert . Equal ( "BodyStarted" , reader . ReadLine ( ) ) ;
125
- block . Set ( ) ;
124
+ block . SetResult ( 0 ) ;
126
125
Assert . Equal ( "BodyFinished" , reader . ReadToEnd ( ) ) ;
127
126
}
128
127
129
128
[ Fact ]
130
129
public async Task HeadersAvailableBeforeAsyncBodyFinished ( )
131
130
{
132
- var block = new ManualResetEvent ( false ) ;
131
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
133
132
var builder = new WebHostBuilder ( ) . Configure ( app =>
134
133
{
135
134
app . Run ( async c =>
136
135
{
137
136
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
138
137
await c . Response . WriteAsync ( "BodyStarted" + Environment . NewLine ) ;
139
- Assert . True ( block . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) ) ;
138
+ await block . Task ;
140
139
await c . Response . WriteAsync ( "BodyFinished" ) ;
141
140
} ) ;
142
141
} ) ;
@@ -146,43 +145,43 @@ public async Task HeadersAvailableBeforeAsyncBodyFinished()
146
145
Assert . Equal ( "TestValue" , context . Response . Headers [ "TestHeader" ] ) ;
147
146
var reader = new StreamReader ( context . Response . Body ) ;
148
147
Assert . Equal ( "BodyStarted" , await reader . ReadLineAsync ( ) ) ;
149
- block . Set ( ) ;
148
+ block . SetResult ( 0 ) ;
150
149
Assert . Equal ( "BodyFinished" , await reader . ReadToEndAsync ( ) ) ;
151
150
}
152
151
153
152
[ Fact ]
154
153
public async Task FlushSendsHeaders ( )
155
154
{
156
- var block = new ManualResetEvent ( false ) ;
155
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
157
156
var builder = new WebHostBuilder ( ) . Configure ( app =>
158
157
{
159
158
app . Run ( async c =>
160
159
{
161
160
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
162
161
c . Response . Body . Flush ( ) ;
163
- block . WaitOne ( ) ;
162
+ await block . Task ;
164
163
await c . Response . WriteAsync ( "BodyFinished" ) ;
165
164
} ) ;
166
165
} ) ;
167
166
var server = new TestServer ( builder ) ;
168
167
var context = await server . SendAsync ( c => { } ) ;
169
168
170
169
Assert . Equal ( "TestValue" , context . Response . Headers [ "TestHeader" ] ) ;
171
- block . Set ( ) ;
170
+ block . SetResult ( 0 ) ;
172
171
Assert . Equal ( "BodyFinished" , new StreamReader ( context . Response . Body ) . ReadToEnd ( ) ) ;
173
172
}
174
173
175
174
[ Fact ]
176
175
public async Task ClientDisposalCloses ( )
177
176
{
178
- var block = new ManualResetEvent ( false ) ;
177
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
179
178
var builder = new WebHostBuilder ( ) . Configure ( app =>
180
179
{
181
180
app . Run ( async c =>
182
181
{
183
182
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
184
183
c . Response . Body . Flush ( ) ;
185
- block . WaitOne ( ) ;
184
+ await block . Task ;
186
185
await c . Response . WriteAsync ( "BodyFinished" ) ;
187
186
} ) ;
188
187
} ) ;
@@ -194,20 +193,20 @@ public async Task ClientDisposalCloses()
194
193
Task < int > readTask = responseStream . ReadAsync ( new byte [ 100 ] , 0 , 100 ) ;
195
194
Assert . False ( readTask . IsCompleted ) ;
196
195
responseStream . Dispose ( ) ;
197
- Assert . True ( readTask . Wait ( TimeSpan . FromSeconds ( 10 ) ) ) ;
198
- Assert . Equal ( 0 , readTask . Result ) ;
199
- block . Set ( ) ;
196
+ var read = await readTask . WithTimeout ( ) ;
197
+ Assert . Equal ( 0 , read ) ;
198
+ block . SetResult ( 0 ) ;
200
199
}
201
200
202
201
[ Fact ]
203
- public void ClientCancellationAborts ( )
202
+ public async Task ClientCancellationAborts ( )
204
203
{
205
- var block = new ManualResetEvent ( false ) ;
204
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
206
205
var builder = new WebHostBuilder ( ) . Configure ( app =>
207
206
{
208
207
app . Run ( c =>
209
208
{
210
- block . Set ( ) ;
209
+ block . SetResult ( 0 ) ;
211
210
Assert . True ( c . RequestAborted . WaitHandle . WaitOne ( TimeSpan . FromSeconds ( 10 ) ) ) ;
212
211
c . RequestAborted . ThrowIfCancellationRequested ( ) ;
213
212
return Task . CompletedTask ;
@@ -216,24 +215,23 @@ public void ClientCancellationAborts()
216
215
var server = new TestServer ( builder ) ;
217
216
var cts = new CancellationTokenSource ( ) ;
218
217
var contextTask = server . SendAsync ( c => { } , cts . Token ) ;
219
- block . WaitOne ( ) ;
218
+ await block . Task ;
220
219
cts . Cancel ( ) ;
221
220
222
- var ex = Assert . Throws < AggregateException > ( ( ) => contextTask . Wait ( TimeSpan . FromSeconds ( 10 ) ) ) ;
223
- Assert . IsAssignableFrom < OperationCanceledException > ( ex . GetBaseException ( ) ) ;
221
+ await Assert . ThrowsAsync < OperationCanceledException > ( ( ) => contextTask . WithTimeout ( ) ) ;
224
222
}
225
223
226
224
[ Fact ]
227
225
public async Task ClientCancellationAbortsReadAsync ( )
228
226
{
229
- var block = new ManualResetEvent ( false ) ;
227
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
230
228
var builder = new WebHostBuilder ( ) . Configure ( app =>
231
229
{
232
230
app . Run ( async c =>
233
231
{
234
232
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
235
233
c . Response . Body . Flush ( ) ;
236
- block . WaitOne ( ) ;
234
+ await block . Task ;
237
235
await c . Response . WriteAsync ( "BodyFinished" ) ;
238
236
} ) ;
239
237
} ) ;
@@ -246,9 +244,8 @@ public async Task ClientCancellationAbortsReadAsync()
246
244
var readTask = responseStream . ReadAsync ( new byte [ 100 ] , 0 , 100 , cts . Token ) ;
247
245
Assert . False ( readTask . IsCompleted ) ;
248
246
cts . Cancel ( ) ;
249
- var ex = Assert . Throws < AggregateException > ( ( ) => readTask . Wait ( TimeSpan . FromSeconds ( 10 ) ) ) ;
250
- Assert . IsAssignableFrom < OperationCanceledException > ( ex . GetBaseException ( ) ) ;
251
- block . Set ( ) ;
247
+ await Assert . ThrowsAsync < OperationCanceledException > ( ( ) => readTask . WithTimeout ( ) ) ;
248
+ block . SetResult ( 0 ) ;
252
249
}
253
250
254
251
[ Fact ]
@@ -268,14 +265,14 @@ public Task ExceptionBeforeFirstWriteIsReported()
268
265
[ Fact ]
269
266
public async Task ExceptionAfterFirstWriteIsReported ( )
270
267
{
271
- var block = new ManualResetEvent ( false ) ;
268
+ var block = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
272
269
var builder = new WebHostBuilder ( ) . Configure ( app =>
273
270
{
274
271
app . Run ( async c =>
275
272
{
276
273
c . Response . Headers [ "TestHeader" ] = "TestValue" ;
277
274
await c . Response . WriteAsync ( "BodyStarted" ) ;
278
- block . WaitOne ( ) ;
275
+ await block . Task ;
279
276
throw new InvalidOperationException ( "Test Exception" ) ;
280
277
} ) ;
281
278
} ) ;
@@ -284,7 +281,7 @@ public async Task ExceptionAfterFirstWriteIsReported()
284
281
285
282
Assert . Equal ( "TestValue" , context . Response . Headers [ "TestHeader" ] ) ;
286
283
Assert . Equal ( 11 , context . Response . Body . Read ( new byte [ 100 ] , 0 , 100 ) ) ;
287
- block . Set ( ) ;
284
+ block . SetResult ( 0 ) ;
288
285
var ex = Assert . Throws < IOException > ( ( ) => context . Response . Body . Read ( new byte [ 100 ] , 0 , 100 ) ) ;
289
286
Assert . IsAssignableFrom < InvalidOperationException > ( ex . InnerException ) ;
290
287
}
0 commit comments