|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | +using System; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace Python.EmbeddingTest |
| 8 | +{ |
| 9 | + public class TestFinalizer |
| 10 | + { |
| 11 | + private int _oldThreshold; |
| 12 | + |
| 13 | + [SetUp] |
| 14 | + public void SetUp() |
| 15 | + { |
| 16 | + _oldThreshold = Finalizer.Instance.Threshold; |
| 17 | + PythonEngine.Initialize(); |
| 18 | + Exceptions.Clear(); |
| 19 | + } |
| 20 | + |
| 21 | + [TearDown] |
| 22 | + public void TearDown() |
| 23 | + { |
| 24 | + Finalizer.Instance.Threshold = _oldThreshold; |
| 25 | + PythonEngine.Shutdown(); |
| 26 | + } |
| 27 | + |
| 28 | + private static void FullGCCollect() |
| 29 | + { |
| 30 | + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); |
| 31 | + GC.WaitForPendingFinalizers(); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void CollectBasicObject() |
| 36 | + { |
| 37 | + Assert.IsTrue(Finalizer.Instance.Enable); |
| 38 | + |
| 39 | + int thId = Thread.CurrentThread.ManagedThreadId; |
| 40 | + Finalizer.Instance.Threshold = 1; |
| 41 | + bool called = false; |
| 42 | + EventHandler<Finalizer.CollectArgs> handler = (s, e) => |
| 43 | + { |
| 44 | + Assert.AreEqual(thId, Thread.CurrentThread.ManagedThreadId); |
| 45 | + Assert.GreaterOrEqual(e.ObjectCount, 1); |
| 46 | + called = true; |
| 47 | + }; |
| 48 | + |
| 49 | + WeakReference shortWeak; |
| 50 | + WeakReference longWeak; |
| 51 | + { |
| 52 | + MakeAGarbage(out shortWeak, out longWeak); |
| 53 | + } |
| 54 | + FullGCCollect(); |
| 55 | + // The object has been resurrected |
| 56 | + Assert.IsFalse(shortWeak.IsAlive); |
| 57 | + Assert.IsTrue(longWeak.IsAlive); |
| 58 | + |
| 59 | + { |
| 60 | + var garbage = Finalizer.Instance.GetCollectedObjects(); |
| 61 | + Assert.NotZero(garbage.Count); |
| 62 | + Assert.IsTrue(garbage.Any(T => ReferenceEquals(T.Target, longWeak.Target))); |
| 63 | + } |
| 64 | + |
| 65 | + Assert.IsFalse(called); |
| 66 | + Finalizer.Instance.CollectOnce += handler; |
| 67 | + try |
| 68 | + { |
| 69 | + Finalizer.Instance.CallPendingFinalizers(); |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + Finalizer.Instance.CollectOnce -= handler; |
| 74 | + } |
| 75 | + Assert.IsTrue(called); |
| 76 | + } |
| 77 | + |
| 78 | + private static void MakeAGarbage(out WeakReference shortWeak, out WeakReference longWeak) |
| 79 | + { |
| 80 | + PyLong obj = new PyLong(1024); |
| 81 | + shortWeak = new WeakReference(obj); |
| 82 | + longWeak = new WeakReference(obj, true); |
| 83 | + obj = null; |
| 84 | + } |
| 85 | + |
| 86 | + private static long CompareWithFinalizerOn(PyObject pyCollect, bool enbale) |
| 87 | + { |
| 88 | + // Must larger than 512 bytes make sure Python use |
| 89 | + string str = new string('1', 1024); |
| 90 | + Finalizer.Instance.Enable = true; |
| 91 | + FullGCCollect(); |
| 92 | + FullGCCollect(); |
| 93 | + pyCollect.Invoke(); |
| 94 | + Finalizer.Instance.Collect(); |
| 95 | + Finalizer.Instance.Enable = enbale; |
| 96 | + |
| 97 | + // Estimate unmanaged memory size |
| 98 | + long before = Environment.WorkingSet - GC.GetTotalMemory(true); |
| 99 | + for (int i = 0; i < 10000; i++) |
| 100 | + { |
| 101 | + // Memory will leak when disable Finalizer |
| 102 | + new PyString(str); |
| 103 | + } |
| 104 | + FullGCCollect(); |
| 105 | + FullGCCollect(); |
| 106 | + pyCollect.Invoke(); |
| 107 | + if (enbale) |
| 108 | + { |
| 109 | + Finalizer.Instance.Collect(); |
| 110 | + } |
| 111 | + |
| 112 | + FullGCCollect(); |
| 113 | + FullGCCollect(); |
| 114 | + long after = Environment.WorkingSet - GC.GetTotalMemory(true); |
| 115 | + return after - before; |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Because of two vms both have their memory manager, |
| 121 | + /// this test only prove the finalizer has take effect. |
| 122 | + /// </summary> |
| 123 | + [Test] |
| 124 | + [Ignore("Too many uncertainties, only manual on when debugging")] |
| 125 | + public void SimpleTestMemory() |
| 126 | + { |
| 127 | + bool oldState = Finalizer.Instance.Enable; |
| 128 | + try |
| 129 | + { |
| 130 | + using (PyObject gcModule = PythonEngine.ImportModule("gc")) |
| 131 | + using (PyObject pyCollect = gcModule.GetAttr("collect")) |
| 132 | + { |
| 133 | + long span1 = CompareWithFinalizerOn(pyCollect, false); |
| 134 | + long span2 = CompareWithFinalizerOn(pyCollect, true); |
| 135 | + Assert.Less(span2, span1); |
| 136 | + } |
| 137 | + } |
| 138 | + finally |
| 139 | + { |
| 140 | + Finalizer.Instance.Enable = oldState; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + class MyPyObject : PyObject |
| 145 | + { |
| 146 | + public MyPyObject(IntPtr op) : base(op) |
| 147 | + { |
| 148 | + } |
| 149 | + |
| 150 | + protected override void Dispose(bool disposing) |
| 151 | + { |
| 152 | + base.Dispose(disposing); |
| 153 | + GC.SuppressFinalize(this); |
| 154 | + throw new Exception("MyPyObject"); |
| 155 | + } |
| 156 | + internal static void CreateMyPyObject(IntPtr op) |
| 157 | + { |
| 158 | + Runtime.Runtime.XIncref(op); |
| 159 | + new MyPyObject(op); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + [Test] |
| 164 | + public void ErrorHandling() |
| 165 | + { |
| 166 | + bool called = false; |
| 167 | + EventHandler<Finalizer.ErrorArgs> handleFunc = (sender, args) => |
| 168 | + { |
| 169 | + called = true; |
| 170 | + Assert.AreEqual(args.Error.Message, "MyPyObject"); |
| 171 | + }; |
| 172 | + Finalizer.Instance.Threshold = 1; |
| 173 | + Finalizer.Instance.ErrorHandler += handleFunc; |
| 174 | + try |
| 175 | + { |
| 176 | + WeakReference shortWeak; |
| 177 | + WeakReference longWeak; |
| 178 | + { |
| 179 | + MakeAGarbage(out shortWeak, out longWeak); |
| 180 | + var obj = (PyLong)longWeak.Target; |
| 181 | + IntPtr handle = obj.Handle; |
| 182 | + shortWeak = null; |
| 183 | + longWeak = null; |
| 184 | + MyPyObject.CreateMyPyObject(handle); |
| 185 | + obj.Dispose(); |
| 186 | + obj = null; |
| 187 | + } |
| 188 | + FullGCCollect(); |
| 189 | + Finalizer.Instance.Collect(); |
| 190 | + Assert.IsTrue(called); |
| 191 | + } |
| 192 | + finally |
| 193 | + { |
| 194 | + Finalizer.Instance.ErrorHandler -= handleFunc; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + [Test] |
| 199 | + public void ValidateRefCount() |
| 200 | + { |
| 201 | + if (!Finalizer.Instance.RefCountValidationEnabled) |
| 202 | + { |
| 203 | + Assert.Pass("Only run with FINALIZER_CHECK"); |
| 204 | + } |
| 205 | + IntPtr ptr = IntPtr.Zero; |
| 206 | + bool called = false; |
| 207 | + Finalizer.IncorrectRefCntHandler handler = (s, e) => |
| 208 | + { |
| 209 | + called = true; |
| 210 | + Assert.AreEqual(ptr, e.Handle); |
| 211 | + Assert.AreEqual(2, e.ImpactedObjects.Count); |
| 212 | + // Fix for this test, don't do this on general environment |
| 213 | + Runtime.Runtime.XIncref(e.Handle); |
| 214 | + return false; |
| 215 | + }; |
| 216 | + Finalizer.Instance.IncorrectRefCntResolver += handler; |
| 217 | + try |
| 218 | + { |
| 219 | + ptr = CreateStringGarbage(); |
| 220 | + FullGCCollect(); |
| 221 | + Assert.Throws<Finalizer.IncorrectRefCountException>(() => Finalizer.Instance.Collect()); |
| 222 | + Assert.IsTrue(called); |
| 223 | + } |
| 224 | + finally |
| 225 | + { |
| 226 | + Finalizer.Instance.IncorrectRefCntResolver -= handler; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + private static IntPtr CreateStringGarbage() |
| 231 | + { |
| 232 | + PyString s1 = new PyString("test_string"); |
| 233 | + // s2 steal a reference from s1 |
| 234 | + PyString s2 = new PyString(s1.Handle); |
| 235 | + return s1.Handle; |
| 236 | + } |
| 237 | + |
| 238 | + } |
| 239 | +} |
0 commit comments