From c19a393f99c56b916c94993312d5dbe54f7f8c04 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 26 Dec 2014 14:55:35 +0100 Subject: [PATCH] Now generating unique ids for queue items sent to the api. --- src/NzbDrone.Common/Crypto/HashConverter.cs | 20 ++++++ src/NzbDrone.Common/NzbDrone.Common.csproj | 1 + .../NzbDrone.Core.Test.csproj | 1 + .../QueueTests/QueueServiceFixture.cs | 66 +++++++++++++++++++ .../Download/Pending/PendingReleaseService.cs | 3 +- src/NzbDrone.Core/Queue/QueueService.cs | 7 +- 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Common/Crypto/HashConverter.cs create mode 100644 src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs diff --git a/src/NzbDrone.Common/Crypto/HashConverter.cs b/src/NzbDrone.Common/Crypto/HashConverter.cs new file mode 100644 index 000000000..3bd521f16 --- /dev/null +++ b/src/NzbDrone.Common/Crypto/HashConverter.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace NzbDrone.Common.Crypto +{ + public static class HashConverter + { + private static SHA1 HashAlgorithm = SHA1.Create(); + + public static int GetHashInt31(string target) + { + var hash = HashAlgorithm.ComputeHash(Encoding.Default.GetBytes(target)); + + return BitConverter.ToInt32(hash, 0) & 0x7fffffff; + } + } +} diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index 246b23919..d7966efc0 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -132,6 +132,7 @@ + diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 046d8238b..1074dbc46 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -236,6 +236,7 @@ + diff --git a/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs b/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs new file mode 100644 index 000000000..069ef9f2c --- /dev/null +++ b/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using NzbDrone.Core.Download.TrackedDownloads; +using NzbDrone.Core.Queue; +using NzbDrone.Core.Test.Framework; +using FizzWare.NBuilder; +using FluentAssertions; +using NzbDrone.Core.Tv; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.Test.QueueTests +{ + [TestFixture] + public class QueueServiceFixture : CoreTest + { + private List _trackedDownloads; + + [SetUp] + public void SetUp() + { + var downloadItem = Builder.CreateNew() + .With(v => v.RemainingTime = TimeSpan.FromSeconds(10)) + .Build(); + + var series = Builder.CreateNew() + .Build(); + + var episodes = Builder.CreateListOfSize(3) + .All() + .With(e => e.SeriesId = series.Id) + .Build(); + + var remoteEpisode = Builder.CreateNew() + .With(r => r.Series = series) + .With(r => r.Episodes = new List(episodes)) + .With(r => r.ParsedEpisodeInfo = new ParsedEpisodeInfo()) + .Build(); + + _trackedDownloads = Builder.CreateListOfSize(1) + .All() + .With(v => v.DownloadItem = downloadItem) + .With(v => v.RemoteEpisode = remoteEpisode) + .Build() + .ToList(); + } + + [Test] + public void queue_items_should_have_id() + { + Subject.Handle(new TrackedDownloadRefreshedEvent(_trackedDownloads)); + + var queue = Subject.GetQueue(); + + queue.Should().HaveCount(3); + + queue.All(v => v.Id > 0).Should().BeTrue(); + + var distinct = queue.Select(v => v.Id).Distinct().ToArray(); + + distinct.Should().HaveCount(3); + } + } +} diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs index c879f1b23..8108b1c98 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Crypto; using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.DecisionEngine; @@ -101,7 +102,7 @@ public List GetPendingRemoteEpisodes(int seriesId) var queue = new Queue.Queue { - Id = episode.Id ^ (pendingRelease.Id << 16), + Id = HashConverter.GetHashInt31(string.Format("pending-{0}-ep{1}", pendingRelease.Id, episode.Id)), Series = pendingRelease.RemoteEpisode.Series, Episode = episode, Quality = pendingRelease.RemoteEpisode.ParsedEpisodeInfo.Quality, diff --git a/src/NzbDrone.Core/Queue/QueueService.cs b/src/NzbDrone.Core/Queue/QueueService.cs index 525c5d66e..4ee499f9d 100644 --- a/src/NzbDrone.Core/Queue/QueueService.cs +++ b/src/NzbDrone.Core/Queue/QueueService.cs @@ -1,6 +1,7 @@ using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; +using NzbDrone.Common.Crypto; using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.Messaging.Events; @@ -40,13 +41,13 @@ public void Handle(TrackedDownloadRefreshedEvent message) _eventAggregator.PublishEvent(new QueueUpdatedEvent()); } - private static IEnumerable MapQueue(TrackedDownload trackedDownload) + private IEnumerable MapQueue(TrackedDownload trackedDownload) { foreach (var episode in trackedDownload.RemoteEpisode.Episodes) { var queue = new Queue { - Id = episode.Id ^ (trackedDownload.DownloadItem.DownloadId.GetHashCode() << 16), + Id = HashConverter.GetHashInt31(string.Format("trackedDownload-{0}-ep{1}", trackedDownload.DownloadItem.DownloadId, episode.Id)), Series = trackedDownload.RemoteEpisode.Series, Episode = episode, Quality = trackedDownload.RemoteEpisode.ParsedEpisodeInfo.Quality,