views:

101

answers:

2

I'm trying to unit test a private method that I have attached to my FileSystemWatcher's Error event. MSDN says that this event "occurs when the internal buffer overflows." I've tried to cause a buffer overflow but have not been successful so far. The FileSystemWatcher's various properties are:

fileWatcher.IncludeSubdirectories = false;
fileWatcher.Filter = "*";
fileWatcher.NotifyFilter = (NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.Size);

What is the best way of raising this event for the purpose of unit-testing?

+4  A: 

I doubt you will be able to reliably cause a buffer overflow. It will be system and configuration dependent. (It may even be load dependent). I would suggest that instead you mock the FileSystemWatcher class and trigger the mocked event yourself to test your handler.

See Wikipedia: Mock Object and this List of Mocking frameworks for some more details if you're new to the idea of mocking.

Simon P Stevens
+1  A: 

Just added a handler for the Error event to an existing code :). I was testing the processing of the files and with 112 files (as I have from the bug i'm working on) this gets triggered each time I copy the files to the watched folder. Each file is about 72k and I copy them using TotalCommander which I suppose does a plain iterative copy.

Unfortunately for you it all depends on the sys config & load (as simon pointed out).

Sure you can try to flush after each written byte with eventually a number of writers, but nothing can guaranty it.

florin