From aacb8970f869c72edf18f5ce0e2194dec97466e6 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 12 Sep 2019 14:44:22 +0200 Subject: [PATCH] Fixed several failing/flaky mono unit tests --- .../DiskTests/FreeSpaceFixtureBase.cs | 34 ------------------- .../ProcessProviderFixture.cs | 7 +++- src/NzbDrone.Common/Composition/Container.cs | 1 - .../Commands/CommandExecutorFixture.cs | 8 ++--- src/NzbDrone.Host.Test/ContainerFixture.cs | 14 ++++---- .../DiskProviderTests/FreeSpaceFixture.cs | 22 +++++++++++- .../MonoPlatformInfoFixture.cs | 2 +- src/NzbDrone.Test.Common/LoggingTest.cs | 2 +- .../DiskProviderTests/FreeSpaceFixture.cs | 27 ++++++++++++++- 9 files changed, 66 insertions(+), 51 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 3d872bf78..e27ca5486 100644 --- a/src/NzbDrone.Common.Test/ProcessProviderFixture.cs +++ b/src/NzbDrone.Common.Test/ProcessProviderFixture.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading; using FluentAssertions; using NUnit.Framework; using NzbDrone.Common.Model; @@ -68,8 +69,10 @@ namespace NzbDrone.Common.Test { var process = StartDummyProcess(); + Thread.Sleep(500); + Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should() - .BeTrue("excepted one dummy process to be already running"); + .BeTrue("one running dummy process"); process.Kill(); process.WaitForExit(); @@ -133,6 +136,8 @@ namespace NzbDrone.Common.Test var dummy1 = StartDummyProcess(); var dummy2 = StartDummyProcess(); + Thread.Sleep(500); + Subject.KillAll(DummyApp.DUMMY_PROCCESS_NAME); dummy1.HasExited.Should().BeTrue(); 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 45e015950..28ef04eb9 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 1064d1c5b..0179b5323 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 5c2e67b4d..7ba5d8414 100644 --- a/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -1,4 +1,9 @@ -using NUnit.Framework; +using System.Collections.Generic; +using System.IO; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.Disk; using NzbDrone.Common.Test.DiskTests; using NzbDrone.Mono.Disk; @@ -12,5 +17,20 @@ namespace NzbDrone.Mono.Test.DiskProviderTests { MonoOnly(); } + + [SetUp] + public void Setup() + { + Mocker.SetConstant(new ProcMountProvider(TestLogger)); + Mocker.GetMock() + .Setup(v => v.GetCompleteRealPath(It.IsAny())) + .Returns(s => s); + } + + [Test] + public void should_be_able_to_check_space_on_ramdrive() + { + Subject.GetAvailableSpace("/run/").Should().NotBe(0); + } } } diff --git a/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs b/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs index 431dab769..7f2fb5f32 100644 --- a/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs +++ b/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo [Test] public void should_get_framework_version() { - Subject.Version.Major.Should().BeOneOf(4, 5); + Subject.Version.Major.Should().BeOneOf(4, 5, 6); if (Subject.Version.Major == 4) { Subject.Version.Minor.Should().BeOneOf(0, 5, 6); diff --git a/src/NzbDrone.Test.Common/LoggingTest.cs b/src/NzbDrone.Test.Common/LoggingTest.cs index 1a1f9e3ff..b69d50986 100644 --- a/src/NzbDrone.Test.Common/LoggingTest.cs +++ b/src/NzbDrone.Test.Common/LoggingTest.cs @@ -46,7 +46,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); + } } }