Fault injection for .NET apps?
Asked Answered
H

4

15

I am wondering if anyone knows of tools or techniques to automatically inject common faults into a running .NET program. Stuff like...

  • Randomly inject an OutOfMemoryException upon allocation
  • Randomly inject FileNotFoundException upon trying to access a files
  • Randomly inject IO or Network exceptions upon using a socket.

So I'm really looking for a way to intercept some specific calls in the CLR similar to what AppVerifier does for native Win32 code. The purpose is to test apps under lots of error conditions beyond the developers control and to make sure such conditions are handled.

Harm answered 11/2, 2010 at 0:46 Comment(3)
Sarcasm Unplug your memory?Autocorrelation
Exactly :) There are a lot of hacks to do this for different resources but it don't scale.Harm
Couldn't you run AppVerifier (or something similar) on the .NET runtime itself?Chef
I
8

There is a codeplex project called TestAPI that can do runtime fault injection. You need to look at its managed code fault injection API. It uses the CLR profiling API to intercept method calls at runtime and modify their behaviour.

Have a look at an example to see how to inject an exception on a method call in an already compiled exe.

Iceskate answered 11/2, 2010 at 1:8 Comment(2)
I read through a couple examples and I think it looks really promising and the fact that it is free certainly helps.Rodolphe
Is there any alternative for this now?Catamite
P
5

Typemock Isolator seems to be your best bet.

Here's what you can do, if you want to throw a FileNotFoundException to simulate testing.

In your production code, you have such method

public static Project OpenProject(string filePath)

And in your test code, you can fake the OpenProject call like this

Isolate.WhenCalled(()=>Project.OpenProject(nulll)).WillThrow(new FileNotFoundException());

And when your code hit OpenProject, a FileNotFoundException will be thrown.

Photoengrave answered 11/2, 2010 at 1:4 Comment(0)
I
2

This isn't exactly on point with what your asking, but it's related and may be helpful toward the same goal of improving exception handling in your app.

redgate Exception Hunter http://www.red-gate.com/products/Exception_Hunter/index.htm

I haven't used this particular product but other redgate products I've used were great.

Iglesias answered 11/2, 2010 at 4:14 Comment(0)
P
0

I don't know how one would "randomly" inject these things, but I would recommend you mock out the part that's not normally under your control, and have the mock throw an exception in some of your tests.

Peursem answered 11/2, 2010 at 1:2 Comment(2)
I get the feeling he is talking about large amounts of code that he wants to verify so I doubt he'll be able to redesign all this code to support mocking.Rodolphe
I'd say that's his problem, but I understand the difficulties of dealing with a large legacy code base. I don't see a way to properly test these scenarios without refactoring for mocking, but maybe someone else will turn something up.Peursem

© 2022 - 2024 — McMap. All rights reserved.