Skip to content

Commit 2ca1ac7

Browse files
committed
Add notes on TaskSchedulerTestHelper logic
1 parent 55b600b commit 2ca1ac7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Helpers/TaskSchedulerTestHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExcepti
6161
// Await for the continuation to actually run
6262
_ = await tcs.Task;
6363

64-
// Some additional time to ensure the exception is propagated
64+
// Wait for some additional time to ensure the exception is propagated. This is a bit counterintuitive, but the delay is
65+
// not actually for the event to be raised, but to ensure the task that is throwing has had time to be scheduled and fail.
66+
// The event is raised only when the exception wrapper inside that task is collected and its finalizer has run (that's where
67+
// the logic to raise the event is executed), which is why we're then calling GC.Collect() and GC.WaitForPendingFinalizers().
68+
// That is, we can't use a task completion source from that event, because that event is only guaranteed to actually be raised
69+
// when the finalizer for that task run, which is why we're calling the GC after the delay here.
6570
await Task.Delay(200);
6671

6772
GC.Collect();

0 commit comments

Comments
 (0)