Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix unit test - Refactor
  • Loading branch information
Martin-Molinero committed Apr 23, 2019
commit 582fe80b62fbee868f57d0f6e6bc2221c3b43833
8 changes: 4 additions & 4 deletions src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void CollectBasicObject()
called = true;
};

Assert.IsFalse(called);
Finalizer.Instance.CollectOnce += handler;

WeakReference shortWeak;
WeakReference longWeak;
{
Expand All @@ -60,12 +63,9 @@ public void CollectBasicObject()
Assert.NotZero(garbage.Count);
Assert.IsTrue(garbage.Any(T => ReferenceEquals(T.Target, longWeak.Target)));
}

Assert.IsFalse(called);
Finalizer.Instance.CollectOnce += handler;
try
{
Finalizer.Instance.CallPendingFinalizers();
Finalizer.Instance.Collect(forceDispose: false);
}
finally
{
Expand Down
24 changes: 9 additions & 15 deletions src/runtime/finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,19 @@ private Finalizer()
Threshold = 200;
}

public bool CallPendingFinalizers()
public void Collect(bool forceDispose = true)
{
if (Instance._finalizerTask != null
&& !Instance._finalizerTask.IsCompleted)
{
var ts = PythonEngine.BeginAllowThreads();
Instance._finalizerTask.Wait();
PythonEngine.EndAllowThreads(ts);
return true;
using (Py.GIL())
{
var ts = PythonEngine.BeginAllowThreads();
Instance._finalizerTask.Wait();
PythonEngine.EndAllowThreads(ts);
}
}
return false;
}

public void Collect()
{
if (!Instance.CallPendingFinalizers())
else if (forceDispose)
{
Instance.DisposeAll();
}
Expand Down Expand Up @@ -133,10 +130,7 @@ internal static void Shutdown()
Instance._objQueue = new ConcurrentQueue<IPyDisposable>();
return;
}
if(!Instance.CallPendingFinalizers())
{
Instance.DisposeAll();
}
Instance.Collect(forceDispose: true);
}

private void AddPendingCollect()
Expand Down