Skip to content

Commit e691ee8

Browse files
[tests] Best effort to await GC bridge processing in CoreCLR (#1338)
I made this change locally to make unit tests pass with CoreCLR + GC Bridge build of dotnet/android. I'm still looking into ways of making this simpler and more reliable.
1 parent b1a0107 commit e691ee8

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

tests/Java.Interop-Tests/Java.Interop/JavaObjectTest.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23

34
using Java.Interop;
45

@@ -73,7 +74,7 @@ public void RegisterWithVM_PermitsAliases ()
7374

7475
#if !NO_GC_BRIDGE_SUPPORT
7576
[Test]
76-
public void UnreferencedInstanceIsCollected ()
77+
public async Task UnreferencedInstanceIsCollected ()
7778
{
7879
JniObjectReference oldHandle = new JniObjectReference ();
7980
WeakReference r = null;
@@ -83,8 +84,7 @@ public void UnreferencedInstanceIsCollected ()
8384
r = new WeakReference (v);
8485
});
8586
JniEnvironment.Runtime.ValueManager.CollectPeers ();
86-
GC.WaitForPendingFinalizers ();
87-
GC.WaitForPendingFinalizers ();
87+
await WaitForGC ();
8888
Assert.IsFalse (r.IsAlive);
8989
Assert.IsNull (r.Target);
9090
Assert.IsNull (JniRuntime.CurrentRuntime.ValueManager.PeekValue (oldHandle));
@@ -105,7 +105,7 @@ public void Dispose ()
105105

106106
#if !NO_GC_BRIDGE_SUPPORT
107107
[Test]
108-
public void Dispose_Finalized ()
108+
public async Task Dispose_Finalized ()
109109
{
110110
var d = false;
111111
var f = false;
@@ -117,12 +117,22 @@ public void Dispose_Finalized ()
117117
JniEnvironment.Runtime.ValueManager.CollectPeers ();
118118
});
119119
JniEnvironment.Runtime.ValueManager.CollectPeers ();
120-
GC.WaitForPendingFinalizers ();
120+
await WaitForGC ();
121121
Assert.IsFalse (d);
122122
Assert.IsTrue (f);
123123
}
124124
#endif // !NO_GC_BRIDGE_SUPPORT
125125

126+
static async Task WaitForGC ()
127+
{
128+
for (int i = 0; i < 3; i++) {
129+
GC.Collect (generation: 2, mode: GCCollectionMode.Forced, blocking: true);
130+
GC.WaitForPendingFinalizers ();
131+
await Task.Yield ();
132+
JniEnvironment.Runtime.ValueManager.WaitForGCBridgeProcessing ();
133+
}
134+
}
135+
126136
[Test]
127137
public void ObjectDisposed ()
128138
{

0 commit comments

Comments
 (0)