<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using Xunit;
using Xunit.Abstractions;

namespace Merq
{
	public abstract class BaseAsyncManagerSpec
	{
		ITestOutputHelper output;

		public BaseAsyncManagerSpec (ITestOutputHelper output)
		{
			this.output = output;
		}

		protected abstract IAsyncManager CreateAsyncManager();

		[StaFact]
		public async void when_switching_to_background_thread_then_changes_current_thread_id ()
		{
			var manager = CreateAsyncManager();

			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (initialThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[StaFact]
		public async void when_switching_to_main_thread_then_switches_to_initial_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (initialThreadId, Thread.CurrentThread.ManagedThreadId);

			await manager.SwitchToMainThread ();

			Assert.Equal (initialThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[StaFact]
		public async void when_switching_to_main_thread_then_switches_to_dispatcher_thread ()
		{
			var manager = CreateAsyncManager();
			var dispatcherThreadId = Dispatcher.CurrentDispatcher.Thread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (dispatcherThreadId, Thread.CurrentThread.ManagedThreadId);

			await manager.SwitchToMainThread ();

			Assert.Equal (dispatcherThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[Fact]
		public void when_running_action_then_runs_on_current_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			manager.Run (async () => {
				Assert.Equal (initialThreadId, Thread.CurrentThread.ManagedThreadId);
				await Task.Yield ();
			});
		}

		[Fact]
		public void when_running_function_then_runs_on_current_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var runThreadId = manager.Run (async () => {
				var threadId = Thread.CurrentThread.ManagedThreadId;
				Assert.Equal (initialThreadId, threadId);
				await Task.Yield ();
				return threadId;
			});

			Assert.Equal (initialThreadId, runThreadId);
		}

		[Fact]
		public void when_run_result_then_returns_value ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var result = manager.Run (() => Task.FromResult("foo"));

			Assert.Equal ("foo", result);
		}

		[Fact]
		public async Task when_runasync_result_then_returns_value ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var result = await manager.RunAsync (() => Task.FromResult("foo"));

			Assert.Equal ("foo", result);
		}
		
		[Fact]
		public void when_run_throws_then_propagates_exception ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			Assert.Throws<InvalidOperationException>(() => manager.Run (async () => {
				await Task.Yield ();
				throw new InvalidOperationException ();
			}));
		}

		[Fact]
		public void when_runasync_throws_then_propagates_exception ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;
<commit_msg>Adds yield to await after a RunAsync<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using Xunit;
using Xunit.Abstractions;

namespace Merq
{
	public abstract class BaseAsyncManagerSpec
	{
		ITestOutputHelper output;

		public BaseAsyncManagerSpec (ITestOutputHelper output)
		{
			this.output = output;
		}

		protected abstract IAsyncManager CreateAsyncManager();

		[StaFact]
		public async void when_switching_to_background_thread_then_changes_current_thread_id ()
		{
			var manager = CreateAsyncManager();

			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (initialThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[StaFact]
		public async void when_switching_to_main_thread_then_switches_to_initial_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (initialThreadId, Thread.CurrentThread.ManagedThreadId);

			await manager.SwitchToMainThread ();

			Assert.Equal (initialThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[StaFact]
		public async void when_switching_to_main_thread_then_switches_to_dispatcher_thread ()
		{
			var manager = CreateAsyncManager();
			var dispatcherThreadId = Dispatcher.CurrentDispatcher.Thread.ManagedThreadId;

			await manager.SwitchToBackground ();

			Assert.NotEqual (dispatcherThreadId, Thread.CurrentThread.ManagedThreadId);

			await manager.SwitchToMainThread ();

			Assert.Equal (dispatcherThreadId, Thread.CurrentThread.ManagedThreadId);
		}

		[Fact]
		public void when_running_action_then_runs_on_current_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			manager.Run (async () => {
				Assert.Equal (initialThreadId, Thread.CurrentThread.ManagedThreadId);
				await Task.Yield ();
			});
		}

		[Fact]
		public void when_running_function_then_runs_on_current_thread ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var runThreadId = manager.Run (async () => {
				var threadId = Thread.CurrentThread.ManagedThreadId;
				Assert.Equal (initialThreadId, threadId);
				await Task.Yield ();
				return threadId;
			});

			Assert.Equal (initialThreadId, runThreadId);
		}

		[Fact]
		public void when_run_result_then_returns_value ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var result = manager.Run (() => Task.FromResult("foo"));

			Assert.Equal ("foo", result);
		}

		[Fact]
		public async Task when_runasync_result_then_returns_value ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			var result = await manager.RunAsync (() => Task.FromResult("foo"));
			
			await Task.Yield ();

			Assert.Equal ("foo", result);
		}
		
		[Fact]
		public void when_run_throws_then_propagates_exception ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

			Assert.Throws<InvalidOperationException>(() => manager.Run (async () => {
				await Task.Yield ();
				throw new InvalidOperationException ();
			}));
		}

		[Fact]
		public void when_runasync_throws_then_propagates_exception ()
		{
			var manager = CreateAsyncManager();
			var initialThreadId = Thread.CurrentThread.ManagedThreadId;

{'fraction_non_alphanumeric': 0.09434752267969296, 'fraction_numerical': 0.0, 'mean_word_length': 4.71451355661882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4326795', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2072, 'n_words': 412}