2013-10-22 07:31:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2013-10-29 16:51:54 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-10-22 07:31:36 +00:00
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
|
using NzbDrone.Core.History;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Download
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2014-04-19 15:09:22 +00:00
|
|
|
|
public class FailedDownloadServiceFixture : CoreTest<DownloadTrackingService>
|
2013-10-22 07:31:36 +00:00
|
|
|
|
{
|
2014-04-19 15:09:22 +00:00
|
|
|
|
private List<DownloadClientItem> _completed;
|
|
|
|
|
private List<DownloadClientItem> _failed;
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2014-04-19 15:09:22 +00:00
|
|
|
|
_completed = Builder<DownloadClientItem>.CreateListOfSize(5)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.All()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.With(h => h.Status = DownloadItemStatus.Completed)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
_failed = Builder<DownloadClientItem>.CreateListOfSize(1)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.All()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.With(h => h.Status = DownloadItemStatus.Failed)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IProvideDownloadClient>()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.Setup(c => c.GetDownloadClients())
|
|
|
|
|
.Returns( new IDownloadClient[] { Mocker.GetMock<IDownloadClient>().Object });
|
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IDownloadClient>()
|
|
|
|
|
.SetupGet(c => c.Definition)
|
|
|
|
|
.Returns(new Core.Download.DownloadClientDefinition { Id = 1, Name = "testClient" });
|
2013-10-29 16:51:54 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IConfigService>()
|
|
|
|
|
.SetupGet(s => s.EnableFailedDownloadHandling)
|
|
|
|
|
.Returns(true);
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
|
|
|
|
.Setup(s => s.Imported())
|
|
|
|
|
.Returns(new List<History.History>());
|
|
|
|
|
|
|
|
|
|
Mocker.SetConstant<IFailedDownloadService>(Mocker.Resolve<FailedDownloadService>());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 05:24:26 +00:00
|
|
|
|
private void GivenNoGrabbedHistory()
|
2013-10-22 07:31:36 +00:00
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
2013-10-24 05:24:26 +00:00
|
|
|
|
.Setup(s => s.Grabbed())
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Returns(new List<History.History>());
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 05:24:26 +00:00
|
|
|
|
private void GivenGrabbedHistory(List<History.History> history)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
2013-10-24 05:24:26 +00:00
|
|
|
|
.Setup(s => s.Grabbed())
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Returns(history);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GivenNoFailedHistory()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
|
|
|
|
.Setup(s => s.Failed())
|
|
|
|
|
.Returns(new List<History.History>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GivenFailedHistory(List<History.History> failedHistory)
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
|
|
|
|
.Setup(s => s.Failed())
|
|
|
|
|
.Returns(failedHistory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GivenFailedDownloadClientHistory()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IDownloadClient>()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.Setup(s => s.GetItems())
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Returns(_failed);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 20:07:41 +00:00
|
|
|
|
private void GivenGracePeriod(int hours)
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IConfigService>().SetupGet(s => s.BlacklistGracePeriod).Returns(hours);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GivenRetryLimit(int count)
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IConfigService>().SetupGet(s => s.BlacklistRetryLimit).Returns(count);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 07:31:36 +00:00
|
|
|
|
private void VerifyNoFailedDownloads()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IEventAggregator>()
|
|
|
|
|
.Verify(v => v.PublishEvent(It.IsAny<DownloadFailedEvent>()), Times.Never());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyFailedDownloads(int count = 1)
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IEventAggregator>()
|
2013-10-24 05:24:26 +00:00
|
|
|
|
.Verify(v => v.PublishEvent(It.Is<DownloadFailedEvent>(d => d.EpisodeIds.Count == count)), Times.Once());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_no_download_client_history()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IDownloadClient>()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.Setup(s => s.GetItems())
|
|
|
|
|
.Returns(new List<DownloadClientItem>());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
|
|
|
|
.Verify(s => s.BetweenDates(It.IsAny<DateTime>(), It.IsAny<DateTime>(), HistoryEventType.Grabbed),
|
|
|
|
|
Times.Never());
|
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_no_failed_items_in_download_client_history()
|
|
|
|
|
{
|
2014-04-19 15:09:22 +00:00
|
|
|
|
GivenNoGrabbedHistory();
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
2013-10-22 07:31:36 +00:00
|
|
|
|
Mocker.GetMock<IDownloadClient>()
|
2014-04-19 15:09:22 +00:00
|
|
|
|
.Setup(s => s.GetItems())
|
2013-10-22 07:31:36 +00:00
|
|
|
|
.Returns(_completed);
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IHistoryService>()
|
|
|
|
|
.Verify(s => s.BetweenDates(It.IsAny<DateTime>(), It.IsAny<DateTime>(), HistoryEventType.Grabbed),
|
|
|
|
|
Times.Never());
|
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_matching_history_is_not_found()
|
|
|
|
|
{
|
2013-10-24 05:24:26 +00:00
|
|
|
|
GivenNoGrabbedHistory();
|
2013-10-22 07:31:36 +00:00
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 20:50:00 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_grabbed_history_contains_null_downloadclient_id()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", null);
|
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-02-24 20:50:00 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_process_if_failed_history_contains_null_downloadclient_id()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2014-02-24 20:50:00 +00:00
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
|
|
|
|
|
var historyFailed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyFailed.First().Data.Add("downloadClient", "SabnzbdClient");
|
|
|
|
|
historyFailed.First().Data.Add("downloadClientId", null);
|
|
|
|
|
|
|
|
|
|
GivenFailedHistory(historyFailed);
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-02-24 20:50:00 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 07:31:36 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_already_added_to_history_as_failed()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var history = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-10-24 05:24:26 +00:00
|
|
|
|
GivenGrabbedHistory(history);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
GivenFailedHistory(history);
|
|
|
|
|
|
|
|
|
|
history.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
history.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_process_if_not_already_in_failed_history()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var history = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-10-24 05:24:26 +00:00
|
|
|
|
GivenGrabbedHistory(history);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
|
|
|
|
history.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
history.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2013-10-24 05:24:26 +00:00
|
|
|
|
public void should_have_multiple_episode_ids_when_multi_episode_release_fails()
|
2013-10-22 07:31:36 +00:00
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var history = Builder<History.History>.CreateListOfSize(2)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-10-24 05:24:26 +00:00
|
|
|
|
GivenGrabbedHistory(history);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
|
|
|
|
history.ForEach(h =>
|
|
|
|
|
{
|
|
|
|
|
h.Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
h.Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads(2);
|
|
|
|
|
}
|
2013-10-29 16:51:54 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_skip_if_enable_failed_download_handling_is_off()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IConfigService>()
|
|
|
|
|
.SetupGet(s => s.EnableFailedDownloadHandling)
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2013-10-29 16:51:54 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
2014-03-17 05:32:39 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_failed_due_to_lack_of_disk_space()
|
|
|
|
|
{
|
|
|
|
|
var history = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(history);
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
_failed.First().Message = "Unpacking failed, write error or disk is full?";
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-03-17 05:32:39 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_process_if_ageHours_is_not_set()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_process_if_age_is_greater_than_grace_period()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("ageHours", "48");
|
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_process_if_retry_count_is_greater_than_grace_period()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("ageHours", "48");
|
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
GivenGracePeriod(6);
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
VerifyFailedDownloads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_process_if_age_is_less_than_grace_period()
|
|
|
|
|
{
|
|
|
|
|
GivenFailedDownloadClientHistory();
|
|
|
|
|
|
|
|
|
|
var historyGrabbed = Builder<History.History>.CreateListOfSize(1)
|
|
|
|
|
.Build()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
historyGrabbed.First().Data.Add("downloadClient", "SabnzbdClient");
|
2014-04-19 15:09:22 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("downloadClientId", _failed.First().DownloadClientId);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
historyGrabbed.First().Data.Add("ageHours", "1");
|
|
|
|
|
|
|
|
|
|
GivenGrabbedHistory(historyGrabbed);
|
|
|
|
|
GivenNoFailedHistory();
|
|
|
|
|
GivenGracePeriod(6);
|
|
|
|
|
GivenRetryLimit(1);
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Subject.Execute(new CheckForFinishedDownloadCommand());
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
VerifyNoFailedDownloads();
|
|
|
|
|
}
|
2013-10-22 07:31:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|