1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed Environment Variable conflict in IISProvider

This commit is contained in:
kay.one 2011-11-13 19:37:36 -08:00
parent fbf7d20c5d
commit bc5307a4d3
8 changed files with 91 additions and 14 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Common.Model;
using NzbDrone.Providers;
using NzbDrone.Test.Common;
using NzbDrone.Test.Dummy;
namespace NzbDrone.App.Test
{
[TestFixture]
public class IISProviderFixture : TestBase
{
[Test]
public void should_not_set_env_varibles_twice()
{
WithTempAsAppPath();
var dummy = StartDummyProcess();
Environment.SetEnvironmentVariable(EnviromentProvider.NZBDRONE_PID, "Test");
Environment.SetEnvironmentVariable(EnviromentProvider.NZBDRONE_PATH, "Test");
Mocker.GetMock<ProcessProvider>()
.Setup(c => c.Start(It.IsAny<ProcessStartInfo>()))
.Returns(dummy);
Mocker.Resolve<IISProvider>().StartServer();
}
[Test]
public void should_set_iis_procces_id()
{
WithTempAsAppPath();
var dummy = StartDummyProcess();
Mocker.GetMock<ProcessProvider>()
.Setup(c => c.Start(It.IsAny<ProcessStartInfo>()))
.Returns(dummy);
//act
Mocker.Resolve<IISProvider>().StartServer();
//assert
Mocker.Resolve<IISProvider>().IISProcessId.Should().Be(dummy.Id);
}
public Process StartDummyProcess()
{
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
return new ProcessProvider().Start(startInfo);
}
}
}

View File

@ -66,6 +66,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CentralDispatchTests.cs" />
<Compile Include="IISProviderFixture.cs" />
<Compile Include="RouterTest.cs" />
<Compile Include="MonitoringProviderTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -4,26 +4,27 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Test.Common;
using NzbDrone.Test.Dummy;
namespace NzbDrone.Common.Test
{
[TestFixture]
public class ProcessProviderTests : TestBase
{
private const string DummyProccessName = "NzbDrone.Test.Dummy";
ProcessProvider _processProvider;
[SetUp]
public void Setup()
{
Process.GetProcessesByName(DummyProccessName).ToList().ForEach(c => c.Kill());
Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c => c.Kill());
_processProvider = new ProcessProvider();
}
[TearDown]
public void TearDown()
{
Process.GetProcessesByName(DummyProccessName).ToList().ForEach(c => c.Kill());
Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c => c.Kill());
}
[TestCase(0)]
@ -58,20 +59,20 @@ public void Should_be_able_to_kill_procces()
[Test]
public void Should_be_able_to_start_process()
{
var startInfo = new ProcessStartInfo(DummyProccessName + ".exe");
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
//Act/Assert
_processProvider.GetProcessByName(DummyProccessName).Should()
_processProvider.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeEmpty("Dummy process is already running");
_processProvider.Start(startInfo).Should().NotBeNull();
_processProvider.GetProcessByName(DummyProccessName).Should()
_processProvider.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.HaveCount(1, "excepted one dummy process to be already running");
}
public Process StartDummyProcess()
{
var startInfo = new ProcessStartInfo(DummyProccessName + ".exe");
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
return _processProvider.Start(startInfo);
}

View File

@ -9,6 +9,9 @@ public class EnviromentProvider
{
public const string IIS_FOLDER_NAME = "iisexpress";
public const string NZBDRONE_PATH = "NZBDRONE_PATH";
public const string NZBDRONE_PID = "NZBDRONE_PID";
#if DEBUG
private static readonly bool isInDebug = true;
#else
@ -98,7 +101,7 @@ public virtual int NzbDroneProcessIdFromEnviroment
{
get
{
var id = Convert.ToInt32(Environment.GetEnvironmentVariable("NZBDRONE_PID"));
var id = Convert.ToInt32(Environment.GetEnvironmentVariable(NZBDRONE_PID));
if (id == 0)
throw new InvalidOperationException("NZBDRONE_PID isn't a valid environment variable.");

View File

@ -3,8 +3,10 @@
namespace NzbDrone.Test.Dummy
{
class Program
public class DummyApp
{
public const string DUMMY_PROCCESS_NAME = "NzbDrone.Test.Dummy";
static void Main(string[] args)
{
Console.WriteLine("Dummy process. ID:{0} Path:{1}", Process.GetCurrentProcess().Id, Process.GetCurrentProcess().MainModule.FileName);

View File

@ -43,7 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="DummyApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -53,9 +53,15 @@ public void StartServer()
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
//Set Variables for the config file.
startInfo.EnvironmentVariables.Add("NZBDRONE_PATH", _enviromentProvider.ApplicationPath);
startInfo.EnvironmentVariables.Add("NZBDRONE_PID", Process.GetCurrentProcess().Id.ToString());
if (!startInfo.EnvironmentVariables.ContainsKey(EnviromentProvider.NZBDRONE_PATH))
{
startInfo.EnvironmentVariables.Add(EnviromentProvider.NZBDRONE_PATH, _enviromentProvider.ApplicationPath);
}
if (!startInfo.EnvironmentVariables.ContainsKey(EnviromentProvider.NZBDRONE_PID))
{
startInfo.EnvironmentVariables.Add(EnviromentProvider.NZBDRONE_PID, Process.GetCurrentProcess().Id.ToString());
}
try
{

View File

@ -117,7 +117,7 @@ public static void AppDomainException(Exception excepion)
}.Submit();
}
Logger.FatalException("EPIC FAIL: {0}", excepion);
Logger.FatalException("EPIC FAIL: " + excepion.Message, excepion);
}
}
}