From 2d25c747581cb2a19869056232685a766d6e4d2c Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 2 Jun 2015 21:37:28 -0700 Subject: [PATCH] Fixed: Premature cleanup of completed tasks cache causes tasks to get stuck till reboot. --- .../Commands/CommandQueueManagerFixture.cs | 55 +++++++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../Messaging/Commands/CommandQueueManager.cs | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core.Test/Messaging/Commands/CommandQueueManagerFixture.cs diff --git a/src/NzbDrone.Core.Test/Messaging/Commands/CommandQueueManagerFixture.cs b/src/NzbDrone.Core.Test/Messaging/Commands/CommandQueueManagerFixture.cs new file mode 100644 index 000000000..16178a9cc --- /dev/null +++ b/src/NzbDrone.Core.Test/Messaging/Commands/CommandQueueManagerFixture.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Download; +using NzbDrone.Core.Messaging.Commands; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Messaging.Commands +{ + [TestFixture] + public class CommandQueueManagerFixture : CoreTest + { + [SetUp] + public void Setup() + { + var id = 0; + var commands = new List(); + + Mocker.GetMock() + .Setup(s => s.Insert(It.IsAny())) + .Returns(c => + { + c.Id = id + 1; + commands.Add(c); + id++; + + return c; + }); + + Mocker.GetMock() + .Setup(s => s.Get(It.IsAny())) + .Returns(c => + { + return commands.SingleOrDefault(e => e.Id == c); + }); + } + + [Test] + public void should_not_remove_commands_for_five_minutes_after_they_end() + { + var command = Subject.Push(new CheckForFinishedDownloadCommand()); + + Subject.Start(command); + Subject.Complete(command, "All done"); + Subject.CleanCommands(); + + Subject.Get(command.Id).Should().NotBeNull(); + + Mocker.GetMock() + .Verify(v => v.Get(It.IsAny()), Times.Never()); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 1328e6e35..44c4e9ffa 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -239,6 +239,7 @@ + diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs index 7f44827e3..42899e6ab 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs @@ -152,7 +152,7 @@ namespace NzbDrone.Core.Messaging.Commands _logger.Trace("Cleaning up old commands"); _repo.Trim(); - var old = _commandCache.Values.Where(c => c.EndedAt < DateTime.UtcNow.AddMinutes(5)); + var old = _commandCache.Values.Where(c => c.EndedAt < DateTime.UtcNow.AddMinutes(-5)); foreach (var command in old) {