Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert performance tests to warnings
- Loading branch information
Showing
3 changed files
with
136 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2017 Charlie Poole, Rob Prouse | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
using System; | ||
using System.Reflection; | ||
using NUnit.Compatibility; | ||
using NUnit.Framework.Constraints; | ||
|
||
namespace NUnit.Framework | ||
{ | ||
public sealed class HelperConstraints | ||
{ | ||
public static Constraint HasMaxTime(int milliseconds) | ||
{ | ||
return new HasMaxTimeConstraint(milliseconds); | ||
} | ||
|
||
private sealed class HasMaxTimeConstraint : Constraint | ||
{ | ||
private readonly int _milliseconds; | ||
|
||
public HasMaxTimeConstraint(int milliseconds) | ||
{ | ||
_milliseconds = milliseconds; | ||
} | ||
|
||
protected override object GetTestObject<TActual>(ActualValueDelegate<TActual> del) | ||
{ | ||
return del; | ||
} | ||
|
||
public override ConstraintResult ApplyTo<TActual>(TActual actual) | ||
{ | ||
var @delegate = actual as Delegate; | ||
if (@delegate == null) | ||
throw new ArgumentException("Actual value must be a delegate.", nameof(actual)); | ||
|
||
var invokeMethod = @delegate.GetType().GetTypeInfo().GetMethod("Invoke"); | ||
if (invokeMethod.GetParameters().Length != 0) | ||
throw new ArgumentException("Delegate must be parameterless.", nameof(actual)); | ||
|
||
var stopwatch = new System.Diagnostics.Stopwatch(); | ||
|
||
#if ASYNC | ||
if (Internal.AsyncInvocationRegion.IsAsyncOperation(@delegate)) | ||
{ | ||
using (var async = Internal.AsyncInvocationRegion.Create(@delegate)) | ||
{ | ||
stopwatch.Start(); | ||
async.WaitForPendingOperationsToComplete(@delegate.DynamicInvoke()); | ||
stopwatch.Stop(); | ||
} | ||
} | ||
else | ||
#endif | ||
{ | ||
stopwatch.Start(); | ||
@delegate.DynamicInvoke(); | ||
stopwatch.Stop(); | ||
} | ||
|
||
return new Result(this, stopwatch.ElapsedMilliseconds); | ||
} | ||
|
||
private sealed class Result : ConstraintResult | ||
{ | ||
private readonly HasMaxTimeConstraint _constraint; | ||
|
||
public Result(HasMaxTimeConstraint constraint, long actualMilliseconds) | ||
: base(constraint, actualMilliseconds, actualMilliseconds <= constraint._milliseconds) | ||
{ | ||
_constraint = constraint; | ||
} | ||
|
||
public override void WriteMessageTo(MessageWriter writer) | ||
{ | ||
writer.Write($"Elapsed time of {ActualValue}ms exceeds maximum of {_constraint._milliseconds}ms"); | ||
} | ||
} | ||
} | ||
} | ||
} |