Skip to content

Commit

Permalink
[unity]加入gc的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Jan 3, 2025
1 parent a52bc59 commit 93c6330
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions unity/test/Src/Cases/CrossLang/CrossLangTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

namespace Puerts.UnitTest
{
[UnityEngine.Scripting.Preserve]
public class TestGC
{
public static int ObjCount = 0;

[UnityEngine.Scripting.Preserve]
public TestGC()
{
++ObjCount;
}

[UnityEngine.Scripting.Preserve]

~TestGC()
{
--ObjCount;
}
}

public class TestObject
{
Expand Down Expand Up @@ -1040,5 +1058,51 @@ public void CallDelegateAfterJsEnvDisposed()
callback();
});
}

[Test]
public void TestJsGC()
{
#if PUERTS_GENERAL
var jsEnv = new JsEnv(new TxtLoader());
#else
var jsEnv = new JsEnv(new DefaultLoader());
#endif
var objCount = jsEnv.Eval<int>(@"
const randomCount = Math.floor(Math.random() * 50) + 1;
var objs = []
for (let i = 0; i < randomCount; i++) {
objs.push(new CS.Puerts.UnitTest.TestGC())
}
randomCount;
");

if (jsEnv.Backend is BackendV8)
{
jsEnv.Eval("gc()");
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.AreEqual(objCount, TestGC.ObjCount);
Assert.True(objCount > 0);

jsEnv.Eval("objs = undefined");

if (jsEnv.Backend is BackendV8)
{
jsEnv.Eval("gc()");
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.AreEqual(0, TestGC.ObjCount);

jsEnv.Dispose();
}
}
}

0 comments on commit 93c6330

Please sign in to comment.