.Net Fault Injection – It’s not just about exceptions

I had a interesting comment on my previous post about .Net fault injection. ‘Losing Side’ asked if this would work for simulating other faults such as timeouts. It’s a good point and one I didn’t think about yesterday, but there are other faults which are interesting when testing the application. Performance is one of those areas, creating performance problems, such as slow disk IO or a slow server is difficult if you don’t have the setup in place, and even then not always possible. How can you effectively, repeatability test for a slow hard drive (and using a virtual machine doesn’t count). Tools such as ANTS Profiler will help tell you where the problems are, but only if you can reproduce the problem.

First demo of the day, I ask the question – how can you simulate a slow write process when using StreamWriter?  Based on my previous post, I’ve changed the method to this:

private static void MethodFails()
{
    Console.WriteLine(“Writing to a file @ ” + DateTime.Now);

    string path = Path.GetTempFileName();
    StreamWriter sw = new StreamWriter(path);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.Close();
    Console.WriteLine(“Done”);

    foreach (var s in File.ReadAllLines(path))
        Console.WriteLine(s);
}

Running my console application normally, I get the following output, notice no delays between each write:

Writing to a file @ 15/11/2008 13:23:48
Done
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48

Running this using my injector, I have a five second delay (which I set, easily could have been a random number) between each of my writes to the file.

Writing to a file @ 15/11/2008 13:19:45
Done
This is a test @ 15/11/2008 13:19:51
This is a test @ 15/11/2008 13:19:56
This is a test @ 15/11/2008 13:20:01
This is a test @ 15/11/2008 13:20:06

I now get the same five second delay each time this happens, creating a scenario repeatable.

Disappointingly, I tried to use the SQLConnection object, but I couldn’t get this to work. I don’t know if its a limitation or a bug.  Still a lot more work to do until its even remotely useable, but I’m finding the concepts interesting.

Technorati Tags: , ,

3 thoughts on “.Net Fault Injection – It’s not just about exceptions”

  1. Hi Ed,

    Thanks for the comment. It’s on my list of books I should read (along with the rest of the pragmatic books… only read a select few).

    But thanks for the heads up, i’ll take a look at the post and reminder to keep it in mind when reading the book itself.

    Ben

  2. hello,Ben.
    Testing Case using in the vs.net IDE for C# programme to find out some bugs,not all.The question is how to use some methods,some tools,some Technology find out?

Leave a Reply to Bluce Lee Cancel reply

Your email address will not be published. Required fields are marked *