- Notifications
You must be signed in to change notification settings - Fork750
Fixed CollectBasicObject test#1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,9 +2,9 @@ | ||
using Python.Runtime; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
namespace Python.EmbeddingTest | ||
@@ -28,26 +28,14 @@ public void TearDown() | ||
PythonEngine.Shutdown(); | ||
} | ||
private staticvoid FullGCCollect() | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
[Test] | ||
[Obsolete("GC tests are not guaranteed")] | ||
public void CollectBasicObject() | ||
{ | ||
Assert.IsTrue(Finalizer.Instance.Enable); | ||
@@ -64,11 +52,7 @@ public void CollectBasicObject() | ||
Assert.IsFalse(called, "The event handler was called before it was installed"); | ||
Finalizer.Instance.CollectOnce += handler; | ||
IntPtr pyObj = MakeAGarbage(out var shortWeak, out var longWeak); | ||
FullGCCollect(); | ||
// The object has been resurrected | ||
Warn.If( | ||
@@ -86,7 +70,7 @@ public void CollectBasicObject() | ||
var garbage = Finalizer.Instance.GetCollectedObjects(); | ||
Assert.NotZero(garbage.Count, "There should still be garbage around"); | ||
Warn.Unless( | ||
garbage.Contains(pyObj), | ||
$"The {nameof(longWeak)} reference doesn't show up in the garbage list", | ||
garbage | ||
); | ||
@@ -104,33 +88,45 @@ public void CollectBasicObject() | ||
} | ||
[Test] | ||
[Obsolete("GC tests are not guaranteed")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is NUnit aware of this attribute or does this just generate yet another compile-time warning? Does it help to retry the test usinghttps://docs.nunit.org/articles/nunit/writing-tests/attributes/retry.html? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. NUnit is not aware. This actually removes warning from the usage of other | ||
public void CollectOnShutdown() | ||
{ | ||
IntPtr op = MakeAGarbage(out var shortWeak, out var longWeak); | ||
FullGCCollect(); | ||
Assert.IsFalse(shortWeak.IsAlive); | ||
List<IntPtr>garbage = Finalizer.Instance.GetCollectedObjects(); | ||
Assert.IsNotEmpty(garbage, "The garbage object should be collected"); | ||
Assert.IsTrue(garbage.Contains(op), | ||
"Garbage should contains the collected object"); | ||
PythonEngine.Shutdown(); | ||
garbage = Finalizer.Instance.GetCollectedObjects(); | ||
Assert.IsEmpty(garbage); | ||
} | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] // ensure lack of references to obj | ||
[Obsolete("GC tests are not guaranteed")] | ||
private static IntPtr MakeAGarbage(out WeakReference shortWeak, out WeakReference longWeak) | ||
{ | ||
IntPtr handle = IntPtr.Zero; | ||
WeakReference @short = null, @long = null; | ||
// must create Python object in the thread where we have GIL | ||
IntPtr val = PyLong.FromLong(1024); | ||
// must create temp object in a different thread to ensure it is not present | ||
// when conservatively scanning stack for GC roots. | ||
// see https://xamarin.github.io/bugzilla-archives/17/17593/bug.html | ||
var garbageGen = new Thread(() => | ||
{ | ||
var obj = new PyObject(val, skipCollect: true); | ||
@short = new WeakReference(obj); | ||
@long = new WeakReference(obj, true); | ||
handle = obj.Handle; | ||
}); | ||
garbageGen.Start(); | ||
Assert.IsTrue(garbageGen.Join(TimeSpan.FromSeconds(5)), "Garbage creation timed out"); | ||
shortWeak = @short; | ||
longWeak = @long; | ||
return handle; | ||
} | ||
private static long CompareWithFinalizerOn(PyObject pyCollect, bool enbale) | ||
@@ -191,62 +187,6 @@ public void SimpleTestMemory() | ||
} | ||
} | ||
[Test] | ||
public void ValidateRefCount() | ||
{ | ||
@@ -279,36 +219,13 @@ public void ValidateRefCount() | ||
} | ||
} | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] // ensure lack of references to s1 and s2 | ||
private static IntPtr CreateStringGarbage() | ||
{ | ||
PyString s1 = new PyString("test_string"); | ||
// s2 steal a reference from s1 | ||
PyString s2 = new PyString(s1.Handle); | ||
return s1.Handle; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.