<commit_before>﻿using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using SharpRemote.Exceptions;
using SharpRemote.Hosting;
using SharpRemote.Hosting.OutOfProcess;
using SharpRemote.Test.Types.Classes;
using SharpRemote.Test.Types.Interfaces.PrimitiveTypes;

namespace SharpRemote.Test.Hosting.OutOfProcess
{
	[TestFixture]
	public sealed class StartTest
		: AbstractTest
	{
		[Test]
		[NUnit.Framework.Description("Verifies that starting the default host process succeeds")]
		public void TestStart1()
		{
			using (var silo = new OutOfProcessSilo())
			{
				silo.IsProcessRunning.Should().BeFalse();
				silo.Start();

				silo.IsProcessRunning.Should().BeTrue();
				silo.HasProcessFailed.Should().BeFalse();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that starting a non-existant executable throws")]
		public void TestStart2()
		{
			using (var silo = new OutOfProcessSilo("Doesntexist.exe"))
			{
				silo.IsProcessRunning.Should().BeFalse();

				new Action(silo.Start)
					.ShouldThrow<FileNotFoundException>()
					.WithMessage("The system cannot find the file specified");

				silo.IsProcessRunning.Should().BeFalse();
				silo.HasProcessFailed.Should().BeFalse();

				new Action(() => silo.CreateGrain<IVoidMethodInt32Parameter>())
					.ShouldThrow<NotConnectedException>();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that starting a non executable throws")]
		public void TestStart3()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.dll"))
			{
				silo.IsProcessRunning.Should().BeFalse();

				new Action(silo.Start)
					.ShouldThrow<Win32Exception>()
					.WithMessage("The specified executable is not a valid application for this OS platform.");

				silo.IsProcessRunning.Should().BeFalse();
				silo.HasProcessFailed.Should().BeFalse();

				new Action(() => silo.CreateGrain<IVoidMethodInt32Parameter>())
					.ShouldThrow<NotConnectedException>();
			}
		}

		[Test]
		[LocalTest("Puts too much stress on AppVeyor")]
		[NUnit.Framework.Description("Verifies that multiple silos can be started concurrently")]
		public void TestStart4()
		{
			const int taskCount = 16;
			var tasks = new Task[taskCount];
			for (int i = 0; i < taskCount; ++i)
			{
				tasks[i] = new Task(() =>
					{
						using (var silo = new OutOfProcessSilo())
						{
							silo.IsProcessRunning.Should().BeFalse();
							silo.Start();
							silo.IsProcessRunning.Should().BeTrue();

							var proxy = silo.CreateGrain<IGetStringProperty>(typeof (GetStringPropertyImplementation));
							proxy.Value.Should().Be("Foobar");
						}
					});
				tasks[i].Start();
			}
			Task.WaitAll(tasks);
		}

		[Test]
		[NUnit.Framework.Description("Verifies that calling Start() after it has succeeded is not allowed")]
		public void TestStart5()
		{
			using (var silo = new OutOfProcessSilo())
			{
				new Action(silo.Start).ShouldNotThrow();
				new Action(silo.Start).ShouldThrow<InvalidOperationException>();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that exceptions thrown in the child process are marshalled back")]
		public void TestStart6()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.Host.FailsStartup.exe"))
			{
				new Action(silo.Start)
					.ShouldThrow<HandshakeException>()
					.WithMessage(
						"Process 'SharpRemote.Host.FailsStartup.exe' caught an unexpected exception during startup and subsequently failed")
					.WithInnerException<FileNotFoundException>()
					.WithInnerMessage("Shit happens");
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that Start() gives up restarting the (defunct) application after a limited amount of times")]
		public void TestStart7()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.Host.FailsStartup.exe",
				failureHandler: new RestartOnFailureStrategy(startFailureThreshold: 20)))
			{
				new Action(() => silo.Start())
					.ShouldThrow<AggregateException>();

				silo.IsProcessRunning.Should().BeFalse();
			}
		}
	}
}<commit_msg>Simplified test<commit_after>﻿using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using SharpRemote.Exceptions;
using SharpRemote.Hosting;
using SharpRemote.Hosting.OutOfProcess;
using SharpRemote.Test.Types.Classes;
using SharpRemote.Test.Types.Interfaces.PrimitiveTypes;

namespace SharpRemote.Test.Hosting.OutOfProcess
{
	[TestFixture]
	public sealed class StartTest
		: AbstractTest
	{
		[Test]
		[NUnit.Framework.Description("Verifies that starting the default host process succeeds")]
		public void TestStart1()
		{
			using (var silo = new OutOfProcessSilo())
			{
				silo.IsProcessRunning.Should().BeFalse();
				silo.Start();

				silo.IsProcessRunning.Should().BeTrue();
				silo.HasProcessFailed.Should().BeFalse();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that starting a non-existant executable throws")]
		public void TestStart2()
		{
			using (var silo = new OutOfProcessSilo("Doesntexist.exe"))
			{
				silo.IsProcessRunning.Should().BeFalse();

				new Action(silo.Start)
					.ShouldThrow<FileNotFoundException>()
					.WithMessage("The system cannot find the file specified");

				silo.IsProcessRunning.Should().BeFalse();
				silo.HasProcessFailed.Should().BeFalse();

				new Action(() => silo.CreateGrain<IVoidMethodInt32Parameter>())
					.ShouldThrow<NotConnectedException>();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that starting a non executable throws")]
		public void TestStart3()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.dll"))
			{
				silo.IsProcessRunning.Should().BeFalse();

				new Action(silo.Start)
					.ShouldThrow<Win32Exception>()
					.WithMessage("The specified executable is not a valid application for this OS platform.");

				silo.IsProcessRunning.Should().BeFalse();
				silo.HasProcessFailed.Should().BeFalse();

				new Action(() => silo.CreateGrain<IVoidMethodInt32Parameter>())
					.ShouldThrow<NotConnectedException>();
			}
		}

		[Test]
		[LocalTest("Puts too much stress on AppVeyor")]
		[NUnit.Framework.Description("Verifies that multiple silos can be started concurrently")]
		public void TestStart4()
		{
			const int taskCount = 16;
			var tasks = new Task[taskCount];
			for (int i = 0; i < taskCount; ++i)
			{
				tasks[i] = new Task(() =>
					{
						using (var silo = new OutOfProcessSilo())
						{
							silo.IsProcessRunning.Should().BeFalse();
							silo.Start();
							silo.IsProcessRunning.Should().BeTrue();

							var proxy = silo.CreateGrain<IGetStringProperty>(typeof (GetStringPropertyImplementation));
							proxy.Value.Should().Be("Foobar");
						}
					});
				tasks[i].Start();
			}
			Task.WaitAll(tasks);
		}

		[Test]
		[NUnit.Framework.Description("Verifies that calling Start() after it has succeeded is not allowed")]
		public void TestStart5()
		{
			using (var silo = new OutOfProcessSilo())
			{
				new Action(silo.Start).ShouldNotThrow();
				new Action(silo.Start).ShouldThrow<InvalidOperationException>();
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that exceptions thrown in the child process are marshalled back")]
		public void TestStart6()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.Host.FailsStartup.exe"))
			{
				new Action(silo.Start)
					.ShouldThrow<HandshakeException>()
					.WithMessage(
						"Process 'SharpRemote.Host.FailsStartup.exe' caught an unexpected exception during startup and subsequently failed")
					.WithInnerException<FileNotFoundException>()
					.WithInnerMessage("Shit happens");
			}
		}

		[Test]
		[NUnit.Framework.Description("Verifies that Start() gives up restarting the (defunct) application after a limited amount of times")]
		public void TestStart7()
		{
			using (var silo = new OutOfProcessSilo("SharpRemote.Host.FailsStartup.exe",
				failureHandler: new RestartOnFailureStrategy(startFailureThreshold: 20)))
			{
				new Action(silo.Start)
					.ShouldThrow<AggregateException>();

				silo.IsProcessRunning.Should().BeFalse();
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12349619445126442, 'fraction_numerical': 0.00441934691873312, 'mean_word_length': 4.029012345679012, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 32, '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': '819943', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2443, 'n_words': 538}