From b25c772d52bcb5420649af4cbb1c85b1478bedbc Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 16 Nov 2017 11:58:34 -0500 Subject: [PATCH] Improved test timing for CommandExecutor. --- .../Commands/CommandExecutorFixture.cs | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs index e0b3d37c5..00b5366e5 100644 --- a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs +++ b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs @@ -35,6 +35,15 @@ namespace NzbDrone.Core.Test.Messaging.Commands .Returns(_executorB.Object); } + [TearDown] + public void TearDown() + { + Subject.Handle(new ApplicationShutdownRequested()); + + // Give the threads a bit of time to shut down. + Thread.Sleep(10); + } + private void GivenCommandQueue() { _commandQueue = new BlockingCollection(new CommandQueue()); @@ -44,8 +53,10 @@ namespace NzbDrone.Core.Test.Messaging.Commands .Returns(_commandQueue.GetConsumingEnumerable); } - private void WaitForExecution(CommandModel commandModel) + private void QueueAndWaitForExecution(CommandModel commandModel) { + Thread.Sleep(10); + Mocker.GetMock() .Setup(s => s.Complete(It.Is(c => c == commandModel), It.IsAny())) .Callback(() => _commandExecuted = true); @@ -54,12 +65,14 @@ namespace NzbDrone.Core.Test.Messaging.Commands .Setup(s => s.Fail(It.Is(c => c == commandModel), It.IsAny(), It.IsAny())) .Callback(() => _commandExecuted = true); + _commandQueue.Add(commandModel); + while (!_commandExecuted) { Thread.Sleep(100); } - var t1 = 1; + Thread.Sleep(10); } [Test] @@ -82,9 +95,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands }; Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); _executorA.Verify(c => c.Execute(commandA), Times.Once()); } @@ -100,9 +112,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands }; Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); _executorA.Verify(c => c.Execute(commandA), Times.Once()); _executorB.Verify(c => c.Execute(It.IsAny()), Times.Never()); @@ -122,9 +133,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands .Throws(new NotImplementedException()); Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); VerifyEventPublished(); ExceptionVerification.ExpectedErrors(1); @@ -141,9 +151,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands }; Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); VerifyEventPublished(); } @@ -159,9 +168,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands }; Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); Mocker.GetMock() .Setup(s => s.Complete(It.Is(c => c == commandModel), commandA.CompletionMessage)) @@ -180,9 +188,8 @@ namespace NzbDrone.Core.Test.Messaging.Commands }; Subject.Handle(new ApplicationStartedEvent()); - _commandQueue.Add(commandModel); - WaitForExecution(commandModel); + QueueAndWaitForExecution(commandModel); Mocker.GetMock() .Setup(s => s.Complete(It.Is(c => c == commandModel), commandModel.Message)) @@ -199,12 +206,6 @@ namespace NzbDrone.Core.Test.Messaging.Commands public class CommandB : Command { - - public CommandB() - { - - } - public override string CompletionMessage => null; }