@@ -314,20 +314,15 @@ public async Task SendMailAsync_CanBeCanceled_CancellationToken()
314314 server . ReceiveMultipleConnections = true ;
315315
316316 // The server will introduce some fake latency so that the operation can be canceled before the request completes
317- ManualResetEvent serverMre = new ManualResetEvent ( false ) ;
318- server . OnConnected += _ => serverMre . WaitOne ( ) ;
319-
320317 CancellationTokenSource cts = new CancellationTokenSource ( ) ;
318+
319+ server . OnConnected += _ => cts . Cancel ( ) ;
321320
322321 var message = new MailMessage ( "[email protected] " , "[email protected] " , "Foo" , "Bar" ) ; 323322
324323 Task sendTask = Task . Run ( ( ) => client . SendMailAsync ( message , cts . Token ) ) ;
325324
326- cts . Cancel ( ) ;
327- await Task . Delay ( 500 ) ;
328- serverMre . Set ( ) ;
329-
330- await Assert . ThrowsAsync < TaskCanceledException > ( async ( ) => await sendTask ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
325+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( async ( ) => await sendTask ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
331326
332327 // We should still be able to send mail on the SmtpClient instance
333328 await Task . Run ( ( ) => client . SendMailAsync ( message ) ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
@@ -369,8 +364,7 @@ public async Task SendAsync_CanBeCanceled_SendAsyncCancel()
369364 client . SendAsync ( message , null ) ;
370365 AsyncCompletedEventArgs e = await tcs . Task . WaitAsync ( TestHelper . PassingTestTimeout ) ;
371366 Assert . True ( e . Cancelled , "SendAsync should have been canceled" ) ;
372- _output . WriteLine ( e . Error ? . ToString ( ) ?? "No error" ) ;
373- Assert . IsType < OperationCanceledException > ( e . Error . InnerException ) ;
367+ Assert . Null ( e . Error ) ;
374368
375369 // We should still be able to send mail on the SmtpClient instance
376370 await client . SendMailAsync ( message ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
0 commit comments