Fix PeriodicTimer_ActiveOperations_TimerRooted test (#68805)
authorJakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
Tue, 3 May 2022 17:47:09 +0000 (19:47 +0200)
committerGitHub <noreply@github.com>
Tue, 3 May 2022 17:47:09 +0000 (19:47 +0200)
commit6110ab22870762cb84b25eadb3bc8a7cf201f220
tree6bd26b63555ddeb5ccbaa1af292b49b2e8d78005
parenta29f51a9b8b18bd3773c8597effe4f307bd7786c
Fix PeriodicTimer_ActiveOperations_TimerRooted test (#68805)

There are two problems with this test
1. `WaitForNextTickAsync` may return a synchronously completed task, in
   which case it does not root the timer, causing our first
   `WaitForTimerToBeCollected` to fail because the timer was collected.
   This problem is easily reproduced by adding a small sleep after
   constructing the `PeriodicTimer` in `Create`, and I believe it is the
   cause of #59542.
2. There is no guarantee that the timer is not still rooted after the
   wait finishes because the returned `ValueTask<bool>` may be keeping
   it alive (although, it seems unlikely Roslyn will extend the lifetime across the await like this).
   Fixed by wrapping in another NoInlining method.

Fix #59542
src/libraries/System.Runtime/tests/System/Threading/PeriodicTimerTests.cs