Repository navigation
[tests] Isolate JNI reference assertions - #13720
Conversation
Context: #12031 The surfaced-peer registry is process-wide; background runtime activity can add or remove unrelated peers between snapshots, so asserting on the total count was flaky. Count only entries for the peer under test, and dispose the local reference in a finally block. Co-authored-by: Copilot App <[email protected]>
Re-enable the weak peer and JNI reference accounting tests. Track only the test-owned peer or reference events instead of comparing process-wide state, and replace obsolete reflective JNI lookups with current direct APIs. Undo the earlier UnregisterFromRuntime change; the same fix is already proposed in #13700. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the JNI reference-test restoration and isolation changes.
No open issues 🎉
What changed in this PR
This PR restores three JNI reference tests and isolates their assertions from unrelated activity in the shared device-test process, addressing #11690 and part of #12031.
Changes:
- Re-enables the weak-reference test, tracks only its own peer, and waits for GC bridge completion before checking collection.
- Replaces obsolete reflected JNI calls with direct calls and exception-safe reference cleanup.
- Checks global-reference accounting events by handle and test thread, with a tracked reference as a positive control.
| File | Description |
|---|---|
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/JnienvTest.cs |
Restores peer-specific weak-reference assertions with GC bridge synchronization. |
tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs |
Restores direct JNI reference tests and isolates global accounting checks. |
Review effort: Balanced
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Code assessment: no blocking findings. 0 ❌ errors, 0
The direct JNI calls preserve the distinction between raw and accounted references, and the cleanup paths use the matching deletion mechanism. Filtering global-reference events by handle and emitting thread avoids process-wide counter drift; the tracked-reference positive control also prevents an inactive listener from making the negative assertions pass vacuously. The weak-reference test keeps peer construction on a terminated worker thread and uses the existing GC-bridge synchronization helper rather than assuming a fixed number of collections is sufficient.
success, including the aggregate dotnet-android check for build 1694781. Direct Azure build/timeline verification could not be completed: the build query required authentication, and the parallel timeline query failed during CLI initialization. I therefore cannot independently confirm the Azure build result or non-gating device-test outcomes. The Release/CoreCLR, NativeAOT, and stress-run results in the description are author-reported, not independently reproduced in this review.
Generated by Android PR Reviewer for #13720 · copilot · gpt61 · 34.2 AIC · ⌖ 7.94 AIC · ⊞ 20.3K
Comment /review to run again
| } | ||
| } | ||
| Assert.IsNotNull (surfacedPeer, "The new peer should be surfaced."); | ||
| weakPeer = peer.PeerReference.NewWeakGlobalRef (); |
There was a problem hiding this comment.
🤖 💡 Testing — Consider adding Assert.AreSame (peer, JniEnvironment.Runtime.ValueManager.PeekPeer (weakPeer)) here, while peer is still strongly reachable. The post-collection assertion only checks for null; this positive control would establish that the same weak JNI reference initially resolves to this registered peer, so a regression that makes weak-reference lookups always return null cannot satisfy that assertion unnoticed. This follows the useful positive-control pattern already added to the global-reference test.
{Rule: Test assertions must be specific; test edge cases}
Fixes: #11690
Context: #12031
Context: #13700
DoNotLeakWeakReferencescompared process-wide peer snapshots, so unrelated tests and GC could change the count or collect a snapshot target. Re-enable it by tracking only its own peer and waiting for the GC bridge before checking that the surfaced weak reference and peer lookup no longer retain it.The local- and global-reference accounting tests were inconclusive because their reflected JNI entry points no longer exist. Use the current direct JNI calls and clean up raw references without changing accounting. Local counts are thread-local; for global references, check events for the specific handle and test thread, with a tracked reference as a positive control.
Validation
API 35 arm64 emulator (
emulator-5556). All three restored tests passed in both full Release runs:The Release SDK build passed:
Final NativeAOT build/install passed with 39 warnings; test run passed:
Final CoreCLR build/install passed with 208 warnings; test run passed:
Ten-minute before/after stress comparison
On an API 35 arm64 emulator (
emulator-5554), a temporary background thread continuously created batches of 100 unreachable Java peers (no explicit disposal) and calledGC.Collect(). The original and fixed assertions ran alternately in the same process for at least 600 seconds per runtime, with no artificial delays between count reads.CoreCLR created 17,837,600 unreachable peers and completed 35,403 bridge rounds; sampled global counts ranged from 19 to 5,422. NativeAOT created 15,259,900 peers and completed 35,374 bridge rounds; counts ranged from 21 to 7,229. Runtime identity was checked both through
_AndroidRuntimeand the on-deviceIsNativeAotRuntimeflag.The original weak test's assertions were copied unchanged, without its ignore attribute. For the original global test, only the unavailable reflective JNI entry points were replaced with direct equivalents and raw-reference cleanup was placed in
finally; its process-wide count assertions were retained. Expected baseline failures were counted and logged rather than stopping the loop. Each harness run passed its final assertions requiring zero fixed-test failures and active reference-count churn.The temporary harness was removed afterward, leaving the committed patch unchanged. Its patch, first failure stacks, minute-by-minute counters, final device logs, and TRX results are preserved in session artifacts (
jni-ten-minute-*).CoreCLR build/install and ten-minute comparison both passed (208 build warnings, 0 errors):
NativeAOT build/install and ten-minute comparison both passed (39 build warnings, 0 errors):
Debug suite limitation
An unfiltered Debug/CoreCLR run aborted after 565 passes and 3 skips in
ManagedExceptionRoundTripPreservesIdentity (False)with anLREF count is -1assertion. The same abort reproduced withDoNotLeakWeakReferencestemporarily ignored; the local/global reference tests had not run yet: