internal static async Task <ThreadRental> CreateAsync(SyncContextAdapter syncContextAdapter, ITestMethod testMethod) { var disposalTaskSource = new TaskCompletionSource <object?>(); var syncContextSource = new TaskCompletionSource <SynchronizationContext>(); var thread = new Thread(() => { var uiSyncContext = syncContextAdapter.Create(); if (syncContextAdapter.ShouldSetAsCurrent) { SynchronizationContext.SetSynchronizationContext(uiSyncContext); } syncContextAdapter.InitializeThread(); syncContextSource.SetResult(uiSyncContext); syncContextAdapter.PumpTill(uiSyncContext, disposalTaskSource.Task); }); thread.Name = $"{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { thread.SetApartmentState(ApartmentState.STA); } thread.Start(); var syncContext = await syncContextSource.Task.ConfigureAwait(false); var rental = new ThreadRental( syncContextAdapter, disposalTaskSource, syncContext); return(rental); }
public override async Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) { 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()); }
/// <summary> /// Initializes a new instance of the <see cref="UITestCaseRunner"/> class. /// </summary> /// <param name="testCase">The test case to be run.</param> /// <param name="displayName">The display name of the test case.</param> /// <param name="skipReason">The skip reason, if the test is to be skipped.</param> /// <param name="constructorArguments">The arguments to be passed to the test class constructor.</param> /// <param name="testMethodArguments">The arguments to be passed to the test method.</param> /// <param name="messageBus">The message bus to report run status to.</param> /// <param name="aggregator">The exception aggregator used to run code and collect exceptions.</param> /// <param name="cancellationTokenSource">The task cancellation token source, used to cancel the test run.</param> /// <param name="threadRental">The <see cref="ThreadRental"/> instance to use.</param> internal UITestCaseRunner( IXunitTestCase testCase, string displayName, string skipReason, object[] constructorArguments, object[] testMethodArguments, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, ThreadRental threadRental) : base(testCase, displayName, skipReason, constructorArguments, testMethodArguments, messageBus, aggregator, cancellationTokenSource) { this.threadRental = threadRental; }
/// <inheritdoc/> public override Task <RunSummary> RunAsync( IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) { var task = Task.Run( async() => { using var threadRental = await ThreadRental.CreateAsync(this.Adapter, this.TestMethod); await threadRental.SynchronizationContext; var runner = new UITestCaseRunner(this, this.DisplayName, this.SkipReason, constructorArguments, this.TestMethodArguments, messageBus, aggregator, cancellationTokenSource, threadRental); return(await runner.RunAsync()); }, cancellationTokenSource.Token); // We need to block the XUnit thread to ensure its concurrency throttle is effective. // See https://github.com/AArnott/Xunit.StaFact/pull/55#issuecomment-826187354 for details. RunSummary runSummary = task.GetAwaiter().GetResult(); return(Task.FromResult(runSummary)); }
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 UITestRunner(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, string skipReason, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, ThreadRental threadRental) : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource) { this.threadRental = threadRental; }