Skip to content

Parallelize Tests #1047

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

Merged
merged 1 commit into from
Aug 19, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ComponentModel;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Silk.NET.SilkTouch.TestFramework;

public sealed class SilkTouchFrameworkDiscoverer : XunitTestFrameworkDiscoverer
{
public SilkTouchFrameworkDiscoverer(
IAssemblyInfo assemblyInfo,
ISourceInformationProvider sourceProvider,
IMessageSink diagnosticMessageSink,
IXunitTestCollectionFactory? collectionFactory = null)
: base(
assemblyInfo,
sourceProvider,
diagnosticMessageSink,
collectionFactory)
{
}

protected override bool FindTestsForMethod
(
ITestMethod testMethod,
bool includeSourceInformation,
IMessageBus messageBus,
ITestFrameworkDiscoveryOptions discoveryOptions
)
{
var traits = testMethod.Method.GetCustomAttributes(typeof(TraitAttribute))
.Select
(
x =>
{
var args = x.GetConstructorArguments().ToArray();
var name = (string) args[0];
var value = (string) args[1];
return (name, value);
}
);
foreach (var (name, value) in traits)
{
if (name is "Category" &&
value is not "Integration" and not "Scraper" and not "Symbols" and not "Emitter" and not "TypeStore" and not "Type Resolution")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Category " + value +
" is not a valid category. Allowed values are \"Integration\", \"Scraper\", \"Symbols\", \"Emitter\"."
), includeSourceInformation, messageBus
);
}

if (name is "Source Language" &&
value is not "C++")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Source Language " + value +
" is not a valid language. Allowed values are \"C++\"."
), includeSourceInformation, messageBus
);
}

if (name is "Target Language" &&
value is not "C#")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Target Language " + value +
" is not a valid language. Allowed values are \"C#\"."
), includeSourceInformation, messageBus
);
}

if (name is "Feature")
{
if (!SilkTouchTestFramework.Features.TryGetValue(value, out var flag))
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod,
TestMethodDisplayOptions.None, testMethod,
"Feature Flag " + value + " is not a valid flag. Allowed values are " + String.Join
(", ", SilkTouchTestFramework.Features.Keys.Select(x => "\"" + x + "\"")) + "."
), includeSourceInformation, messageBus
);
}

if (!flag)
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new DisabledTestCase
(
"Flag " + value + " is not enabled", this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod,
TestMethodDisplayOptions.None, testMethod
), includeSourceInformation, messageBus
);
}
}
}
return base.FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions);
}

private sealed class DisabledTestCase : XunitTestCase
{
private readonly string _skipReason;

/// <summary />
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete
(
"Called by the de-serializer; should only be called by deriving classes for de-serialization purposes"
)]
public DisabledTestCase() : base()
{
_skipReason = "Generic Skip";
}

public DisabledTestCase
(
string skipReason,
IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
TestMethodDisplayOptions defaultMethodDisplayOptions,
ITestMethod testMethod,
object[]? testMethodArguments = null
) : base
(
diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod,
testMethodArguments
)
{
_skipReason = skipReason;
}

protected override string GetSkipReason(IAttributeInfo factAttribute)
{
return _skipReason;
}
}
}
160 changes: 4 additions & 156 deletions tests/Silk.NET.SilkTouch.TestFramework/SilkTouchTestFramework.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

Expand Down Expand Up @@ -35,157 +32,8 @@ protected override ITestFrameworkDiscoverer CreateDiscoverer(
assemblyInfo,
SourceInformationProvider,
DiagnosticMessageSink);
}

public class SilkTouchFrameworkDiscoverer : XunitTestFrameworkDiscoverer
{
public SilkTouchFrameworkDiscoverer(
IAssemblyInfo assemblyInfo,
ISourceInformationProvider sourceProvider,
IMessageSink diagnosticMessageSink,
IXunitTestCollectionFactory? collectionFactory = null)
: base(
assemblyInfo,
sourceProvider,
diagnosticMessageSink,
collectionFactory)
{
}

protected override bool FindTestsForMethod
(
ITestMethod testMethod,
bool includeSourceInformation,
IMessageBus messageBus,
ITestFrameworkDiscoveryOptions discoveryOptions
)
{
var traits = testMethod.Method.GetCustomAttributes(typeof(TraitAttribute))
.Select
(
x =>
{
var args = x.GetConstructorArguments().ToArray();
var name = (string) args[0];
var value = (string) args[1];
return (name, value);
}
);
foreach (var (name, value) in traits)
{
if (name is "Category" &&
value is not "Integration" and not "Scraper" and not "Symbols" and not "Emitter" and not "TypeStore" and not "Type Resolution")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Category " + value +
" is not a valid category. Allowed values are \"Integration\", \"Scraper\", \"Symbols\", \"Emitter\"."
), includeSourceInformation, messageBus
);
}

if (name is "Source Language" &&
value is not "C++")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Source Language " + value +
" is not a valid language. Allowed values are \"C++\"."
), includeSourceInformation, messageBus
);
}

if (name is "Target Language" &&
value is not "C#")
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None,
testMethod,
"Target Language " + value +
" is not a valid language. Allowed values are \"C#\"."
), includeSourceInformation, messageBus
);
}

if (name is "Feature")
{
if (!SilkTouchTestFramework.Features.TryGetValue(value, out var flag))
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new ExecutionErrorTestCase
(
this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod,
TestMethodDisplayOptions.None, testMethod,
"Feature Flag " + value + " is not a valid flag. Allowed values are " + String.Join
(", ", SilkTouchTestFramework.Features.Keys.Select(x => "\"" + x + "\"")) + "."
), includeSourceInformation, messageBus
);
}

if (!flag)
{
return this.ReportDiscoveredTestCase
(
(ITestCase) new DisabledTestCase
(
"Flag " + value + " is not enabled", this.DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod,
TestMethodDisplayOptions.None, testMethod
), includeSourceInformation, messageBus
);
}
}
}
return base.FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions);
}

private sealed class DisabledTestCase : XunitTestCase
{
private readonly string _skipReason;

/// <summary />
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete
(
"Called by the de-serializer; should only be called by deriving classes for de-serialization purposes"
)]
public DisabledTestCase() : base()
{
_skipReason = "Generic Skip";
}

public DisabledTestCase
(
string skipReason,
IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
TestMethodDisplayOptions defaultMethodDisplayOptions,
ITestMethod testMethod,
object[]? testMethodArguments = null
) : base
(
diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod,
testMethodArguments
)
{
_skipReason = skipReason;
}

protected override string GetSkipReason(IAttributeInfo factAttribute)
{
return _skipReason;
}
}
protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName)
=> new SilkTouchTestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink);
}
}
Loading