From 3c5e56919f1cbef39d9f8c6e66a4842fee097b27 Mon Sep 17 00:00:00 2001 From: ta264 Date: Mon, 17 Aug 2020 22:18:47 +0100 Subject: [PATCH] Run integration tests in parallel --- .../IntegrationTest.cs | 17 ++++++++--- src/NzbDrone.Test.Common/NzbDroneRunner.cs | 30 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Integration.Test/IntegrationTest.cs b/src/NzbDrone.Integration.Test/IntegrationTest.cs index 5ab23cb6c..b81470417 100644 --- a/src/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/src/NzbDrone.Integration.Test/IntegrationTest.cs @@ -1,24 +1,33 @@ +using System.Threading; using NLog; +using NUnit.Framework; using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Test.Common; using Radarr.Http.ClientSchema; namespace NzbDrone.Integration.Test { + [Parallelizable(ParallelScope.Fixtures)] public abstract class IntegrationTest : IntegrationTestBase { + protected static int StaticPort = 7878; + protected NzbDroneRunner _runner; public override string MovieRootFolder => GetTempDirectory("MovieRootFolder"); - protected override string RootUrl => "http://localhost:7878/"; + protected int Port { get; private set; } + + protected override string RootUrl => $"http://localhost:{Port}/"; protected override string ApiKey => _runner.ApiKey; protected override void StartTestTarget() { - _runner = new NzbDroneRunner(LogManager.GetCurrentClassLogger()); - _runner.KillAll(); + Port = Interlocked.Increment(ref StaticPort); + + _runner = new NzbDroneRunner(LogManager.GetCurrentClassLogger(), Port); + _runner.Kill(); _runner.Start(); } @@ -45,7 +54,7 @@ protected override void InitializeTestTarget() protected override void StopTestTarget() { - _runner.KillAll(); + _runner.Kill(); } } } diff --git a/src/NzbDrone.Test.Common/NzbDroneRunner.cs b/src/NzbDrone.Test.Common/NzbDroneRunner.cs index 2fc0f4cfb..54f8bb343 100644 --- a/src/NzbDrone.Test.Common/NzbDroneRunner.cs +++ b/src/NzbDrone.Test.Common/NzbDroneRunner.cs @@ -20,11 +20,14 @@ public class NzbDroneRunner public string AppData { get; private set; } public string ApiKey { get; private set; } + public int Port { get; private set; } public NzbDroneRunner(Logger logger, int port = 7878) { _processProvider = new ProcessProvider(logger); - _restClient = new RestClient("http://localhost:7878/api/v3"); + _restClient = new RestClient($"http://localhost:{port}/api/v3"); + + Port = port; } public void Start() @@ -75,7 +78,7 @@ public void Start() if (statusCall.ResponseStatus == ResponseStatus.Completed) { - TestContext.Progress.WriteLine("Radarr is started. Running Tests"); + TestContext.Progress.WriteLine($"Radarr {Port} is started. Running Tests"); return; } @@ -85,6 +88,23 @@ public void Start() } } + public void Kill() + { + try + { + if (_nzbDroneProcess != null) + { + _processProvider.Kill(_nzbDroneProcess.Id); + } + } + catch (InvalidOperationException) + { + // May happen if the process closes while being closed + } + + TestBase.DeleteTempFolder(AppData); + } + public void KillAll() { try @@ -107,13 +127,14 @@ public void KillAll() private void Start(string outputRadarrConsoleExe) { + TestContext.Progress.WriteLine("Starting instance from {0} on port {1}", outputRadarrConsoleExe, Port); var args = "-nobrowser -data=\"" + AppData + "\""; _nzbDroneProcess = _processProvider.Start(outputRadarrConsoleExe, args, null, OnOutputDataReceived, OnOutputDataReceived); } private void OnOutputDataReceived(string data) { - TestContext.Progress.WriteLine(data); + TestContext.Progress.WriteLine($" [{Port}] > " + data); if (data.Contains("Press enter to exit")) { @@ -132,7 +153,8 @@ private void GenerateConfigFile() new XDeclaration("1.0", "utf-8", "yes"), new XElement(ConfigFileProvider.CONFIG_ELEMENT_NAME, new XElement(nameof(ConfigFileProvider.ApiKey), apiKey), - new XElement(nameof(ConfigFileProvider.AnalyticsEnabled), false))); + new XElement(nameof(ConfigFileProvider.AnalyticsEnabled), false), + new XElement(nameof(ConfigFileProvider.Port), Port))); var data = xDoc.ToString();