@@ -314,20 +314,15 @@ public async Task SendMailAsync_CanBeCanceled_CancellationToken()
314
314
server . ReceiveMultipleConnections = true ;
315
315
316
316
// 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
-
320
317
CancellationTokenSource cts = new CancellationTokenSource ( ) ;
318
+
319
+ server . OnConnected += _ => cts . Cancel ( ) ;
321
320
322
321
var message = new MailMessage ( "[email protected] " , "[email protected] " , "Foo" , "Bar" ) ;
323
322
324
323
Task sendTask = Task . Run ( ( ) => client . SendMailAsync ( message , cts . Token ) ) ;
325
324
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 ) ;
331
326
332
327
// We should still be able to send mail on the SmtpClient instance
333
328
await Task . Run ( ( ) => client . SendMailAsync ( message ) ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
@@ -369,8 +364,7 @@ public async Task SendAsync_CanBeCanceled_SendAsyncCancel()
369
364
client . SendAsync ( message , null ) ;
370
365
AsyncCompletedEventArgs e = await tcs . Task . WaitAsync ( TestHelper . PassingTestTimeout ) ;
371
366
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 ) ;
374
368
375
369
// We should still be able to send mail on the SmtpClient instance
376
370
await client . SendMailAsync ( message ) . WaitAsync ( TestHelper . PassingTestTimeout ) ;
0 commit comments