Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8d2cd16

Browse files
committed
Fix Task test causing CI failures
A Task test is setting a handler for the static UnobservedTaskException event, failing an assert if it's raised. This is problematic if the test runs concurrently with another test that has an unobserved exception. This commit fixes the handler to be more robust and assert only if state related to the one test is incorrect.
1 parent fd08a1e commit 8d2cd16

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/System.Threading.Tasks/tests/System.Runtime.CompilerServices/AsyncTaskMethodBuilderTests.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,22 +434,16 @@ public static void RunAsyncAdditionalBehaviorsTests_NegativeCases2()
434434
cts.Cancel();
435435
b.SignalAndWait(); // release task to complete
436436

437-
// A TCS task
438-
var tcs = new TaskCompletionSource<int>();
439-
tcs.SetCanceled();
440-
Task t2 = tcs.Task;
441-
442-
EventHandler<UnobservedTaskExceptionEventArgs> handler = (s, e) =>
443-
{
444-
Assert.True(false, string.Format(" > OCE shouldn't have resulted in unobserved event firing with " + e.Exception.ToString()));
445-
};
437+
// This test may be run concurrently with other tests in the suite,
438+
// which can be problematic as TaskScheduler.UnobservedTaskException
439+
// is global state. The handler is carefully written to be non-problematic
440+
// if it happens to be set during the execution of another test that has
441+
// an unobserved exception.
442+
EventHandler<UnobservedTaskExceptionEventArgs> handler =
443+
(s, e) => Assert.DoesNotContain(oce, e.Exception.InnerExceptions);
446444
TaskScheduler.UnobservedTaskException += handler;
447-
448445
((IAsyncResult)t1).AsyncWaitHandle.WaitOne();
449-
((IAsyncResult)t2).AsyncWaitHandle.WaitOne();
450446
t1 = null;
451-
t2 = null;
452-
453447
for (int i = 0; i < 10; i++)
454448
{
455449
GC.Collect();

0 commit comments

Comments
 (0)