From 4786bc0c34f29f16032e84791982e38f308fb0a6 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 24 Feb 2014 21:50:00 +0100 Subject: [PATCH] Failed Download detection now ignores history without unique download client id. --- src/NzbDrone.Common/DictionaryExtensions.cs | 16 +++++++ src/NzbDrone.Common/NzbDrone.Common.csproj | 1 + .../Download/FailedDownloadServiceFixture.cs | 48 +++++++++++++++++++ .../Download/FailedDownloadService.cs | 17 +++---- 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 src/NzbDrone.Common/DictionaryExtensions.cs diff --git a/src/NzbDrone.Common/DictionaryExtensions.cs b/src/NzbDrone.Common/DictionaryExtensions.cs new file mode 100644 index 000000000..da0f4786f --- /dev/null +++ b/src/NzbDrone.Common/DictionaryExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Common +{ + public static class DictionaryExtensions + { + public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue = default(TValue)) + { + TValue value; + return dictionary.TryGetValue(key, out value) ? value : defaultValue; + } + } +} diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index 00e5276e6..6ee9aeeb4 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -67,6 +67,7 @@ + diff --git a/src/NzbDrone.Core.Test/Download/FailedDownloadServiceFixture.cs b/src/NzbDrone.Core.Test/Download/FailedDownloadServiceFixture.cs index 6e0293182..bb91060ae 100644 --- a/src/NzbDrone.Core.Test/Download/FailedDownloadServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Download/FailedDownloadServiceFixture.cs @@ -131,6 +131,54 @@ public void should_not_process_if_matching_history_is_not_found() VerifyNoFailedDownloads(); } + [Test] + public void should_not_process_if_grabbed_history_contains_null_downloadclient_id() + { + GivenFailedDownloadClientHistory(); + + var historyGrabbed = Builder.CreateListOfSize(1) + .Build() + .ToList(); + + historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient"); + historyGrabbed.First().Data.Add("downloadClientId", null); + + GivenGrabbedHistory(historyGrabbed); + GivenNoFailedHistory(); + + Subject.Execute(new CheckForFailedDownloadCommand()); + + VerifyNoFailedDownloads(); + } + + [Test] + public void should_process_if_failed_history_contains_null_downloadclient_id() + { + GivenFailedDownloadClientHistory(); + + var historyGrabbed = Builder.CreateListOfSize(1) + .Build() + .ToList(); + + historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient"); + historyGrabbed.First().Data.Add("downloadClientId", _failed.First().Id); + + GivenGrabbedHistory(historyGrabbed); + + var historyFailed = Builder.CreateListOfSize(1) + .Build() + .ToList(); + + historyFailed.First().Data.Add("downloadClient", "SabnzbdClient"); + historyFailed.First().Data.Add("downloadClientId", null); + + GivenFailedHistory(historyFailed); + + Subject.Execute(new CheckForFailedDownloadCommand()); + + VerifyFailedDownloads(); + } + [Test] public void should_not_process_if_already_added_to_history_as_failed() { diff --git a/src/NzbDrone.Core/Download/FailedDownloadService.cs b/src/NzbDrone.Core/Download/FailedDownloadService.cs index 373ddc567..f5438dea5 100644 --- a/src/NzbDrone.Core/Download/FailedDownloadService.cs +++ b/src/NzbDrone.Core/Download/FailedDownloadService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common; using NzbDrone.Core.Configuration; using NzbDrone.Core.History; using NzbDrone.Core.Messaging.Commands; @@ -72,8 +73,7 @@ private void CheckQueue(List grabbedHistory, List h.Data.ContainsKey(DOWNLOAD_CLIENT_ID) && - h.Data[DOWNLOAD_CLIENT_ID].Equals(failedLocal.Id))) + if (failedHistory.Any(h => failedLocal.Id.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID)))) { _logger.Trace("Already added to history as failed"); continue; @@ -118,8 +118,7 @@ private void CheckHistory(List grabbedHistory, List h.Data.ContainsKey(DOWNLOAD_CLIENT_ID) && - h.Data[DOWNLOAD_CLIENT_ID].Equals(failedLocal.Id))) + if (failedHistory.Any(h => failedLocal.Id.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID)))) { _logger.Trace("Already added to history as failed"); continue; @@ -137,8 +136,7 @@ private void CheckHistory(List grabbedHistory, List GetHistoryItems(List grabbedHistory, string downloadClientId) { - return grabbedHistory.Where(h => h.Data.ContainsKey(DOWNLOAD_CLIENT_ID) && - h.Data[DOWNLOAD_CLIENT_ID].Equals(downloadClientId)) + return grabbedHistory.Where(h => downloadClientId.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID))) .ToList(); } @@ -148,17 +146,14 @@ private void PublishDownloadFailedEvent(List historyItems, stri string downloadClient; string downloadClientId; - historyItem.Data.TryGetValue(DOWNLOAD_CLIENT, out downloadClient); - historyItem.Data.TryGetValue(DOWNLOAD_CLIENT_ID, out downloadClientId); - _eventAggregator.PublishEvent(new DownloadFailedEvent { SeriesId = historyItem.SeriesId, EpisodeIds = historyItems.Select(h => h.EpisodeId).ToList(), Quality = historyItem.Quality, SourceTitle = historyItem.SourceTitle, - DownloadClient = downloadClient, - DownloadClientId = downloadClientId, + DownloadClient = historyItem.Data.GetValueOrDefault(DOWNLOAD_CLIENT), + DownloadClientId = historyItem.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID), Message = message }); }