Make JavaObject finalizer tests deterministic#1408
Merged
jonathanpeppers merged 6 commits intomainfrom Apr 21, 2026
Merged
Conversation
Avoid relying on WaitForGCBridgeProcessing timing in JavaObjectTest. Use explicit completion signals plus a bounded 2-second wait so Dispose_Finalized and the collection tests assert the behavior we actually care about without hanging indefinitely. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR makes JavaObjectTest GC/finalizer-related assertions deterministic by waiting for specific conditions (object collected / finalizer ran) instead of relying on GC bridge processing timing, and by bounding those waits to a 2-second timeout.
Changes:
- Updated
UnreferencedInstanceIsCollectedto wait until theWeakReferenceis dead and the peer is removed from theValueManager. - Updated
Dispose_Finalizedto wait for the finalizer path usingTaskCompletionSourcesignals instead of timing-based flags. - Replaced the old
WaitForGC()helper with two overloads that wait on either a predicate or aTask, with a 2-second timeout.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| Assert.IsFalse (d); | ||
| Assert.IsTrue (f); | ||
| await WaitForGC ( | ||
| () => disposed.Task.IsCompleted || finalized.Task.IsCompleted, |
Member
There was a problem hiding this comment.
Should this be:
Suggested change
| () => disposed.Task.IsCompleted || finalized.Task.IsCompleted, | |
| () => disposed.Task.IsCompleted && finalized.Task.IsCompleted, |
If one completes quicker than the other?
Member
Author
There was a problem hiding this comment.
I'm convinced it's correct the way it is. The expectation is that finalized task completes and disposed does not. So we definitely need || for the passing scenario. Also, if disposed completes, the test will fail and there's no need to wait for the finalized task.
jonathanpeppers
approved these changes
Apr 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Why
Dispose_Finalized is still a useful regression test, but the previous version depended on GC bridge timing rather than the actual contract under test. That became visible from dotnet/android PR #11119, where removing the bridge wait exposed the test as flaky.
This keeps the regression coverage while making the assertions deterministic and implementation-independent.
Validation