public static void ActionRunsInExpectedTime(Action action, int repeatCount, int expectedTimeInMilliseconds)
{
if (action == null)
{
throw new ArgumentNullException("method");
}
if (repeatCount <= 0)
{
throw new ArgumentException("The argument is less than or equal to 0.", "repeatCount");
}
if (expectedTimeInMilliseconds <= 0)
{
throw new ArgumentException("The argument is less than or equal to 0.", "expectedTimeInMilliseconds");
}
Stopwatch watch = new Stopwatch();
for (int i = 0; i < repeatCount; i++)
{
watch.Start();
action();
watch.Stop();
}
string assertionMessage = string.Format(
"The given action does not perform as expected. repeatCount: {0}; expectedTimeInMilliseconds: {1}; actualTimeInMilliseconds: {2}",
repeatCount,
expectedTimeInMilliseconds,
watch.ElapsedMilliseconds);
Assert.IsTrue(watch.ElapsedMilliseconds < expectedTimeInMilliseconds, assertionMessage);
}
}