Stress testing functions from NUnit
NUnit may say that a function works when run once, but what happens if you the same function twice at once. You may find that a static or shared dictionary could crash when adding two identical keys from two seperate threads.
So, here is a nice function to call another function multiple times at once, and verify the output.
/// <summary>
/// Puts a particular method under stress by calling it multiple times at once
/// </summary>
/// <typeparam name=”T”>The type of the parameter sent to the method under test</typeparam>
/// <typeparam name=”TResult”>The type of the result from the method.</typeparam>
/// <param name=”method”>The method.</param>
/// <param name=”parameter”>The parameter to be passed to the method.</param>
/// <param name=”check”>The check.</param>
/// <param name=”load”>The number of times the method should be called at once.</param>
public static void StressTest<T, TResult>(Func<T, TResult> method, T parameter, Predicate<TResult> check, int load)
{
var lWorkers = new List<IAsyncResult>();
for (var i = 0; i < load; i++)
{
lWorkers.Add(method.BeginInvoke(parameter, null, null));
}
foreach (var result in lWorkers.Select(method.EndInvoke))
{
Assert.IsTrue(check(result));
}
}
And that puts the funk into funktion 🙂