Merge branch 'blackhole-delay' into develop

This commit is contained in:
Taloth Saldono
2016-03-15 21:47:18 +01:00
16 changed files with 416 additions and 160 deletions
@@ -0,0 +1,94 @@
using System;
using System.IO;
using System.Linq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Core.Download;
using NzbDrone.Test.Common;
using System.Threading;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Download.Clients.Blackhole;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
{
[TestFixture]
public class ScanWatchFolderFixture : CoreTest<ScanWatchFolder>
{
protected readonly string _title = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE";
protected string _completedDownloadFolder = @"c:\blackhole\completed".AsOsAgnostic();
protected void GivenCompletedItem()
{
var targetDir = Path.Combine(_completedDownloadFolder, _title);
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetDirectories(_completedDownloadFolder))
.Returns(new[] { targetDir });
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFiles(targetDir, SearchOption.AllDirectories))
.Returns(new[] { Path.Combine(targetDir, "somefile.mkv") });
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFileSize(It.IsAny<string>()))
.Returns(1000000);
}
protected void GivenChangedItem()
{
var currentSize = Mocker.GetMock<IDiskProvider>().Object.GetFileSize("abc");
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFileSize(It.IsAny<string>()))
.Returns(currentSize + 1);
}
private void VerifySingleItem(DownloadItemStatus status)
{
var items = Subject.GetItems(_completedDownloadFolder, TimeSpan.FromMilliseconds(50)).ToList();
items.Count.Should().Be(1);
items.First().Status.Should().Be(status);
}
[Test]
public void GetItems_should_considered_locked_files_queued()
{
GivenCompletedItem();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.IsFileLocked(It.IsAny<string>()))
.Returns(true);
Thread.Sleep(60);
VerifySingleItem(DownloadItemStatus.Downloading);
}
[Test]
public void GetItems_should_considered_changing_files_queued()
{
GivenCompletedItem();
VerifySingleItem(DownloadItemStatus.Downloading);
// If we keep changing the file every 20ms we should stay Downloading.
for (int i = 0; i < 10; i++)
{
TestLogger.Info("Iteration {0}", i);
GivenChangedItem();
VerifySingleItem(DownloadItemStatus.Downloading);
Thread.Sleep(10);
}
// Until it remains unchanged for >=50ms.
Thread.Sleep(60);
VerifySingleItem(DownloadItemStatus.Completed);
}
}
}
@@ -8,7 +8,7 @@ using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.TorrentBlackhole;
using NzbDrone.Core.Download.Clients.Blackhole;
using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model;
@@ -30,6 +30,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
_blackholeFolder = @"c:\blackhole\torrent".AsOsAgnostic();
_filePath = (@"c:\blackhole\torrent\" + _title + ".torrent").AsOsAgnostic();
Mocker.SetConstant<IScanWatchFolder>(Mocker.Resolve<ScanWatchFolder>());
Subject.Definition = new DownloadClientDefinition();
Subject.Definition.Settings = new TorrentBlackholeSettings
{
@@ -56,13 +58,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
protected void GivenCompletedItem()
{
var targetDir = Path.Combine(_completedDownloadFolder, _title);
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetDirectories(_completedDownloadFolder))
.Returns(new[] { targetDir });
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFiles(targetDir, SearchOption.AllDirectories))
.Returns(new[] { Path.Combine(_completedDownloadFolder, "somefile.mkv") });
.Returns(new[] { Path.Combine(targetDir, "somefile.mkv") });
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFileSize(It.IsAny<string>()))
@@ -87,6 +90,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
[Test]
public void completed_download_should_have_required_properties()
{
Subject.ScanGracePeriod = TimeSpan.Zero;
GivenCompletedItem();
var result = Subject.GetItems().Single();
@@ -94,6 +99,16 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
VerifyCompleted(result);
}
[Test]
public void partial_download_should_have_required_properties()
{
GivenCompletedItem();
var result = Subject.GetItems().Single();
VerifyPostprocessing(result);
}
[Test]
public void should_return_category()
{
@@ -142,21 +157,6 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Assert.Throws<ReleaseDownloadException>(() => Subject.Download(remoteEpisode));
}
[Test]
public void GetItems_should_considered_locked_files_queued()
{
GivenCompletedItem();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.IsFileLocked(It.IsAny<string>()))
.Returns(true);
var items = Subject.GetItems().ToList();
items.Count.Should().Be(1);
items.First().Status.Should().Be(DownloadItemStatus.Downloading);
}
[Test]
public void RemoveItem_should_delete_file()
{
@@ -9,7 +9,7 @@ using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.UsenetBlackhole;
using NzbDrone.Core.Download.Clients.Blackhole;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
@@ -29,6 +29,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
_blackholeFolder = @"c:\blackhole\nzb".AsOsAgnostic();
_filePath = (@"c:\blackhole\nzb\" + _title + ".nzb").AsOsAgnostic();
Mocker.SetConstant<IScanWatchFolder>(Mocker.Resolve<ScanWatchFolder>());
Subject.Definition = new DownloadClientDefinition();
Subject.Definition.Settings = new UsenetBlackholeSettings
{
@@ -58,7 +60,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFiles(targetDir, SearchOption.AllDirectories))
.Returns(new[] { Path.Combine(_completedDownloadFolder, "somefile.mkv") });
.Returns(new[] { Path.Combine(targetDir, "somefile.mkv") });
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFileSize(It.IsAny<string>()))
@@ -68,6 +70,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
[Test]
public void completed_download_should_have_required_properties()
{
Subject.ScanGracePeriod = TimeSpan.Zero;
GivenCompletedItem();
var result = Subject.GetItems().Single();
@@ -75,6 +79,17 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
VerifyCompleted(result);
}
[Test]
public void partial_download_should_have_required_properties()
{
GivenCompletedItem();
var result = Subject.GetItems().Single();
VerifyPostprocessing(result);
}
[Test]
public void should_return_category()
{
@@ -114,20 +129,6 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
}
[Test]
public void GetItems_should_considered_locked_files_downloading()
{
GivenCompletedItem();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.IsFileLocked(It.IsAny<string>()))
.Returns(true);
var result = Subject.GetItems().Single();
result.Status.Should().Be(DownloadItemStatus.Downloading);
}
[Test]
public void RemoveItem_should_delete_file()
{
@@ -96,6 +96,15 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
downloadClientItem.Status.Should().Be(DownloadItemStatus.Downloading);
}
protected void VerifyPostprocessing(DownloadClientItem downloadClientItem)
{
VerifyIdentifiable(downloadClientItem);
//downloadClientItem.RemainingTime.Should().NotBe(TimeSpan.Zero);
//downloadClientItem.OutputPath.Should().NotBeNullOrEmpty();
downloadClientItem.Status.Should().Be(DownloadItemStatus.Downloading);
}
protected void VerifyCompleted(DownloadClientItem downloadClientItem)
{
VerifyIdentifiable(downloadClientItem);