MaxTimeCommand
MaxTimeCommand adjusts the result of a successful test
to a failure if the elapsed time has exceeded the specified maximum
time allowed.
using NUnit.Framework.Interfaces;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal.Commands
{
public class MaxTimeCommand : AfterTestCommand
{
[System.Runtime.CompilerServices.NullableContext(1)]
public MaxTimeCommand(TestCommand innerCommand, int maxTime)
: base(innerCommand)
{
AfterTest = delegate(TestExecutionContext context) {
long num = Stopwatch.GetTimestamp() - context.StartTicks;
double duration = (double)num / (double)Stopwatch.Frequency;
TestResult currentResult = context.CurrentResult;
currentResult.Duration = duration;
if (currentResult.ResultState == ResultState.Success) {
double num2 = currentResult.Duration * 1000;
if (num2 > (double)maxTime)
currentResult.SetResult(ResultState.Failure, $"""{num2}""{maxTime}""");
}
};
}
}
}