From 4a41ce7f29ffd3086d176a46632a112ab1ab2b93 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 13 Aug 2013 20:22:28 -0700 Subject: [PATCH] Updated NzbDroneRunner --- NzbDrone.Common/ProcessProvider.cs | 11 +- NzbDrone.Host/Bootstrap.cs | 3 - NzbDrone.Integration.Test/IntegrationTest.cs | 100 ++---------------- .../NzbDrone.Integration.Test.csproj | 1 + NzbDrone.Integration.Test/NzbDroneRunner.cs | 82 ++++++++++++++ 5 files changed, 97 insertions(+), 100 deletions(-) create mode 100644 NzbDrone.Integration.Test/NzbDroneRunner.cs diff --git a/NzbDrone.Common/ProcessProvider.cs b/NzbDrone.Common/ProcessProvider.cs index 0afd803c8..fe1120411 100644 --- a/NzbDrone.Common/ProcessProvider.cs +++ b/NzbDrone.Common/ProcessProvider.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; @@ -91,6 +90,7 @@ public Process ShellExecute(string path, string args = null, Action onOu UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, + RedirectStandardInput = true }; @@ -125,9 +125,12 @@ public Process ShellExecute(string path, string args = null, Action onOu } }; + process.Start(); + process.BeginErrorReadLine(); process.BeginOutputReadLine(); + return process; } @@ -204,13 +207,15 @@ private static ProcessInfo ConvertToProcessInfo(Process process) private void Kill(int processId) { - if (processId == 0 || Process.GetProcesses().All(p => p.Id != processId)) + var process = Process.GetProcesses().FirstOrDefault(p => p.Id == processId); + + if (process == null) { Logger.Warn("Cannot find process with id: {0}", processId); return; } - var process = Process.GetProcessById(processId); + process.Refresh(); if (process.HasExited) { diff --git a/NzbDrone.Host/Bootstrap.cs b/NzbDrone.Host/Bootstrap.cs index 36b4aecbd..225900e8a 100644 --- a/NzbDrone.Host/Bootstrap.cs +++ b/NzbDrone.Host/Bootstrap.cs @@ -48,9 +48,6 @@ public static IContainer Start(StartupArguments args) var container = MainAppContainerBuilder.BuildContainer(args); - throw new IndexOutOfRangeException(); - - DbFactory.RegisterDatabase(container); container.Resolve().Route(); diff --git a/NzbDrone.Integration.Test/IntegrationTest.cs b/NzbDrone.Integration.Test/IntegrationTest.cs index f5db5d68e..9077b3f4b 100644 --- a/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/NzbDrone.Integration.Test/IntegrationTest.cs @@ -25,109 +25,21 @@ public abstract class IntegrationTest protected ReleaseClient Releases; protected IndexerClient Indexers; + private NzbDroneRunner _runner; + [SetUp] public void SmokeTestSetup() { new StartupArguments(); - KillNzbDrone(); + _runner = new NzbDroneRunner(); + _runner.KillAll(); InitRestClients(); - PackageStart(); + _runner.Start(); } - private static void KillNzbDrone() - { - foreach (var p in Process.GetProcessesByName("NzbDrone.Console")) - { - try - { - p.Kill(); - } - catch (Win32Exception) - { - } - } - } - - private string AppDate; - - private void PackageStart() - { - AppDate = Path.Combine(Directory.GetCurrentDirectory(), "_intg_" + DateTime.Now.Ticks); - - if (BuildInfo.IsDebug) - { - Start("..\\..\\..\\..\\_output\\NzbDrone.Console.exe"); - } - else - { - Start("bin\\NzbDrone.Console.exe"); - } - - while (RestClient.Get(new RestRequest("system/status")).ResponseStatus != ResponseStatus.Completed) - { - - Thread.Sleep(1000); - } - } - - private Process Start(string path) - { - - var args = "-nobrowser -data=\"" + AppDate + "\""; - - var startInfo = new ProcessStartInfo(path, args) - { - CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardError = true, - RedirectStandardOutput = true, - RedirectStandardInput = true - }; - - Console.WriteLine("Starting {0} {1}", path, args); - - var process = new Process - { - StartInfo = startInfo - }; - - process.OutputDataReceived += (sender, eventArgs) => - { - if (string.IsNullOrWhiteSpace(eventArgs.Data)) return; - - if (eventArgs.Data.Contains("Press enter to exit")) - { - process.StandardInput.WriteLine(" "); - } - - Console.WriteLine(eventArgs.Data); - }; - - process.ErrorDataReceived += (sender, eventArgs) => - { - if (string.IsNullOrWhiteSpace(eventArgs.Data)) return; - - if (eventArgs.Data.Contains("Press enter to exit")) - { - process.StandardInput.WriteLine(" "); - } - - Console.WriteLine(eventArgs.Data); - }; - - - process.Start(); - - process.BeginErrorReadLine(); - process.BeginOutputReadLine(); - - process.Exited += (sender, eventArgs) => Assert.Fail("Process exited"); - - return process; - } private void InitRestClients() { @@ -142,7 +54,7 @@ private void InitRestClients() [TearDown] public void SmokeTestTearDown() { - KillNzbDrone(); + _runner.KillAll(); } } diff --git a/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj b/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj index b3ee0c330..6a35e4218 100644 --- a/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj +++ b/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj @@ -95,6 +95,7 @@ + diff --git a/NzbDrone.Integration.Test/NzbDroneRunner.cs b/NzbDrone.Integration.Test/NzbDroneRunner.cs new file mode 100644 index 000000000..cb933fd63 --- /dev/null +++ b/NzbDrone.Integration.Test/NzbDroneRunner.cs @@ -0,0 +1,82 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using NUnit.Framework; +using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; +using RestSharp; + +namespace NzbDrone.Integration.Test +{ + public class NzbDroneRunner + { + private readonly IProcessProvider _processProvider; + private readonly IRestClient _restClient; + private Process _nzbDroneProcess; + + public NzbDroneRunner(int port = 8989) + { + _processProvider = new ProcessProvider(); + _restClient = new RestClient("http://localhost:8989/api"); + } + + + public void Start() + { + AppDate = Path.Combine(Directory.GetCurrentDirectory(), "_intg_" + DateTime.Now.Ticks); + + if (BuildInfo.IsDebug) + { + + Start("..\\..\\..\\..\\_output\\NzbDrone.Console.exe"); + } + else + { + Start("bin\\NzbDrone.Console.exe"); + } + + while (true) + { + + _nzbDroneProcess.Refresh(); + + if (_nzbDroneProcess.HasExited) + { + Assert.Fail("Process has exited"); + } + + if (_restClient.Get(new RestRequest("system/status")).ResponseStatus == ResponseStatus.Completed) + { + return; + } + + Thread.Sleep(500); + } + } + + public void KillAll() + { + _processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName); + _processProvider.KillAll(ProcessProvider.NzbDroneProcessName); + } + + private void Start(string outputNzbdroneConsoleExe) + { + var args = "-nobrowser -data=\"" + AppDate + "\""; + _nzbDroneProcess = _processProvider.ShellExecute(outputNzbdroneConsoleExe, args, OnOutputDataReceived, OnOutputDataReceived); + + } + + private void OnOutputDataReceived(string data) + { + if (data.Contains("Press enter to exit")) + { + _nzbDroneProcess.StandardInput.WriteLine(" "); + } + } + + + public string AppDate { get; private set; } + } +} \ No newline at end of file