diff --git a/src/Xunit.StaFact/Sdk/UITheoryTestCase.cs b/src/Xunit.StaFact/Sdk/UITheoryTestCase.cs index f8955f8a..827d8d7f 100644 --- a/src/Xunit.StaFact/Sdk/UITheoryTestCase.cs +++ b/src/Xunit.StaFact/Sdk/UITheoryTestCase.cs @@ -25,13 +25,11 @@ public UITheoryTestCase(UITestCase.SyncContextType synchronizationContextType, I internal UITestCase.SyncContextType SynchronizationContextType { get; private set; } - internal ThreadRental ThreadRental { get; private set; } - public override async Task RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) { - this.ThreadRental = await ThreadRental.CreateAsync(UITestCase.GetAdapter(this.SynchronizationContextType), this.TestMethod); - await this.ThreadRental.SynchronizationContext; - return await new UITheoryTestCaseRunner(this, this.DisplayName, this.SkipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource).RunAsync(); + using var threadRental = await ThreadRental.CreateAsync(UITestCase.GetAdapter(this.SynchronizationContextType), this.TestMethod); + await threadRental.SynchronizationContext; + return await new UITheoryTestCaseRunner(this, this.DisplayName, this.SkipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource, threadRental).RunAsync(); } public override void Deserialize(IXunitSerializationInfo data) @@ -47,11 +45,5 @@ public override void Serialize(IXunitSerializationInfo data) data.AddValue("SyncContextType", this.SynchronizationContextType.ToString()); } - - public override void Dispose() - { - this.ThreadRental?.Dispose(); - base.Dispose(); - } } } diff --git a/src/Xunit.StaFact/Sdk/UITheoryTestCaseRunner.cs b/src/Xunit.StaFact/Sdk/UITheoryTestCaseRunner.cs index f43ca934..04e4d887 100644 --- a/src/Xunit.StaFact/Sdk/UITheoryTestCaseRunner.cs +++ b/src/Xunit.StaFact/Sdk/UITheoryTestCaseRunner.cs @@ -11,14 +11,17 @@ namespace Xunit.Sdk public class UITheoryTestCaseRunner : XunitTheoryTestCaseRunner { - public UITheoryTestCaseRunner(UITheoryTestCase testCase, string displayName, string skipReason, object[] constructorArguments, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) + private readonly ThreadRental threadRental; + + internal UITheoryTestCaseRunner(UITheoryTestCase testCase, string displayName, string skipReason, object[] constructorArguments, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, ThreadRental threadRental) : base(testCase, displayName, skipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource) { + this.threadRental = threadRental; } internal new UITheoryTestCase TestCase => (UITheoryTestCase)base.TestCase; protected override XunitTestRunner CreateTestRunner(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, string skipReason, IReadOnlyList beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) - => new UITestRunner(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource, this.TestCase.ThreadRental); + => new UITestRunner(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource, this.threadRental); } }