Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI/CD: ignore tests that were failing on OSX #493

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions tests/ExchangeSharpTests/CryptoUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,32 +226,47 @@ public void ConvertInvariantTest()
}
}

[TestMethod]
[ConditionalTestMethod]
[PlatformSpecificTest(
~TestPlatforms.OSX,
"Has an issue on MacOS. See https://github.com/dotnet/corefx/issues/42607"
)]
public async Task RateGate()
{
const int timesPerPeriod = 1;
const int ms = 100;
const int loops = 5;
double msMax = (double)ms * 1.5;
double msMin = (double)ms * (1.0 / 1.5);
RateGate gate = new RateGate(timesPerPeriod, TimeSpan.FromMilliseconds(ms));
if (!(await gate.WaitToProceedAsync(0)))
{
throw new APIException("Rate gate should have allowed immediate access to first attempt");
}
for (int i = 0; i < loops; i++)
{
Stopwatch timer = Stopwatch.StartNew();
await gate.WaitToProceedAsync();
timer.Stop();

if (i > 0)
{
// check for too much elapsed time with a little fudge
Assert.IsTrue(timer.Elapsed.TotalMilliseconds <= msMax, "Rate gate took too long to wait in between calls: " + timer.Elapsed.TotalMilliseconds + "ms");
Assert.IsTrue(timer.Elapsed.TotalMilliseconds >= msMin, "Rate gate took too little to wait in between calls: " + timer.Elapsed.TotalMilliseconds + "ms");
}
}
const int timesPerPeriod = 1;
const int ms = 100;
const int loops = 5;
const double msMax = (double) ms * 1.5;
const double msMin = (double) ms * (1.0 / 1.5);
var gate = new RateGate(timesPerPeriod, TimeSpan.FromMilliseconds(ms));

var entered = await gate.WaitToProceedAsync(0);
if (!entered)
{
throw new APIException("Rate gate should have allowed immediate access to first attempt");
}

for (var i = 0; i < loops; i++)
{
var timer = Stopwatch.StartNew();
await gate.WaitToProceedAsync();
timer.Stop();

if (i <= 0)
{
continue;
}

// check for too much elapsed time with a little fudge
Assert.IsTrue(
timer.Elapsed.TotalMilliseconds <= msMax,
"Rate gate took too long to wait in between calls: " + timer.Elapsed.TotalMilliseconds + "ms"
);
Assert.IsTrue(
timer.Elapsed.TotalMilliseconds >= msMin,
"Rate gate took too little to wait in between calls: " + timer.Elapsed.TotalMilliseconds + "ms"
);
}
}
}
}
}
1 change: 0 additions & 1 deletion tests/ExchangeSharpTests/ExchangeSharpTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<PackageReference Include="MSTest.TestFramework" Version="1.2.1" />
<PackageReference Include="NSubstitute" Version="3.1.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.10" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 54 additions & 0 deletions tests/ExchangeSharpTests/Utility/ConditionalTestMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;

// ReSharper disable once CheckNamespace
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
/// <summary>
/// An extension to the [TestMethod] attribute. It walks the method hierarchy looking
/// for an [IgnoreIf] attribute. If one or more are found, they are each evaluated, if the attribute
/// returns `true`, evaluation is short-circuited, and the test method is skipped.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class ConditionalTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
var ignoreAttributes = FindAttributes(testMethod);

// Evaluate each attribute, and skip if one returns `true`
foreach (var ignoreAttribute in ignoreAttributes)
{
if (!ignoreAttribute.ShouldIgnore(testMethod))
continue;

var message =
"Test not executed. " +
(string.IsNullOrWhiteSpace(ignoreAttribute.Message)
? $"Conditionally ignored by {ignoreAttribute.GetType().Name}."
: ignoreAttribute.Message);

return new[]
{
new TestResult
{
Outcome = UnitTestOutcome.Inconclusive,
TestFailureException = new AssertInconclusiveException(message)
}
};
}

return base.Execute(testMethod);
}

private IEnumerable<IgnoreIfAttribute> FindAttributes(ITestMethod testMethod)
{
// Look for an [IgnoreIf] on the method, including any virtuals this method overrides
var ignoreAttributes = new List<IgnoreIfAttribute>();

ignoreAttributes.AddRange(testMethod.GetAttributes<IgnoreIfAttribute>(inherit: true));

return ignoreAttributes;
}
}
}
58 changes: 58 additions & 0 deletions tests/ExchangeSharpTests/Utility/IgnoreIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

// ReSharper disable once CheckNamespace
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
/// <summary>
/// An extension to the [Ignore] attribute. Instead of using test lists / test categories to conditionally
/// skip tests, allow a [TestClass] or [TestMethod] to specify a method to run. If the member returns
/// `true` the test method will be skipped. The "ignore criteria" method or property must be `static`, return a single
/// `bool` value, and not accept any parameters.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class IgnoreIfAttribute : Attribute
{
public string IgnoreCriteriaMemberName { get; }

public string Message { get; }

public IgnoreIfAttribute(string ignoreCriteriaMemberName, string message = null)
{
IgnoreCriteriaMemberName = ignoreCriteriaMemberName;
Message = message;
}

internal virtual bool ShouldIgnore(ITestMethod testMethod)
{
try
{
// Search for the method or prop specified by name in this class or any parent classes.
var searchFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy |
BindingFlags.Static;
Debug.Assert(testMethod.MethodInfo.DeclaringType != null,
"testMethod.MethodInfo.DeclaringType != null");
var member = testMethod.MethodInfo.DeclaringType.GetMember(IgnoreCriteriaMemberName, searchFlags)
.FirstOrDefault();

switch (member)
{
case MethodInfo method:
return (bool) method.Invoke(null, null);
case PropertyInfo prop:
return (bool) prop.GetValue(null);
default:
throw new ArgumentOutOfRangeException(nameof(member));
}
}
catch (Exception e)
{
var message =
$"Conditional ignore bool returning method/prop {IgnoreCriteriaMemberName} not found. Ensure the method/prop is in the same class as the test method, marked as `static`, returns a `bool`, and doesn't accept any parameters.";
throw new ArgumentException(message, e);
}
}
}
}
40 changes: 40 additions & 0 deletions tests/ExchangeSharpTests/Utility/PlatformSpecificTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Runtime.InteropServices;

// ReSharper disable once CheckNamespace
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class PlatformSpecificTestAttribute : IgnoreIfAttribute
{
public static OSPlatform NetBSD { get; } = OSPlatform.Create("NETBSD");

public TestPlatforms FlagPlatform { get; }

public PlatformSpecificTestAttribute(TestPlatforms flagPlatform, string message = null)
: base(null, message)
{
FlagPlatform = flagPlatform;
}

internal override bool ShouldIgnore(ITestMethod testMethod)
{
var shouldRun = false;

if (FlagPlatform.HasFlag(TestPlatforms.Any))
return true;
if (FlagPlatform.HasFlag(TestPlatforms.Windows))
shouldRun = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (FlagPlatform.HasFlag(TestPlatforms.Linux))
shouldRun = shouldRun || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
if (FlagPlatform.HasFlag(TestPlatforms.OSX))
shouldRun = shouldRun || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
if (FlagPlatform.HasFlag(TestPlatforms.FreeBSD))
shouldRun = shouldRun || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
if (FlagPlatform.HasFlag(TestPlatforms.NetBSD))
shouldRun = shouldRun || RuntimeInformation.IsOSPlatform(NetBSD);

return !shouldRun;
}
}
}
17 changes: 17 additions & 0 deletions tests/ExchangeSharpTests/Utility/TestPlatforms.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

// ReSharper disable once CheckNamespace
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
[Flags]
public enum TestPlatforms
{
Windows = 1,
Linux = 2,
OSX = 4,
FreeBSD = 8,
NetBSD = 16,
AnyUnix = FreeBSD | Linux | NetBSD | OSX,
Any = ~0
}
}