@@ -72,49 +72,47 @@ public async Task CancellationAsync_NonCanceledWithArgument_Succeeds()
7272
7373 [ TestMethod ]
7474 public async Task CancellationAsync_ObserveException_Succeeds ( )
75- => await RetryHelper . RetryAsync (
76- async ( ) =>
77- {
78- ManualResetEvent waitException = new ( false ) ;
79- await Assert . ThrowsAsync < OperationCanceledException > ( async ( )
80- => await Task . Run (
81- async ( ) =>
82- {
83- await Task . Delay ( TimeSpan . FromSeconds ( 10 ) , TestContext . CancellationToken ) ;
84- waitException . Set ( ) ;
85- throw new InvalidOperationException ( ) ;
86- } , TestContext . CancellationToken ) . WithCancellationAsync ( new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) . Token ) ) ;
87-
88- waitException . WaitOne ( ) ;
89- await Task . Delay ( TimeSpan . FromSeconds ( 4 ) , TestContext . CancellationToken ) ;
90- } , 3 , TimeSpan . FromSeconds ( 3 ) ) ;
75+ {
76+ ManualResetEvent waitException = new ( false ) ;
77+ CancellationToken token = new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) . Token ;
78+ OperationCanceledException ex = await Assert . ThrowsAsync < OperationCanceledException > ( async ( )
79+ => await Task . Run (
80+ async ( ) =>
81+ {
82+ await Task . Delay ( TimeSpan . FromSeconds ( 5 ) , TestContext . CancellationToken ) ;
83+ waitException . Set ( ) ;
84+ throw new InvalidOperationException ( ) ;
85+ } , TestContext . CancellationToken ) . WithCancellationAsync ( token ) ) ;
86+ #if ! NETFRAMEWORK // Polyfill bug in Task.WaitAsync implementation :/
87+ Assert . AreEqual ( token , ex . CancellationToken ) ;
88+ #endif
89+ waitException . WaitOne ( ) ;
90+ }
9191
9292 [ TestMethod ]
9393 public async Task CancellationAsyncWithReturnValue_ObserveException_Succeeds ( )
94- => await RetryHelper . RetryAsync (
95- async ( ) =>
94+ {
95+ ManualResetEvent waitException = new ( false ) ;
96+ CancellationToken token = new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) . Token ;
97+ OperationCanceledException ex = await Assert . ThrowsAsync < OperationCanceledException > ( async ( )
98+ => await Task . Run ( async ( ) =>
9699 {
97- ManualResetEvent waitException = new ( false ) ;
98- await Assert . ThrowsAsync < OperationCanceledException > ( async ( )
99- => await Task . Run ( async ( ) =>
100- {
101- await Task . Delay ( TimeSpan . FromSeconds ( 10 ) , TestContext . CancellationToken ) ;
102- try
103- {
104- return 2 ;
105- }
106- finally
107- {
108- waitException . Set ( ) ;
100+ await Task . Delay ( TimeSpan . FromSeconds ( 5 ) , TestContext . CancellationToken ) ;
101+ try
102+ {
103+ return 2 ;
104+ }
105+ finally
106+ {
107+ waitException . Set ( ) ;
109108#pragma warning disable CA2219 // Do not raise exceptions in finally clauses
110- throw new InvalidOperationException ( ) ;
109+ throw new InvalidOperationException ( ) ;
111110#pragma warning restore CA2219 // Do not raise exceptions in finally clauses
112- }
113- } ) . WithCancellationAsync ( new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) . Token ) ) ;
114-
115- waitException . WaitOne ( ) ;
116- await Task . Delay ( TimeSpan . FromSeconds ( 4 ) , TestContext . CancellationToken ) ;
117- } , 3 , TimeSpan . FromSeconds ( 3 ) ) ;
111+ }
112+ } ) . WithCancellationAsync ( token ) ) ;
113+ Assert . AreEqual ( token , ex . CancellationToken ) ;
114+ waitException . WaitOne ( ) ;
115+ }
118116
119117 private static async Task < string > DoSomething ( )
120118 {
0 commit comments