From 679c0599dd0bfab95f2d6b0400127e931b3bf578 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 22 Aug 2019 22:20:39 +0200 Subject: [PATCH] Flaky CommandExecutorFixture tests --- .../Commands/CommandExecutorFixture.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs index 33b61c523..fb79b04eb 100644 --- a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs +++ b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs @@ -18,7 +18,6 @@ namespace NzbDrone.Core.Test.Messaging.Commands private CommandQueue _commandQueue; private Mock> _executorA; private Mock> _executorB; - private bool _commandExecuted = false; [SetUp] public void Setup() @@ -53,26 +52,34 @@ namespace NzbDrone.Core.Test.Messaging.Commands .Returns(_commandQueue.GetConsumingEnumerable); } - private void QueueAndWaitForExecution(CommandModel commandModel) + private void QueueAndWaitForExecution(CommandModel commandModel, bool waitPublish = false) { - Thread.Sleep(10); + var waitEventComplete = new ManualResetEventSlim(); + var waitEventPublish = new ManualResetEventSlim(); Mocker.GetMock() .Setup(s => s.Complete(It.Is(c => c == commandModel), It.IsAny())) - .Callback(() => _commandExecuted = true); + .Callback(() => waitEventComplete.Set()); Mocker.GetMock() .Setup(s => s.Fail(It.Is(c => c == commandModel), It.IsAny(), It.IsAny())) - .Callback(() => _commandExecuted = true); + .Callback(() => waitEventComplete.Set()); + + Mocker.GetMock() + .Setup(s => s.PublishEvent(It.IsAny())) + .Callback(() => waitEventPublish.Set()); _commandQueue.Add(commandModel); - while (!_commandExecuted) + if (!waitEventComplete.Wait(2000)) { - Thread.Sleep(100); + Assert.Fail("Command did not Complete/Fail within 2 sec"); } - Thread.Sleep(10); + if (waitPublish && !waitEventPublish.Wait(500)) + { + Assert.Fail("Command did not Publish within 500 msec"); + } } [Test] @@ -138,8 +145,6 @@ namespace NzbDrone.Core.Test.Messaging.Commands VerifyEventPublished(); - Thread.Sleep(10); - ExceptionVerification.ExpectedErrors(1); } @@ -175,18 +180,17 @@ namespace NzbDrone.Core.Test.Messaging.Commands QueueAndWaitForExecution(commandModel); Mocker.GetMock() - .Setup(s => s.Complete(It.Is(c => c == commandModel), commandA.CompletionMessage)) - .Callback(() => _commandExecuted = true); + .Verify(s => s.Complete(It.Is(c => c == commandModel), commandA.CompletionMessage), Times.Once()); } [Test] public void should_use_last_progress_message_if_completion_message_is_null() { GivenCommandQueue(); - var commandA = new CommandA(); + var commandB = new CommandB(); var commandModel = new CommandModel { - Body = commandA, + Body = commandB, Message = "Do work" }; @@ -195,8 +199,7 @@ namespace NzbDrone.Core.Test.Messaging.Commands QueueAndWaitForExecution(commandModel); Mocker.GetMock() - .Setup(s => s.Complete(It.Is(c => c == commandModel), commandModel.Message)) - .Callback(() => _commandExecuted = true); + .Verify(s => s.Complete(It.Is(c => c == commandModel), commandModel.Message), Times.Once()); } }