<commit_before>﻿using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

using Xunit.Abstractions;
using Xunit.Sdk;

namespace Roslyn.Test.Utilities
{
    public class WpfTestCase : XunitTestCase
    {
        public WpfTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, ITestMethod testMethod, object[] testMethodArguments = null)
            : base(diagnosticMessageSink, defaultMethodDisplay, testMethod, testMethodArguments) { }

        public override Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            => Task.Factory.StartNew(() =>
            {
                // All of the tests which use StaTestCase require an STA thread, so assert that we are 
                // actually running from an STA Thread (which should be from the StaTaskScheduler pool).
                Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA);

                // xUnit will set and clean up its own context, so assert that none is currently
                // set. If our assert fails, then something wasn't cleaned up properly...
                Debug.Assert(SynchronizationContext.Current == null);

                try
                {
                    // All WPF Tests need a DispatcherSynchronizationContext and we dont want to block pending keyboard
                    // or mouse input from the user. So use background priority which is a single level below user input.
                    var dispatcherSynchronizationContext = new DispatcherSynchronizationContext();

                    // xUnit creates its own synchronization context and wraps any existing context so that messages are
                    // still pumped as necessary. So we are safe setting it here, where we are not safe setting it in test.
                    SynchronizationContext.SetSynchronizationContext(dispatcherSynchronizationContext);

                    // Just call back into the normal xUnit dispatch process now that we are on an STA Thread with no synchronization context.
                    return base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource).Result;
                }
                finally
                {
                    // Cleanup the synchronization context even if the test is failing exceptionally
                    SynchronizationContext.SetSynchronizationContext(null);
                }

            }, CancellationToken.None, TaskCreationOptions.None, StaTaskScheduler.DefaultSta);
    }
}<commit_msg>Pump messages until the test finishes<commit_after>﻿using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

using Xunit.Abstractions;
using Xunit.Sdk;

namespace Roslyn.Test.Utilities
{
    public class WpfTestCase : XunitTestCase
    {
        public WpfTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, ITestMethod testMethod, object[] testMethodArguments = null)
            : base(diagnosticMessageSink, defaultMethodDisplay, testMethod, testMethodArguments) { }

        public override Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            => Task.Factory.StartNew(() =>
            {
                // All of the tests which use StaTestCase require an STA thread, so assert that we are 
                // actually running from an STA Thread (which should be from the StaTaskScheduler pool).
                Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA);

                // xUnit will set and clean up its own context, so assert that none is currently
                // set. If our assert fails, then something wasn't cleaned up properly...
                Debug.Assert(SynchronizationContext.Current == null);

                try
                {
                    // All WPF Tests need a DispatcherSynchronizationContext and we dont want to block pending keyboard
                    // or mouse input from the user. So use background priority which is a single level below user input.
                    var dispatcherSynchronizationContext = new DispatcherSynchronizationContext();

                    // xUnit creates its own synchronization context and wraps any existing context so that messages are
                    // still pumped as necessary. So we are safe setting it here, where we are not safe setting it in test.
                    SynchronizationContext.SetSynchronizationContext(dispatcherSynchronizationContext);

                    // Just call back into the normal xUnit dispatch process now that we are on an STA Thread with no synchronization context.
                    var baseTask = base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);
                    while (!baseTask.IsCompleted)
                    {
                        new FrameworkElement().Dispatcher.DoEvents();
                    }

                    return baseTask.Result;
                }
                finally
                {
                    // Cleanup the synchronization context even if the test is failing exceptionally
                    SynchronizationContext.SetSynchronizationContext(null);
                }

            }, CancellationToken.None, TaskCreationOptions.None, StaTaskScheduler.DefaultSta);
    }
}
{'fraction_non_alphanumeric': 0.055053838138242446, 'fraction_numerical': 0.0, 'mean_word_length': 2.4056771141336486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2400807', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1227, 'n_words': 456}