I have the following class which is a decorator for an IDisposable
object (I have omitted the stuff it adds) which itself implements IDisposable
using
I might be misunderstanding, but:
GC.WaitForPendingFinalizers();
Might do the trick - http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx
When writing unit tests, you should always try to test outside visible behavior, not implementation details. One could argue that supressing finalization is indeed outside visible behavior, but on the other hand, there's probably no way you can (nor should you) mock out the garabage collector.
What you try to make sure in your case, is that a "best-practice" or a coding practice is followed. It should be enforced via a tool that is made for this purpose, such as FxCop.
I use Appdomain (see sample below). Class TemporaryFile creates temporary file in constructor and delete's it in Dispose or in finalizer ~TemporaryFile().
Unfortunately, GC.WaitForPendingFinalizers(); doesn't help me to test finalizer.
[Test]
public void TestTemporaryFile_without_Dispose()
{
const string DOMAIN_NAME = "testDomain";
const string FILENAME_KEY = "fileName";
string testRoot = Directory.GetCurrentDirectory();
AppDomainSetup info = new AppDomainSetup
{
ApplicationBase = testRoot
};
AppDomain testDomain = AppDomain.CreateDomain(DOMAIN_NAME, null, info);
testDomain.DoCallBack(delegate
{
TemporaryFile temporaryFile = new TemporaryFile();
Assert.IsTrue(File.Exists(temporaryFile.FileName));
AppDomain.CurrentDomain.SetData(FILENAME_KEY, temporaryFile.FileName);
});
string createdTemporaryFileName = (string)testDomain.GetData(FILENAME_KEY);
Assert.IsTrue(File.Exists(createdTemporaryFileName));
AppDomain.Unload(testDomain);
Assert.IsFalse(File.Exists(createdTemporaryFileName));
}
It's not easy to test finalization, but it can be easier to test if an object is a subject to garbage collection.
This can be done with a weak references.
In a test, it's important to for the local variables to run out of scope before calling GC.Collect(). The easiest way to make sure is a function scope.
class Stuff
{
~Stuff()
{
}
}
WeakReference CreateWithWeakReference<T>(Func<T> factory)
{
return new WeakReference(factory());
}
[Test]
public void TestEverythingOutOfScopeIsReleased()
{
var tracked = new List<WeakReference>();
var referer = new List<Stuff>();
tracked.Add(CreateWithWeakReference(() => { var stuff = new Stuff(); referer.Add(stuff); return stuff; }));
// Run some code that is expected to release the references
referer.Clear();
GC.Collect();
Assert.IsFalse(tracked.Any(o => o.IsAlive), "All objects should have been released");
}
[Test]
public void TestLocalVariableIsStillInScope()
{
var tracked = new List<WeakReference>();
var referer = new List<Stuff>();
for (var i = 0; i < 10; i++)
{
var stuff = new Stuff();
tracked.Add(CreateWithWeakReference(() => { referer.Add(stuff); return stuff; }));
}
// Run some code that is expected to release the references
referer.Clear();
GC.Collect();
// Following holds because of the stuff variable is still on stack!
Assert.IsTrue(tracked.Count(o => o.IsAlive) == 1, "Should still have a reference to the last one from the for loop");
}