From 7ffecf6c50be66fdc210c31d10471382c091e3a3 Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 13 Sep 2019 21:24:15 -0400 Subject: [PATCH] Fixed: several failing/flaky mono unit tests Co-Authored-By: taloth --- .../DiskTests/FreeSpaceFixtureBase.cs | 34 ------------------- .../ProcessProviderFixture.cs | 33 +++++------------- src/NzbDrone.Common/Composition/Container.cs | 1 - .../Commands/CommandExecutorFixture.cs | 8 ++--- src/NzbDrone.Host.Test/ContainerFixture.cs | 14 ++++---- .../DiskProviderTests/FreeSpaceFixture.cs | 18 +++++++--- src/NzbDrone.Test.Common/LoggingTest.cs | 2 +- .../DiskProviderTests/FreeSpaceFixture.cs | 27 ++++++++++++++- 8 files changed, 61 insertions(+), 76 deletions(-) diff --git a/src/NzbDrone.Common.Test/DiskTests/FreeSpaceFixtureBase.cs b/src/NzbDrone.Common.Test/DiskTests/FreeSpaceFixtureBase.cs index a4dbe737b..bf6d8ec27 100644 --- a/src/NzbDrone.Common.Test/DiskTests/FreeSpaceFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskTests/FreeSpaceFixtureBase.cs @@ -25,13 +25,6 @@ namespace NzbDrone.Common.Test.DiskTests Subject.GetAvailableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0); } - [Test] - public void should_be_able_to_check_space_on_ramdrive() - { - MonoOnly(); - Subject.GetAvailableSpace("/run/").Should().NotBe(0); - } - [Test] public void should_return_free_disk_space() { @@ -39,33 +32,6 @@ namespace NzbDrone.Common.Test.DiskTests result.Should().BeGreaterThan(0); } - [Test] - public void should_be_able_to_get_space_on_unc() - { - WindowsOnly(); - - var result = Subject.GetAvailableSpace(@"\\localhost\c$\Windows"); - result.Should().BeGreaterThan(0); - } - - [Test] - public void should_throw_if_drive_doesnt_exist() - { - WindowsOnly(); - - // Find a drive that doesn't exist. - for (char driveletter = 'Z'; driveletter > 'D' ; driveletter--) - { - if (new DriveInfo(driveletter.ToString()).IsReady) - continue; - - Assert.Throws(() => Subject.GetAvailableSpace(driveletter + @":\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic())); - return; - } - - Assert.Inconclusive("No drive available for testing."); - } - [Test] public void should_be_able_to_get_space_on_folder_that_doesnt_exist() { diff --git a/src/NzbDrone.Common.Test/ProcessProviderFixture.cs b/src/NzbDrone.Common.Test/ProcessProviderFixture.cs index 7f4e5b7a9..471c693fd 100644 --- a/src/NzbDrone.Common.Test/ProcessProviderFixture.cs +++ b/src/NzbDrone.Common.Test/ProcessProviderFixture.cs @@ -16,6 +16,7 @@ namespace NzbDrone.Common.Test // We don't want one tests setup killing processes used in another [NonParallelizable] [TestFixture] + [Platform(Exclude = "MacOsX")] public class ProcessProviderFixture : TestBase { @@ -71,20 +72,18 @@ namespace NzbDrone.Common.Test { var process = StartDummyProcess(); - var check = Subject.GetProcessById(process.Id); - check.Should().NotBeNull(); + Thread.Sleep(500); - process.Refresh(); - process.HasExited.Should().BeFalse(); + Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should() + .BeTrue("one running dummy process"); process.Kill(); process.WaitForExit(); - process.HasExited.Should().BeTrue(); + + Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should().BeFalse(); } [Test] - [Platform(Exclude="MacOsX")] - [Retry(3)] public void exists_should_find_running_process() { var process = StartDummyProcess(); @@ -100,12 +99,13 @@ namespace NzbDrone.Common.Test [Test] - [Platform(Exclude="MacOsX")] public void kill_all_should_kill_all_process_with_name() { var dummy1 = StartDummyProcess(); var dummy2 = StartDummyProcess(); + Thread.Sleep(500); + Subject.KillAll(DummyApp.DUMMY_PROCCESS_NAME); dummy1.HasExited.Should().BeTrue(); @@ -114,26 +114,11 @@ namespace NzbDrone.Common.Test private Process StartDummyProcess() { - var processStarted = new ManualResetEventSlim(); - var path = Path.Combine(TestContext.CurrentContext.TestDirectory, DummyApp.DUMMY_PROCCESS_NAME + ".exe"); - var process = Subject.Start(path, onOutputDataReceived: (string data) => { - if (data.StartsWith("Dummy process. ID:")) - { - processStarted.Set(); - } - }); - - if (!processStarted.Wait(2000)) - { - Assert.Fail("Failed to start process within 2 sec"); - } - - return process; + return Subject.Start(path); } [Test] - [Retry(3)] public void ToString_on_new_processInfo() { Console.WriteLine(new ProcessInfo().ToString()); diff --git a/src/NzbDrone.Common/Composition/Container.cs b/src/NzbDrone.Common/Composition/Container.cs index 55a56bee2..a52ad0dc7 100644 --- a/src/NzbDrone.Common/Composition/Container.cs +++ b/src/NzbDrone.Common/Composition/Container.cs @@ -54,7 +54,6 @@ namespace NzbDrone.Common.Composition var factory = CreateSingletonImplementationFactory(implementation); _container.Register(service, factory); - _container.Register(service, factory, implementation.FullName); } public IEnumerable ResolveAll() where T : class diff --git a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs index 733e00240..bb1e6a733 100644 --- a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs +++ b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs @@ -71,9 +71,9 @@ namespace NzbDrone.Core.Test.Messaging.Commands _commandQueue.Add(commandModel); - if (!waitEventComplete.Wait(2000)) + if (!waitEventComplete.Wait(15000)) { - Assert.Fail("Command did not Complete/Fail within 2 sec"); + Assert.Fail("Command did not Complete/Fail within 15 sec"); } if (waitPublish && !waitEventPublish.Wait(500)) @@ -141,7 +141,7 @@ namespace NzbDrone.Core.Test.Messaging.Commands Subject.Handle(new ApplicationStartedEvent()); - QueueAndWaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel, true); VerifyEventPublished(); @@ -160,7 +160,7 @@ namespace NzbDrone.Core.Test.Messaging.Commands Subject.Handle(new ApplicationStartedEvent()); - QueueAndWaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel, true); VerifyEventPublished(); } diff --git a/src/NzbDrone.Host.Test/ContainerFixture.cs b/src/NzbDrone.Host.Test/ContainerFixture.cs index 0cd54bd0d..82d2aa23f 100644 --- a/src/NzbDrone.Host.Test/ContainerFixture.cs +++ b/src/NzbDrone.Host.Test/ContainerFixture.cs @@ -65,19 +65,19 @@ namespace NzbDrone.App.Test } [Test] - public void should_return_same_instance_of_singletons() + public void should_return_same_instance_via_resolve_and_resolveall() { - var first = _container.ResolveAll>().OfType().Single(); - var second = _container.ResolveAll>().OfType().Single(); + var first = (DownloadMonitoringService)_container.Resolve>(); + var second = _container.ResolveAll>().OfType().Single(); first.Should().BeSameAs(second); } [Test] - public void should_return_same_instance_of_singletons_by_different_same_interface() + public void should_return_same_instance_of_singletons_by_same_interface() { - var first = _container.ResolveAll>().OfType().Single(); - var second = _container.ResolveAll>().OfType().Single(); + var first = _container.ResolveAll>().OfType().Single(); + var second = _container.ResolveAll>().OfType().Single(); first.Should().BeSameAs(second); } @@ -85,7 +85,7 @@ namespace NzbDrone.App.Test [Test] public void should_return_same_instance_of_singletons_by_different_interfaces() { - var first = _container.ResolveAll>().OfType().Single(); + var first = _container.ResolveAll>().OfType().Single(); var second = (DownloadMonitoringService)_container.Resolve>(); first.Should().BeSameAs(second); diff --git a/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs b/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs index 1f61f6c65..7ba5d8414 100644 --- a/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.IO; +using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; @@ -11,16 +13,24 @@ namespace NzbDrone.Mono.Test.DiskProviderTests [Platform("Mono")] public class FreeSpaceFixture : FreeSpaceFixtureBase { + public FreeSpaceFixture() + { + MonoOnly(); + } + [SetUp] public void Setup() { - Mocker.SetConstant(Mocker.Resolve()); - Mocker.SetConstant(Mocker.Resolve()); + Mocker.SetConstant(new ProcMountProvider(TestLogger)); + Mocker.GetMock() + .Setup(v => v.GetCompleteRealPath(It.IsAny())) + .Returns(s => s); } - public FreeSpaceFixture() + [Test] + public void should_be_able_to_check_space_on_ramdrive() { - MonoOnly(); + Subject.GetAvailableSpace("/run/").Should().NotBe(0); } } } diff --git a/src/NzbDrone.Test.Common/LoggingTest.cs b/src/NzbDrone.Test.Common/LoggingTest.cs index b2d783761..26f0a915d 100644 --- a/src/NzbDrone.Test.Common/LoggingTest.cs +++ b/src/NzbDrone.Test.Common/LoggingTest.cs @@ -44,7 +44,7 @@ namespace NzbDrone.Test.Common private static void RegisterConsoleLogger() { - var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" }; + var consoleTarget = new ConsoleTarget { Layout = "${date:format=HH\\:mm\\:ss.f} ${level}: ${message} ${exception}" }; LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget); LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget)); } diff --git a/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs b/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs index c68de2451..633877880 100644 --- a/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -1,4 +1,6 @@ -using NUnit.Framework; +using System.IO; +using FluentAssertions; +using NUnit.Framework; using NzbDrone.Common.Test.DiskTests; using NzbDrone.Windows.Disk; @@ -12,5 +14,28 @@ namespace NzbDrone.Windows.Test.DiskProviderTests { WindowsOnly(); } + + [Test] + public void should_throw_if_drive_doesnt_exist() + { + // Find a drive that doesn't exist. + for (char driveletter = 'Z'; driveletter > 'D'; driveletter--) + { + if (new DriveInfo(driveletter.ToString()).IsReady) + continue; + + Assert.Throws(() => Subject.GetAvailableSpace(driveletter + @":\NOT_A_REAL_PATH\DOES_NOT_EXIST")); + return; + } + + Assert.Inconclusive("No drive available for testing."); + } + + [Test] + public void should_be_able_to_get_space_on_unc() + { + var result = Subject.GetAvailableSpace(@"\\localhost\c$\Windows"); + result.Should().BeGreaterThan(0); + } } }