From 027fad04c0817b55b1f89735bc4d4f5ad9b4e71f Mon Sep 17 00:00:00 2001 From: obligaron Date: Mon, 27 May 2024 09:59:39 +0200 Subject: [PATCH] Remove ITestRunner.TestWorkerId --- CHANGELOG.md | 3 +- .../build/MSTest.AssemblyHooks.template.vb | 2 +- .../XUnitParallelWorkerTracker.cs | 29 ------- .../Generation/UnitTestFeatureGenerator.cs | 8 +- .../IUnitTestGeneratorProvider.cs | 2 - .../MsTestGeneratorProvider.cs | 9 -- .../NUnit3TestGeneratorProvider.cs | 3 - .../XUnit2TestGeneratorProvider.cs | 22 ----- Reqnroll/ISyncTestRunner.cs | 5 -- Reqnroll/ITestRunner.cs | 6 -- Reqnroll/ITestRunnerManager.cs | 3 +- .../Infrastructure/BlockingSyncTestRunner.cs | 1 - .../Infrastructure/TestExecutionEngine.cs | 1 + Reqnroll/TestRunner.cs | 7 -- Reqnroll/TestRunnerManager.cs | 82 +++++++++++-------- Reqnroll/Tracing/TraceListenerQueue.cs | 2 +- .../UnitTestFeatureGeneratorTests.cs | 1 - .../TestRunnerManagerRunnerCreationTests.cs | 12 +-- .../TestRunnerManagerStaticApiTest.cs | 12 +-- .../TestRunnerManagerTests.cs | 25 ++---- .../TraceListenerQueueTests.cs | 2 - .../InAppDomainParallelExecution.feature | 2 +- 22 files changed, 78 insertions(+), 161 deletions(-) delete mode 100644 Plugins/Reqnroll.xUnit.ReqnrollPlugin/XUnitParallelWorkerTracker.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index c0bcf6281..b7bfe66f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ # [vNext] +* Removed `ITestRunner.TestWorkerId` and related code (required to support scenario parallelization) + ## Bug fixes: * Fix: BeforeTestRun not run in .NET462 up to .NET481 in multitarget test project (#146) - # v2.0.0 - 2024-05-22 ## Breaking changes: diff --git a/Plugins/Reqnroll.MSTest.Generator.ReqnrollPlugin/build/MSTest.AssemblyHooks.template.vb b/Plugins/Reqnroll.MSTest.Generator.ReqnrollPlugin/build/MSTest.AssemblyHooks.template.vb index d0af6f83a..14102a49e 100644 --- a/Plugins/Reqnroll.MSTest.Generator.ReqnrollPlugin/build/MSTest.AssemblyHooks.template.vb +++ b/Plugins/Reqnroll.MSTest.Generator.ReqnrollPlugin/build/MSTest.AssemblyHooks.template.vb @@ -16,7 +16,7 @@ Public NotInheritable Class PROJECT_ROOT_NAMESPACE_MSTestAssemblyHooks 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) - Await Global.Reqnroll.TestRunnerManager.OnTestRunStartAsync(currentAssembly, Nothing, containerBuilder) + Await Global.Reqnroll.TestRunnerManager.OnTestRunStartAsync(currentAssembly, containerBuilder) End Function diff --git a/Plugins/Reqnroll.xUnit.ReqnrollPlugin/XUnitParallelWorkerTracker.cs b/Plugins/Reqnroll.xUnit.ReqnrollPlugin/XUnitParallelWorkerTracker.cs deleted file mode 100644 index fdcb3893a..000000000 --- a/Plugins/Reqnroll.xUnit.ReqnrollPlugin/XUnitParallelWorkerTracker.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Threading; - -namespace Reqnroll.xUnit.ReqnrollPlugin; - -public class XUnitParallelWorkerTracker -{ - public static readonly XUnitParallelWorkerTracker Instance = new(); - - private readonly ConcurrentBag _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); - } -} \ No newline at end of file diff --git a/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs b/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs index baba0e0b8..7a7aa925b 100644 --- a/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs +++ b/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs @@ -177,15 +177,9 @@ private void SetupTestClassInitializeMethod(TestClassGenerationContext generatio //testRunner = TestRunnerManager.GetTestRunnerForAssembly(null, [test_worker_id]); var testRunnerField = _scenarioPartHelper.GetTestRunnerExpression(); - var testRunnerParameters = new[] - { - new CodePrimitiveExpression(null), - _testGeneratorProvider.GetTestWorkerIdExpression() - }; - var getTestRunnerExpression = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(_codeDomHelper.GetGlobalizedTypeName(typeof(TestRunnerManager))), - nameof(TestRunnerManager.GetTestRunnerForAssembly), testRunnerParameters); + nameof(TestRunnerManager.GetTestRunnerForAssembly)); testClassInitializeMethod.Statements.Add( new CodeAssignStatement( diff --git a/Reqnroll.Generator/UnitTestProvider/IUnitTestGeneratorProvider.cs b/Reqnroll.Generator/UnitTestProvider/IUnitTestGeneratorProvider.cs index c4bdd3dd2..ea46151c4 100644 --- a/Reqnroll.Generator/UnitTestProvider/IUnitTestGeneratorProvider.cs +++ b/Reqnroll.Generator/UnitTestProvider/IUnitTestGeneratorProvider.cs @@ -28,7 +28,5 @@ public interface IUnitTestGeneratorProvider void SetTestMethodAsRow(TestClassGenerationContext generationContext, CodeMemberMethod testMethod, string scenarioTitle, string exampleSetName, string variantName, IEnumerable> arguments); void MarkCodeMethodInvokeExpressionAsAwait(CodeMethodInvokeExpression expression); - - CodeExpression GetTestWorkerIdExpression(); } } diff --git a/Reqnroll.Generator/UnitTestProvider/MsTestGeneratorProvider.cs b/Reqnroll.Generator/UnitTestProvider/MsTestGeneratorProvider.cs index beb0cb675..cb2b92a4d 100644 --- a/Reqnroll.Generator/UnitTestProvider/MsTestGeneratorProvider.cs +++ b/Reqnroll.Generator/UnitTestProvider/MsTestGeneratorProvider.cs @@ -228,14 +228,5 @@ public void MarkCodeMethodInvokeExpressionAsAwait(CodeMethodInvokeExpression exp { CodeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(expression); } - - public CodeExpression GetTestWorkerIdExpression() - { - // System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() - return new CodeMethodInvokeExpression( - new CodeVariableReferenceExpression("System.Threading.Thread.CurrentThread.ManagedThreadId"), - nameof(ToString) - ); - } } } diff --git a/Reqnroll.Generator/UnitTestProvider/NUnit3TestGeneratorProvider.cs b/Reqnroll.Generator/UnitTestProvider/NUnit3TestGeneratorProvider.cs index 66fc9568f..237aab2f9 100644 --- a/Reqnroll.Generator/UnitTestProvider/NUnit3TestGeneratorProvider.cs +++ b/Reqnroll.Generator/UnitTestProvider/NUnit3TestGeneratorProvider.cs @@ -154,8 +154,5 @@ public void MarkCodeMethodInvokeExpressionAsAwait(CodeMethodInvokeExpression exp { CodeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(expression); } - - public CodeExpression GetTestWorkerIdExpression() - => new CodePropertyReferenceExpression(GetTestContextExpression(), TESTCONTEXT_WORKERID_PROPERTY); } } diff --git a/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs b/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs index 1355cdffd..7b15c51c8 100644 --- a/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs +++ b/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs @@ -33,7 +33,6 @@ public class XUnit2TestGeneratorProvider : IUnitTestGeneratorProvider protected internal const string IGNORE_TEST_CLASS = "IgnoreTestClass"; protected internal const string NONPARALLELIZABLE_COLLECTION_NAME = "ReqnrollNonParallelizableFeatures"; protected internal const string IASYNCLIFETIME_INTERFACE = "Xunit.IAsyncLifetime"; - protected internal const string XUNITPARALLELWORKERTRACKER_INSTANCE = "Reqnroll.xUnit.ReqnrollPlugin.XUnitParallelWorkerTracker.Instance"; public XUnit2TestGeneratorProvider(CodeDomHelper codeDomHelper) { @@ -178,19 +177,6 @@ public virtual void FinalizeTestClass(TestClassGenerationContext generationConte nameof(IObjectContainer.RegisterInstanceAs), new CodeTypeReference(OUTPUT_INTERFACE)), new CodeVariableReferenceExpression(OUTPUT_INTERFACE_FIELD_NAME))); - - // Wrap FeatureTearDown: - // var testWorkerId = .TestWorkerId; - // - // XUnitParallelWorkerTracker.Instance.ReleaseWorker(testWorkerId); - generationContext.TestClassCleanupMethod.Statements.Insert(0, - new CodeVariableDeclarationStatement(typeof(string), "testWorkerId", - new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(generationContext.TestRunnerField.Name), "TestWorkerId"))); - generationContext.TestClassCleanupMethod.Statements.Add( - new CodeMethodInvokeExpression( - new CodeVariableReferenceExpression(GlobalNamespaceIfCSharp(XUNITPARALLELWORKERTRACKER_INSTANCE)), - "ReleaseWorker", - new CodeVariableReferenceExpression("testWorkerId"))); } protected virtual void IgnoreFeature(TestClassGenerationContext generationContext) @@ -414,14 +400,6 @@ public void MarkCodeMethodInvokeExpressionAsAwait(CodeMethodInvokeExpression exp CodeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(expression); } - public CodeExpression GetTestWorkerIdExpression() - { - // XUnitParallelWorkerTracker.Instance.GetWorkerId() - return new CodeMethodInvokeExpression( - new CodeVariableReferenceExpression(GlobalNamespaceIfCSharp(XUNITPARALLELWORKERTRACKER_INSTANCE)), - "GetWorkerId"); - } - private string GlobalNamespaceIfCSharp(string typeName) { return CodeDomHelper.TargetLanguage == CodeDomProviderLanguage.CSharp ? "global::" + typeName : typeName; diff --git a/Reqnroll/ISyncTestRunner.cs b/Reqnroll/ISyncTestRunner.cs index f26dc301b..49ec39bc3 100644 --- a/Reqnroll/ISyncTestRunner.cs +++ b/Reqnroll/ISyncTestRunner.cs @@ -4,11 +4,6 @@ namespace Reqnroll { public interface ISyncTestRunner { - /// - /// The ID of the parallel test worker processing the current scenario. How the worker ID is obtained is dependent on the test execution framework. - /// - string TestWorkerId { get; } - FeatureContext FeatureContext { get; } ScenarioContext ScenarioContext { get; } diff --git a/Reqnroll/ITestRunner.cs b/Reqnroll/ITestRunner.cs index b8a7daff1..201c0e06f 100644 --- a/Reqnroll/ITestRunner.cs +++ b/Reqnroll/ITestRunner.cs @@ -4,16 +4,10 @@ namespace Reqnroll { public interface ITestRunner { - /// - /// The ID of the parallel test worker processing the current scenario. How the worker ID is obtained is dependent on the test execution framework. - /// - string TestWorkerId { get; } FeatureContext FeatureContext { get; } ScenarioContext ScenarioContext { get; } ITestThreadContext TestThreadContext { get; } - void InitializeTestRunner(string testWorkerId); - Task OnTestRunStartAsync(); Task OnTestRunEndAsync(); diff --git a/Reqnroll/ITestRunnerManager.cs b/Reqnroll/ITestRunnerManager.cs index eb96f1d6a..815926d33 100644 --- a/Reqnroll/ITestRunnerManager.cs +++ b/Reqnroll/ITestRunnerManager.cs @@ -10,7 +10,8 @@ public interface ITestRunnerManager Assembly TestAssembly { get; } Assembly[] BindingAssemblies { get; } bool IsMultiThreaded { get; } - ITestRunner GetTestRunner(string workerId); + ITestRunner GetTestRunner(); + void ReleaseTestThreadContext(ITestThreadContext testThreadContext); void Initialize(Assembly testAssembly); Task FireTestRunEndAsync(); Task FireTestRunStartAsync(); diff --git a/Reqnroll/Infrastructure/BlockingSyncTestRunner.cs b/Reqnroll/Infrastructure/BlockingSyncTestRunner.cs index f0d06b789..1f90f57e2 100644 --- a/Reqnroll/Infrastructure/BlockingSyncTestRunner.cs +++ b/Reqnroll/Infrastructure/BlockingSyncTestRunner.cs @@ -16,7 +16,6 @@ private void SyncWrapper(Func asyncCall) asyncCall().GetAwaiter().GetResult(); } - public string TestWorkerId => _testRunner.TestWorkerId; public FeatureContext FeatureContext => _testRunner.FeatureContext; public ScenarioContext ScenarioContext => _testRunner.ScenarioContext; diff --git a/Reqnroll/Infrastructure/TestExecutionEngine.cs b/Reqnroll/Infrastructure/TestExecutionEngine.cs index c0af86828..ec075e06b 100644 --- a/Reqnroll/Infrastructure/TestExecutionEngine.cs +++ b/Reqnroll/Infrastructure/TestExecutionEngine.cs @@ -162,6 +162,7 @@ public virtual async Task OnFeatureEndAsync() _testThreadExecutionEventPublisher.PublishEvent(new FeatureFinishedEvent(FeatureContext)); _contextManager.CleanupFeatureContext(); + _testRunnerManager.ReleaseTestThreadContext(TestThreadContext); } public virtual void OnScenarioInitialize(ScenarioInfo scenarioInfo) diff --git a/Reqnroll/TestRunner.cs b/Reqnroll/TestRunner.cs index b215f3823..ab00c5c83 100644 --- a/Reqnroll/TestRunner.cs +++ b/Reqnroll/TestRunner.cs @@ -8,8 +8,6 @@ public class TestRunner : ITestRunner { private readonly ITestExecutionEngine _executionEngine; - public string TestWorkerId { get; private set; } - public TestRunner(ITestExecutionEngine executionEngine) { _executionEngine = executionEngine; @@ -26,11 +24,6 @@ public async Task OnTestRunStartAsync() await _executionEngine.OnTestRunStartAsync(); } - public void InitializeTestRunner(string testWorkerId) - { - TestWorkerId = testWorkerId; - } - public async Task OnFeatureStartAsync(FeatureInfo featureInfo) { await _executionEngine.OnFeatureStartAsync(featureInfo); diff --git a/Reqnroll/TestRunnerManager.cs b/Reqnroll/TestRunnerManager.cs index 861339de0..43010d61b 100644 --- a/Reqnroll/TestRunnerManager.cs +++ b/Reqnroll/TestRunnerManager.cs @@ -17,19 +17,19 @@ namespace Reqnroll; public class TestRunnerManager : ITestRunnerManager { - public const string TestRunStartWorkerId = "TestRunStart"; - protected readonly IObjectContainer _globalContainer; protected readonly IContainerBuilder _containerBuilder; protected readonly ReqnrollConfiguration _reqnrollConfiguration; protected readonly IRuntimeBindingRegistryBuilder _bindingRegistryBuilder; protected readonly ITestTracer _testTracer; - private readonly ConcurrentDictionary _testRunnerRegistry = new(); + private readonly ConcurrentBag _availableTestWorkerContainers = new(); + private volatile int _usedTestThreadCount = 0; public bool IsTestRunInitialized { get; private set; } private int _wasDisposed = 0; private int _wasSingletonInstanceDisabled = 0; private readonly object _createTestRunnerLockObject = new(); + private volatile ITestRunner _globalTestRunner; public Assembly TestAssembly { get; private set; } public Assembly[] BindingAssemblies { get; private set; } @@ -48,14 +48,13 @@ public TestRunnerManager(IObjectContainer globalContainer, IContainerBuilder con private int GetWorkerTestRunnerCount() { - var hasTestRunStartWorker = _testRunnerRegistry.ContainsKey(TestRunStartWorkerId); - return _testRunnerRegistry.Count - (hasTestRunStartWorker ? 1 : 0); + var hasTestRunStartWorker = _globalTestRunner != null; + return _usedTestThreadCount - (hasTestRunStartWorker ? 1 : 0); } - public virtual ITestRunner CreateTestRunner(string testWorkerId = "default-worker") + public virtual ITestRunner CreateTestRunner() { var testRunner = CreateTestRunnerInstance(); - testRunner.InitializeTestRunner(testWorkerId); if (!IsTestRunInitialized) { @@ -72,6 +71,12 @@ public virtual ITestRunner CreateTestRunner(string testWorkerId = "default-worke return testRunner; } + public virtual void ReleaseTestThreadContext(ITestThreadContext testThreadContext) + { + Interlocked.Decrement(ref _usedTestThreadCount); + _availableTestWorkerContainers.Add(testThreadContext.TestThreadContainer); + } + protected virtual void InitializeBindingRegistry(ITestRunner testRunner) { BindingAssemblies = _bindingRegistryBuilder.GetBindingAssemblies(TestAssembly); @@ -100,10 +105,23 @@ protected internal virtual async Task OnDomainUnloadAsync() await DisposeAsync(); } + ITestRunner GetOrCreateGlobalTestRunner() + { + if (_globalTestRunner == null) + { + lock (_availableTestWorkerContainers) + { + if (_globalTestRunner == null) + _globalTestRunner = GetTestRunner(); + } + } + return _globalTestRunner; + } + public async Task FireTestRunEndAsync() { // this method must not be called multiple times - var onTestRunnerEndExecutionHost = _testRunnerRegistry.Values.FirstOrDefault(); + var onTestRunnerEndExecutionHost = GetOrCreateGlobalTestRunner(); if (onTestRunnerEndExecutionHost != null) await onTestRunnerEndExecutionHost.OnTestRunEndAsync(); } @@ -111,14 +129,19 @@ public async Task FireTestRunEndAsync() public async Task FireTestRunStartAsync() { // this method must not be called multiple times - var onTestRunnerStartExecutionHost = _testRunnerRegistry.Values.FirstOrDefault(); + var onTestRunnerStartExecutionHost = GetOrCreateGlobalTestRunner(); if (onTestRunnerStartExecutionHost != null) await onTestRunnerStartExecutionHost.OnTestRunStartAsync(); } protected virtual ITestRunner CreateTestRunnerInstance() { - var testThreadContainer = _containerBuilder.CreateTestThreadContainer(_globalContainer); + Interlocked.Increment(ref _usedTestThreadCount); + + if (!_availableTestWorkerContainers.TryTake(out var testThreadContainer)) + { + testThreadContainer = _containerBuilder.CreateTestThreadContainer(_globalContainer); + } return testThreadContainer.Resolve(); } @@ -128,12 +151,11 @@ public void Initialize(Assembly assignedTestAssembly) TestAssembly = assignedTestAssembly; } - public virtual ITestRunner GetTestRunner(string testWorkerId) + public virtual ITestRunner GetTestRunner() { - testWorkerId ??= Guid.NewGuid().ToString(); //Creates a Test Runner with a unique test thread try { - return GetTestRunnerWithoutExceptionHandling(testWorkerId); + return GetTestRunnerWithoutExceptionHandling(); } catch (Exception ex) { @@ -142,22 +164,11 @@ public virtual ITestRunner GetTestRunner(string testWorkerId) } } - private ITestRunner GetTestRunnerWithoutExceptionHandling(string testWorkerId) + private ITestRunner GetTestRunnerWithoutExceptionHandling() { - if (testWorkerId == null) - throw new ArgumentNullException(nameof(testWorkerId)); - - bool wasAdded = false; + var testRunner = CreateTestRunner(); - var testRunner = _testRunnerRegistry.GetOrAdd( - testWorkerId, - workerId => - { - wasAdded = true; - return CreateTestRunner(workerId); - }); - - if (wasAdded && IsMultiThreaded && Interlocked.CompareExchange(ref _wasSingletonInstanceDisabled, 1, 0) == 0) + if (IsMultiThreaded && Interlocked.CompareExchange(ref _wasSingletonInstanceDisabled, 1, 0) == 0) { FeatureContext.DisableSingletonInstance(); ScenarioContext.DisableSingletonInstance(); @@ -173,15 +184,19 @@ public virtual async Task DisposeAsync() { await FireTestRunEndAsync(); - foreach (var testRunner in _testRunnerRegistry.Values) + if (_globalTestRunner != null) + { + ReleaseTestThreadContext(_globalTestRunner.TestThreadContext); + } + + while (_availableTestWorkerContainers.TryTake(out var testThreadContainer)) { - testRunner.TestThreadContext.TestThreadContainer.Dispose(); + testThreadContainer.Dispose(); } // this call dispose on this object, but the disposeLockObj will avoid double execution _globalContainer.Dispose(); - _testRunnerRegistry.Clear(); OnTestRunnerManagerDisposed(this); } } @@ -256,20 +271,19 @@ public static async Task OnTestRunEndAsync(Assembly testAssembly = null, IContai } } - public static async Task OnTestRunStartAsync(Assembly testAssembly = null, string testWorkerId = null, IContainerBuilder containerBuilder = null) + public static async Task OnTestRunStartAsync(Assembly testAssembly = null, IContainerBuilder containerBuilder = null) { testAssembly ??= GetCallingAssembly(); var testRunnerManager = GetTestRunnerManager(testAssembly, createIfMissing: true, containerBuilder: containerBuilder); - testRunnerManager.GetTestRunner(testWorkerId ?? TestRunStartWorkerId); await testRunnerManager.FireTestRunStartAsync(); } - public static ITestRunner GetTestRunnerForAssembly(Assembly testAssembly = null, string testWorkerId = null, IContainerBuilder containerBuilder = null) + public static ITestRunner GetTestRunnerForAssembly(Assembly testAssembly = null, IContainerBuilder containerBuilder = null) { testAssembly ??= GetCallingAssembly(); var testRunnerManager = GetTestRunnerManager(testAssembly, containerBuilder); - return testRunnerManager.GetTestRunner(testWorkerId); + return testRunnerManager.GetTestRunner(); } internal static async Task ResetAsync() diff --git a/Reqnroll/Tracing/TraceListenerQueue.cs b/Reqnroll/Tracing/TraceListenerQueue.cs index fa13000ca..257a259b2 100644 --- a/Reqnroll/Tracing/TraceListenerQueue.cs +++ b/Reqnroll/Tracing/TraceListenerQueue.cs @@ -67,7 +67,7 @@ public void EnqueueMessage(ITestRunner sourceTestRunner, string message, bool is } } - _messages.Add(new TraceMessage(isToolMessgae, string.Format("#{1}: {0}", message, sourceTestRunner.TestWorkerId))); + _messages.Add(new TraceMessage(isToolMessgae, message)); } public void Dispose() diff --git a/Tests/Reqnroll.GeneratorTests/UnitTestFeatureGeneratorTests.cs b/Tests/Reqnroll.GeneratorTests/UnitTestFeatureGeneratorTests.cs index 0964fed34..855d66ddc 100644 --- a/Tests/Reqnroll.GeneratorTests/UnitTestFeatureGeneratorTests.cs +++ b/Tests/Reqnroll.GeneratorTests/UnitTestFeatureGeneratorTests.cs @@ -28,7 +28,6 @@ protected virtual void SetupInternal() { Container = new GeneratorContainerBuilder().CreateContainer(new ReqnrollConfigurationHolder(ConfigSource.Default, null), new ProjectSettings(), Enumerable.Empty()); UnitTestGeneratorProviderMock = new Mock(); - UnitTestGeneratorProviderMock.Setup(utp => utp.GetTestWorkerIdExpression()).Returns(new CodePrimitiveExpression(null)); Container.RegisterInstanceAs(UnitTestGeneratorProviderMock.Object); } diff --git a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerRunnerCreationTests.cs b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerRunnerCreationTests.cs index 3305037fa..ca04dc9a5 100644 --- a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerRunnerCreationTests.cs +++ b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerRunnerCreationTests.cs @@ -48,7 +48,7 @@ public void Should_resolve_a_test_runner() { var factory = CreateTestRunnerFactory(); - var testRunner = factory.CreateTestRunner("0"); + var testRunner = factory.CreateTestRunner(); testRunner.Should().NotBeNull(); } @@ -56,7 +56,7 @@ public void Should_resolve_a_test_runner() public void Should_initialize_test_runner_with_the_provided_assembly() { var factory = CreateTestRunnerFactory(); - factory.CreateTestRunner("0"); + factory.CreateTestRunner(); factory.IsTestRunInitialized.Should().BeTrue(); } @@ -67,7 +67,7 @@ public void Should_initialize_test_runner_with_additional_step_assemblies() var factory = CreateTestRunnerFactory(); _reqnrollConfigurationStub.AddAdditionalStepAssembly(anotherAssembly); - factory.CreateTestRunner("0"); + factory.CreateTestRunner(); factory.IsTestRunInitialized.Should().BeTrue(); } @@ -78,7 +78,7 @@ public void Should_initialize_test_runner_with_the_provided_assembly_even_if_the var factory = CreateTestRunnerFactory(); _reqnrollConfigurationStub.AddAdditionalStepAssembly(anotherAssembly); - factory.CreateTestRunner("0"); + factory.CreateTestRunner(); factory.IsTestRunInitialized.Should().BeTrue(); } @@ -92,13 +92,13 @@ public async Task Should_resolve_a_test_runner_specific_test_tracer() //see https://github.com/reqnroll/Reqnroll/issues/638 if (!TestEnvironmentHelper.IsBeingRunByNCrunch()) { - var testRunner1 = TestRunnerManager.GetTestRunnerForAssembly(anAssembly, "0", new RuntimeTestsContainerBuilder()); + var testRunner1 = TestRunnerManager.GetTestRunnerForAssembly(anAssembly, new RuntimeTestsContainerBuilder()); await testRunner1.OnFeatureStartAsync(new FeatureInfo(new CultureInfo("en-US", false), string.Empty, "sds", "sss")); testRunner1.OnScenarioInitialize(new ScenarioInfo("foo", "foo_desc", null, null)); await testRunner1.OnScenarioStartAsync(); var tracer1 = testRunner1.ScenarioContext.ScenarioContainer.Resolve(); - var testRunner2 = TestRunnerManager.GetTestRunnerForAssembly(anAssembly, "1", new RuntimeTestsContainerBuilder()); + var testRunner2 = TestRunnerManager.GetTestRunnerForAssembly(anAssembly, new RuntimeTestsContainerBuilder()); await testRunner2.OnFeatureStartAsync(new FeatureInfo(new CultureInfo("en-US", false), string.Empty, "sds", "sss")); testRunner2.OnScenarioInitialize(new ScenarioInfo("foo", "foo_desc", null, null)); await testRunner1.OnScenarioStartAsync(); diff --git a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerStaticApiTest.cs b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerStaticApiTest.cs index 64b406727..ceeac7b4e 100644 --- a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerStaticApiTest.cs +++ b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerStaticApiTest.cs @@ -19,7 +19,7 @@ public async Task InitializeAsync() [Fact] public async Task GetTestRunner_without_arguments_should_return_TestRunner_instance() { - var testRunner = TestRunnerManager.GetTestRunnerForAssembly(testWorkerId: "0", containerBuilder: new RuntimeTestsContainerBuilder()); + var testRunner = TestRunnerManager.GetTestRunnerForAssembly(containerBuilder: new RuntimeTestsContainerBuilder()); testRunner.Should().NotBeNull(); testRunner.Should().BeOfType(); @@ -28,8 +28,8 @@ public async Task GetTestRunner_without_arguments_should_return_TestRunner_insta [Fact] public async Task GetTestRunner_should_return_different_instances_for_different_assemblies() { - var testRunner1 = TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, "0", containerBuilder: new RuntimeTestsContainerBuilder()); - var testRunner2 = TestRunnerManager.GetTestRunnerForAssembly(anotherAssembly, "0", containerBuilder: new RuntimeTestsContainerBuilder()); + var testRunner1 = TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, containerBuilder: new RuntimeTestsContainerBuilder()); + var testRunner2 = TestRunnerManager.GetTestRunnerForAssembly(anotherAssembly, containerBuilder: new RuntimeTestsContainerBuilder()); testRunner1.Should().NotBe(testRunner2); } @@ -81,7 +81,7 @@ public static void BeforeTestRun() public async Task OnTestRunEnd_should_fire_AfterTestRun_events() { // make sure a test runner is initialized - TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, "0", containerBuilder: new RuntimeTestsContainerBuilder()); + TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, containerBuilder: new RuntimeTestsContainerBuilder()); AfterTestRunTestBinding.AfterTestRunCallCount = 0; //reset await TestRunnerManager.OnTestRunEndAsync(thisAssembly); @@ -93,7 +93,7 @@ public async Task OnTestRunEnd_should_fire_AfterTestRun_events() public async Task OnTestRunEnd_without_arguments_should_fire_AfterTestRun_events_for_calling_assembly() { // make sure a test runner is initialized - TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, "0", containerBuilder: new RuntimeTestsContainerBuilder()); + TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, containerBuilder: new RuntimeTestsContainerBuilder()); AfterTestRunTestBinding.AfterTestRunCallCount = 0; //reset await TestRunnerManager.OnTestRunEndAsync(); @@ -105,7 +105,7 @@ public async Task OnTestRunEnd_without_arguments_should_fire_AfterTestRun_events public async Task OnTestRunEnd_should_not_fire_AfterTestRun_events_multiple_times() { // make sure a test runner is initialized - TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, "0", containerBuilder: new RuntimeTestsContainerBuilder()); + TestRunnerManager.GetTestRunnerForAssembly(thisAssembly, containerBuilder: new RuntimeTestsContainerBuilder()); AfterTestRunTestBinding.AfterTestRunCallCount = 0; //reset await TestRunnerManager.OnTestRunEndAsync(thisAssembly); diff --git a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerTests.cs b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerTests.cs index cd2429250..1d6c4d2cc 100644 --- a/Tests/Reqnroll.RuntimeTests/TestRunnerManagerTests.cs +++ b/Tests/Reqnroll.RuntimeTests/TestRunnerManagerTests.cs @@ -19,7 +19,7 @@ public TestRunnerManagerTests() [Fact] public void CreateTestRunner_should_be_able_to_create_a_TestRunner() { - var testRunner = _testRunnerManager.CreateTestRunner("0"); + var testRunner = _testRunnerManager.CreateTestRunner(); testRunner.Should().NotBeNull(); testRunner.Should().BeOfType(); @@ -28,27 +28,17 @@ public void CreateTestRunner_should_be_able_to_create_a_TestRunner() [Fact] public void GetTestRunner_should_be_able_to_create_a_TestRunner() { - var testRunner = _testRunnerManager.GetTestRunner("0"); + var testRunner = _testRunnerManager.GetTestRunner(); testRunner.Should().NotBeNull(); testRunner.Should().BeOfType(); } - [Fact] - public void GetTestRunner_should_cache_instance() - { - var testRunner1 = _testRunnerManager.GetTestRunner("0"); - var testRunner2 = _testRunnerManager.GetTestRunner("0"); - - - testRunner1.Should().Be(testRunner2); - } - [Fact] public void Should_return_different_instances_for_different_thread_ids() { - var testRunner1 = _testRunnerManager.GetTestRunner("0"); - var testRunner2 = _testRunnerManager.GetTestRunner("1"); + var testRunner1 = _testRunnerManager.GetTestRunner(); + var testRunner2 = _testRunnerManager.GetTestRunner(); testRunner1.Should().NotBe(testRunner2); } @@ -65,8 +55,8 @@ public void Dispose() [Fact] public async Task Should_dispose_test_thread_container_at_after_test_run() { - var testRunner1 = _testRunnerManager.GetTestRunner("0"); - var testRunner2 = _testRunnerManager.GetTestRunner("1"); + var testRunner1 = _testRunnerManager.GetTestRunner(); + var testRunner2 = _testRunnerManager.GetTestRunner(); var disposableClass1 = new DisposableClass(); testRunner1.TestThreadContext.TestThreadContainer.RegisterInstanceAs(disposableClass1, dispose: true); @@ -74,6 +64,9 @@ public async Task Should_dispose_test_thread_container_at_after_test_run() var disposableClass2 = new DisposableClass(); testRunner2.TestThreadContext.TestThreadContainer.RegisterInstanceAs(disposableClass2, dispose: true); + _testRunnerManager.ReleaseTestThreadContext(testRunner1.TestThreadContext); + _testRunnerManager.ReleaseTestThreadContext(testRunner2.TestThreadContext); + await TestRunnerManager.OnTestRunEndAsync(_anAssembly); disposableClass1.IsDisposed.Should().BeTrue(); diff --git a/Tests/Reqnroll.RuntimeTests/TraceListenerQueueTests.cs b/Tests/Reqnroll.RuntimeTests/TraceListenerQueueTests.cs index fa6e09d72..127ce325e 100644 --- a/Tests/Reqnroll.RuntimeTests/TraceListenerQueueTests.cs +++ b/Tests/Reqnroll.RuntimeTests/TraceListenerQueueTests.cs @@ -62,8 +62,6 @@ void WriteTestOutputCallback(string message) private Mock GetTestRunnerMock() { var testRunnerMock = new Mock(); - testRunnerMock.SetupGet(r => r.TestWorkerId) - .Returns(() => Thread.CurrentThread.ManagedThreadId.ToString()); return testRunnerMock; } diff --git a/Tests/Reqnroll.Specs/Features/Execution/InAppDomainParallelExecution.feature b/Tests/Reqnroll.Specs/Features/Execution/InAppDomainParallelExecution.feature index 45b838576..c990000c5 100644 --- a/Tests/Reqnroll.Specs/Features/Execution/InAppDomainParallelExecution.feature +++ b/Tests/Reqnroll.Specs/Features/Execution/InAppDomainParallelExecution.feature @@ -27,7 +27,7 @@ Background: void WhenIDoSomething() { var currentStartIndex = System.Threading.Interlocked.Increment(ref startIndex); - _traceListener.WriteTestOutput($"Start index: {currentStartIndex}, Worker: {_testRunner.TestWorkerId}"); + _traceListener.WriteTestOutput($"Start index: {currentStartIndex}"); System.Threading.Tasks.Task.Delay(500).Wait(); var afterStartIndex = startIndex; if (afterStartIndex == currentStartIndex)