Skip to content

[Testing] Make tests depending on GC less flaky#28489

Closed
simonrozsival wants to merge 4 commits intomainfrom
dev/simonrozsival/make-gc-in-tests-less-flaky
Closed

[Testing] Make tests depending on GC less flaky#28489
simonrozsival wants to merge 4 commits intomainfrom
dev/simonrozsival/make-gc-in-tests-less-flaky

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

Description of Change

This PR is motivated by frequently failing SetterSpecificityListTests.RemovingLastValueDoesNotLeak. It is often not enough to call GC.Collect() + GC.WaitForPendingFinalizers() once to ensure that the objects are collected (for some reason). Even in tests in dotnet/runtime, we call GC multiple times to reduce the chance it does not collect what we need (for example https://github.com/dotnet/runtime/blob/367cf39652b1193d04ce3ac345a6384ecba53382/src/tests/nativeaot/SmokeTests/DynamicGenerics/dictionaries.cs#L1516-L1521)

Related to #28357

Copilot AI review requested due to automatic review settings March 18, 2025 21:05
@simonrozsival simonrozsival requested a review from a team as a code owner March 18, 2025 21:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the test infrastructure to reduce flaky behavior by replacing direct GC calls with helper methods that perform multiple collections. Key changes include updating TestHelpers to provide both synchronous and async GC collection helpers, and using these helpers throughout various unit tests to improve GC reliability.

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Controls/tests/Core.UnitTests/TestHelpers.cs Updated GC collection methods; converted an async method to a synchronous one with an added overload for async collection and added a generic WaitForCollect.
src/Controls/tests/Core.UnitTests/SetterSpecificityListTests.cs Replaced direct GC calls with TestHelpers.WaitForCollect for improved object collection assertion.
src/Controls/tests/Core.UnitTests/PlatformBindingTests.cs Replaced direct GC calls with TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/MapTests.cs Switched from Collect to CollectAsync to align with async test behavior.
src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs Replaced direct GC calls with TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/ListProxyTests.cs Updated GC calls with TestHelpers.Collect for consistency.
src/Controls/tests/Core.UnitTests/BindableLayoutTests.cs Replaced GC.Collect sequences with TestHelpers.CollectAsync.
src/Controls/tests/Core.UnitTests/VisualElementTests.cs Updated GC collection calls to use TestHelpers.CollectAsync.
src/Controls/tests/Core.UnitTests/BindingBaseUnitTests.cs Updated direct GC calls to use TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/WindowsTests.cs Replaced direct GC collection with TestHelpers.CollectAsync.
src/Core/tests/UnitTests/MauiContextTests.cs Updated GC calls to use TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/TitleBarTests.cs Replaced GC calls with TestHelpers.CollectAsync.
src/Controls/tests/Core.UnitTests/MessagingCenterTests.cs Replaced GC calls with TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/BindingUnitTests.cs Updated various GC calls with appropriate TestHelpers.Collect/CollectAsync.
src/Controls/tests/Core.UnitTests/WeakEventProxyTests.cs Replaced direct GC calls with TestHelpers.CollectAsync.
src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs Switched GC calls to TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/TypedBindingUnitTests.cs Replaced multiple direct GC calls with TestHelpers.Collect / CollectAsync as appropriate.
src/Controls/tests/Core.UnitTests/Layouts/GridLayoutTests.cs Replaced GC calls with TestHelpers.CollectAsync.
src/Controls/tests/Core.UnitTests/MultiPageTests.cs Updated GC calls to use TestHelpers.Collect.
src/Controls/tests/Core.UnitTests/ListViewTests.cs Replaced direct GC calls with TestHelpers.Collect for consistency.
Comments suppressed due to low confidence (1)

src/Controls/tests/Core.UnitTests/TestHelpers.cs:39

  • [nitpick] Consider renaming WaitForCollect to more clearly communicate its semantics since it returns false when the target is collected. This could help avoid confusion among future maintainers.
public static async Task<bool> WaitForCollect<T>(this WeakReference<T> reference)

{
for (int i = 0; i < 40; i++)
{
await Task.Yield();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this call intentionally in the for loop? I would naively expect it to be called once.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I just modified the existing WaitForCollect for WeakReference<T> and I have not considered changing it.

@PureWeen PureWeen added this to the .NET 9 SR6 milestone Mar 18, 2025
@jsuarezruiz jsuarezruiz added the area-testing Unit tests, device tests label Mar 19, 2025
@simonrozsival simonrozsival marked this pull request as draft March 19, 2025 14:25
@rmarinho rmarinho moved this from Todo to In Progress in MAUI SDK Ongoing Mar 20, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR6, .NET 9 SR7 Mar 24, 2025
@rmarinho
Copy link
Copy Markdown
Member

/rebase

@github-actions github-actions bot force-pushed the dev/simonrozsival/make-gc-in-tests-less-flaky branch from fd6c79f to fa51962 Compare March 26, 2025 12:07
@simonrozsival
Copy link
Copy Markdown
Member Author

I'm not sure what's going on but this PR isn't making it more likely to pass, but it's making them fail every time in CI. The tests are passing just fine for me locally. I don't have time to investigate at the moment, so I'm closing the PR.

@github-project-automation github-project-automation bot moved this from In Progress to Done in MAUI SDK Ongoing Mar 26, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Apr 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-testing Unit tests, device tests

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants