Skip to content

Commit

Permalink
Integrating PR 1614 (#2582)
Browse files Browse the repository at this point in the history
Co-authored-by: Norbert Žofák <[email protected]>
  • Loading branch information
gasparnagy and NorekZ authored May 5, 2022
1 parent 6250dae commit 8b7eb2e
Show file tree
Hide file tree
Showing 93 changed files with 2,232 additions and 1,816 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Threading.Tasks;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace Specs
Expand All @@ -9,17 +10,17 @@ namespace Specs
public class NUnitAssemblyHooks
{
[OneTimeSetUp]
public void AssemblyInitialize()
public async Task AssemblyInitialize()
{
var currentAssembly = typeof(NUnitAssemblyHooks).Assembly;
TestRunnerManager.OnTestRunStart(currentAssembly);
await TestRunnerManager.OnTestRunStartAsync(currentAssembly);
}

[OneTimeTearDown]
public void AssemblyCleanup()
public async Task AssemblyCleanup()
{
var currentAssembly = typeof(NUnitAssemblyHooks).Assembly;
TestRunnerManager.OnTestRunEnd(currentAssembly);
await TestRunnerManager.OnTestRunEndAsync(currentAssembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
using global::TechTalk.SpecFlow;
using global::TechTalk.SpecFlow.MSTest.SpecFlowPlugin;
using global::System.Runtime.CompilerServices;
using System.Threading.Tasks;

[GeneratedCode("SpecFlow", "SPECFLOW_VERSION")]
[TestClass]
public class PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks
{
[AssemblyInitialize]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AssemblyInitialize(TestContext testContext)
public static async Task AssemblyInitializeAsync(TestContext testContext)
{
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks).Assembly;
var containerBuilder = new MsTestContainerBuilder(testContext);

TestRunnerManager.OnTestRunStart(currentAssembly, containerBuilder);
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly, containerBuilder: containerBuilder);
}

[AssemblyCleanup]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AssemblyCleanup()
public static async Task AssemblyCleanupAsync()
{
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks).Assembly;

TestRunnerManager.OnTestRunEnd(currentAssembly);
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly);
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ Imports System.Runtime.CompilerServices
Public NotInheritable Class PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks
<AssemblyInitialize>
<MethodImpl(MethodImplOptions.NoInlining)>
Public Shared Sub AssemblyInitialize(testContext As TestContext)

Public Shared Async Function AssemblyInitializeAsync(testContext As TestContext) As Task
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks).Assembly
Dim containerBuilder As New MsTestContainerBuilder(testContext)

TestRunnerManager.OnTestRunStart(currentAssembly, containerBuilder)
End Sub
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly, Nothing, containerBuilder)
End Function

<AssemblyCleanup>
<MethodImpl(MethodImplOptions.NoInlining)>
Public Shared Sub AssemblyCleanup()

Public Shared Async Function AssemblyCleanupAsync() As Task
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks).Assembly

TestRunnerManager.OnTestRunEnd(currentAssembly)
End Sub
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly)
End Function

End Class
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@

using System.CodeDom.Compiler;
using System.Diagnostics;
using global::NUnit.Framework;
using global::TechTalk.SpecFlow;
using global::System.Runtime.CompilerServices;
using System.Threading.Tasks;

[GeneratedCode("SpecFlow", "SPECFLOW_VERSION")]
[SetUpFixture]
[global::NUnit.Framework.SetUpFixture]
public class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
{
[OneTimeSetUp]
[global::NUnit.Framework.OneTimeSetUp]
[MethodImpl(MethodImplOptions.NoInlining)]
public void AssemblyInitialize()
public async Task AssemblyInitializeAsync()
{
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks).Assembly;

TestRunnerManager.OnTestRunStart(currentAssembly);
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly);
}

[OneTimeTearDown]
[global::NUnit.Framework.OneTimeTearDown]
[MethodImpl(MethodImplOptions.NoInlining)]
public void AssemblyCleanup()
public async ValueTask AssemblyCleanupAsync()
{
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks).Assembly;

TestRunnerManager.OnTestRunEnd(currentAssembly);
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly);
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ Imports System.Runtime.CompilerServices
Public NotInheritable Class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
<OneTimeSetUp>
<MethodImpl(MethodImplOptions.NoInlining)>
Public Shared Sub AssemblyInitialize()
Public Async Function AssemblyInitializeAsync() As Task
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks).Assembly

TestRunnerManager.OnTestRunStart(currentAssembly)
End Sub
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly)
End Function

<OneTimeTearDown>
<MethodImpl(MethodImplOptions.NoInlining)>
Public Shared Sub AssemblyCleanup()
Public Async Function AssemblyCleanupAsync() As Task
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks).Assembly

TestRunnerManager.OnTestRunEnd(currentAssembly)
End Sub
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly)
End Function

End Class
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<dependency id="SpecFlow.Tools.MsBuild.Generation" version="[$version$]" />
<dependency id="xunit.core" version="2.4.0" />
<dependency id="Xunit.SkippableFact" version="1.3.12" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" />
</group>
<group targetFramework=".NETStandard2.0">
<dependency id="SpecFlow" version="[$version$]" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

using System.CodeDom.Compiler;
using global::System.Runtime.CompilerServices;
using System.Threading.Tasks;

[assembly: global::Xunit.TestFramework("TechTalk.SpecFlow.xUnit.SpecFlowPlugin.XunitTestFrameworkWithAssemblyFixture", "TechTalk.SpecFlow.xUnit.SpecFlowPlugin")]
[assembly: global::TechTalk.SpecFlow.xUnit.SpecFlowPlugin.AssemblyFixture(typeof(global::PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture))]

[GeneratedCode("SpecFlow", "SPECFLOW_VERSION")]
public class PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture : global::System.IDisposable
public class PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture : global::Xunit.IAsyncLifetime
{
private readonly global::System.Reflection.Assembly _currentAssembly;

[MethodImpl(MethodImplOptions.NoInlining)]
public PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture()
public async Task InitializeAsync()
{
_currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly;
global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunStart(_currentAssembly);
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly;
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly);
}

[MethodImpl(MethodImplOptions.NoInlining)]
public void Dispose()
public async Task DisposeAsync()
{
global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunEnd(_currentAssembly);
var currentAssembly = typeof(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly;
await global::TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

Imports System.CodeDom.Compiler
Imports System.Runtime.CompilerServices
Imports System.Threading.Tasks
Imports System.Reflection

<Assembly: Global.Xunit.TestFramework("TechTalk.SpecFlow.xUnit.SpecFlowPlugin.XunitTestFrameworkWithAssemblyFixture", "TechTalk.SpecFlow.xUnit.SpecFlowPlugin")>
<Assembly: Global.TechTalk.SpecFlow.xUnit.SpecFlowPlugin.AssemblyFixture(GetType(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture))>

<GeneratedCode("SpecFlow", "SPECFLOW_VERSION")>
Public Class PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture
Implements Global.System.IDisposable

Private ReadOnly _currentAssembly As Global.System.Reflection.Assembly
Implements Global.Xunit.IAsyncLifetime

<MethodImpl(MethodImplOptions.NoInlining)>
Public Sub New()
_currentAssembly = GetType(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly
Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunStart(_currentAssembly)
End Sub
Public Async Function InitializeAsync() As Task Implements Global.Xunit.IAsyncLifetime.InitializeAsync
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunStartAsync(currentAssembly)
End Function

<MethodImpl(MethodImplOptions.NoInlining)>
Public Sub Dispose() Implements Global.System.IDisposable.Dispose
Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunEnd(_currentAssembly)
End Sub
Private Async Function DisposeAsync() As Task Implements Global.Xunit.IAsyncLifetime.DisposeAsync
Dim currentAssembly As Assembly = GetType(PROJECT_ROOT_NAMESPACE_XUnitAssemblyFixture).Assembly
Await Global.TechTalk.SpecFlow.TestRunnerManager.OnTestRunEndAsync(currentAssembly)
End Function
End Class

<Global.Xunit.CollectionDefinition("SpecFlowNonParallelizableFeatures", DisableParallelization:=True)>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace TechTalk.SpecFlow.xUnit.SpecFlowPlugin;

public class XUnitParallelWorkerTracker
{
public static readonly XUnitParallelWorkerTracker Instance = new();

private readonly ConcurrentBag<string> _availableWorkers = new();
private int _workerCount = 0;

private XUnitParallelWorkerTracker() { }

public string GetWorkerId()
{
if (!_availableWorkers.TryTake(out var workerId))
{
workerId = $"XW{Interlocked.Increment(ref _workerCount):D3}";
}
return workerId;
}

public void ReleaseWorker(string workerId)
{
_availableWorkers.Add(workerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

Expand Down Expand Up @@ -36,27 +37,46 @@ protected override async Task AfterTestAssemblyStartingAsync()
await base.AfterTestAssemblyStartingAsync();

// Go find all the AssemblyFixtureAttributes adorned on the test assembly
Aggregator.Run(() =>
{
var reflectionAssemblyInfo = (IReflectionAssemblyInfo)TestAssembly.Assembly;
var fixturesAttrs = reflectionAssemblyInfo.Assembly
.GetCustomAttributes(typeof(AssemblyFixtureAttribute), false)
.Cast<AssemblyFixtureAttribute>()
.ToList();
// Instantiate all the fixtures
foreach (var fixtureAttr in fixturesAttrs)
await Aggregator.RunAsync(
async () =>
{
_assemblyFixtureMappings[fixtureAttr.FixtureType] = Activator.CreateInstance(fixtureAttr.FixtureType);
}
});
var reflectionAssemblyInfo = (IReflectionAssemblyInfo)TestAssembly.Assembly;
var fixturesAttrs = reflectionAssemblyInfo.Assembly
.GetCustomAttributes(typeof(AssemblyFixtureAttribute), false)
.Cast<AssemblyFixtureAttribute>()
.ToList();
// Instantiate all the fixtures
foreach (var fixtureAttr in fixturesAttrs)
{
object assemblyFixture = Activator.CreateInstance(fixtureAttr.FixtureType);
if (assemblyFixture is IAsyncLifetime assemblyFixtureLifetime)
await assemblyFixtureLifetime.InitializeAsync();
_assemblyFixtureMappings[fixtureAttr.FixtureType] = assemblyFixture;
}
});
}

protected override Task BeforeTestAssemblyFinishedAsync()
{
// Make sure we clean up everybody who is disposable, and use Aggregator.Run to isolate Dispose failures
foreach (var disposable in _assemblyFixtureMappings.Values.OfType<IDisposable>())
Aggregator.Run(disposable.Dispose);
foreach (var potentialDisposable in _assemblyFixtureMappings.Values)
{
if (potentialDisposable is IDisposable disposable)
{
Aggregator.Run(disposable.Dispose);
}
#if NET // IAsyncDisposable supported natively in .NET 5, .NET 6
else if (potentialDisposable is IAsyncDisposable asyncDisposable)
{
Aggregator.RunAsync(async () => await asyncDisposable.DisposeAsync());
}
#endif
else if (potentialDisposable is IAsyncLifetime asyncLifetime)
{
Aggregator.RunAsync(async () => await asyncLifetime.DisposeAsync());
}
}

return base.BeforeTestAssemblyFinishedAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IResult> TransmitProjectCompilingEventAsync()
_specFlowProjectInfo.TargetFrameworks,
_specFlowProjectInfo.CurrentTargetFramework,
_specFlowProjectInfo.ProjectGuid);
return await _analyticsTransmitter.TransmitSpecFlowProjectCompilingEvent(projectCompilingEvent);
return await _analyticsTransmitter.TransmitSpecFlowProjectCompilingEventAsync(projectCompilingEvent);
}
}
}
Loading

0 comments on commit 8b7eb2e

Please sign in to comment.