@@ -366,7 +366,7 @@ public async Task WebSocketTinyReceiveGeneratesEndOfMessage()
366
366
public async Task ClientDisposalAbortsRequest ( )
367
367
{
368
368
// Arrange
369
- TaskCompletionSource < object > tcs = new TaskCompletionSource < object > ( ) ;
369
+ var tcs = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
370
370
RequestDelegate appDelegate = async ctx =>
371
371
{
372
372
// Write Headers
@@ -399,30 +399,26 @@ public async Task ClientDisposalAbortsRequest()
399
399
[ Fact ]
400
400
public async Task ClientCancellationAbortsRequest ( )
401
401
{
402
- // Arrange
403
- TaskCompletionSource < object > tcs = new TaskCompletionSource < object > ( ) ;
404
- RequestDelegate appDelegate = async ctx =>
402
+ var tcs = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
403
+ var builder = new WebHostBuilder ( ) . Configure ( app => app . Run ( async ctx =>
405
404
{
406
- var sem = new SemaphoreSlim ( 0 ) ;
407
405
try
408
406
{
409
- await sem . WaitAsync ( ctx . RequestAborted ) ;
407
+ await Task . Delay ( TimeSpan . FromSeconds ( 30 ) , ctx . RequestAborted ) ;
408
+ tcs . SetResult ( 0 ) ;
410
409
}
411
410
catch ( Exception e )
412
411
{
413
412
tcs . SetException ( e ) ;
413
+ return ;
414
414
}
415
- } ;
416
-
417
- // Act
418
- var builder = new WebHostBuilder ( ) . Configure ( app => app . Run ( appDelegate ) ) ;
419
- var server = new TestServer ( builder ) ;
420
- var client = server . CreateClient ( ) ;
421
- var cts = new CancellationTokenSource ( ) ;
422
- cts . CancelAfter ( 500 ) ;
415
+ throw new InvalidOperationException ( "The request was not aborted" ) ;
416
+ } ) ) ;
417
+ using var server = new TestServer ( builder ) ;
418
+ using var client = server . CreateClient ( ) ;
419
+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) ;
423
420
var response = await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => client . GetAsync ( "http://localhost:12345" , cts . Token ) ) ;
424
421
425
- // Assert
426
422
var exception = await Assert . ThrowsAnyAsync < OperationCanceledException > ( async ( ) => await tcs . Task ) ;
427
423
}
428
424
0 commit comments