General fixes and adjusted ParseSize method.

This commit is contained in:
Taloth Saldono 2014-09-07 13:37:50 +02:00
parent 518a75ea5c
commit 82061cf5a0
10 changed files with 105 additions and 84 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Net;
using System.Linq;
@ -35,12 +36,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
};
}
protected void WithSuccessfulDownload()
{
}
protected void WithFailedDownload()
protected void GivenFailedDownload()
{
Mocker.GetMock<IHttpClient>()
.Setup(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
@ -110,5 +106,15 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
result.Status.Should().Be(DownloadItemStatus.Downloading);
}
[Test]
public void should_return_status_with_outputdirs()
{
var result = Subject.GetStatus();
result.IsLocalhost.Should().BeTrue();
result.OutputRootFolders.Should().NotBeNull();
result.OutputRootFolders.First().Should().Be(_completedDownloadFolder);
}
}
}

View File

@ -92,26 +92,26 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
.Returns(configItems);
}
protected void WithMountPoint(String mountPath)
protected void GivenMountPoint(String mountPath)
{
(Subject.Definition.Settings as NzbgetSettings).TvCategoryLocalPath = mountPath;
}
protected void WithFailedDownload()
protected void GivenFailedDownload()
{
Mocker.GetMock<INzbgetProxy>()
.Setup(s => s.DownloadNzb(It.IsAny<Stream>(), It.IsAny<String>(), It.IsAny<String>(), It.IsAny<int>(), It.IsAny<NzbgetSettings>()))
.Returns((String)null);
}
protected void WithSuccessfulDownload()
protected void GivenSuccessfulDownload()
{
Mocker.GetMock<INzbgetProxy>()
.Setup(s => s.DownloadNzb(It.IsAny<Stream>(), It.IsAny<String>(), It.IsAny<String>(), It.IsAny<int>(), It.IsAny<NzbgetSettings>()))
.Returns(Guid.NewGuid().ToString().Replace("-", ""));
}
protected virtual void WithQueue(NzbgetQueueItem queue)
protected virtual void GivenQueue(NzbgetQueueItem queue)
{
var list = new List<NzbgetQueueItem>();
@ -129,7 +129,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
.Returns(new List<NzbgetPostQueueItem>());
}
protected virtual void WithHistory(NzbgetHistoryItem history)
protected virtual void GivenHistory(NzbgetHistoryItem history)
{
var list = new List<NzbgetHistoryItem>();
@ -146,8 +146,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void GetItems_should_return_no_items_when_queue_is_empty()
{
WithQueue(null);
WithHistory(null);
GivenQueue(null);
GivenHistory(null);
Subject.GetItems().Should().BeEmpty();
}
@ -157,8 +157,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
{
_queued.ActiveDownloads = 0;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -170,8 +170,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
{
_queued.PausedSizeLo = _queued.RemainingSizeLo;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -183,8 +183,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
{
_queued.ActiveDownloads = 1;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -194,8 +194,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void completed_download_should_have_required_properties()
{
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -205,8 +205,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void failed_item_should_have_required_properties()
{
WithQueue(null);
WithHistory(_failed);
GivenQueue(null);
GivenHistory(_failed);
var result = Subject.GetItems().Single();
@ -216,7 +216,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void Download_should_return_unique_id()
{
WithSuccessfulDownload();
GivenSuccessfulDownload();
var remoteEpisode = CreateRemoteEpisode();
@ -230,8 +230,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
{
_completed.Category = "mycat";
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var items = Subject.GetItems();
@ -251,7 +251,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void should_return_status_with_mounted_outputdir()
{
WithMountPoint(@"O:\mymount".AsOsAgnostic());
GivenMountPoint(@"O:\mymount".AsOsAgnostic());
var result = Subject.GetStatus();
@ -263,10 +263,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests
[Test]
public void should_remap_storage_if_mounted()
{
WithMountPoint(@"O:\mymount".AsOsAgnostic());
GivenMountPoint(@"O:\mymount".AsOsAgnostic());
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();

View File

@ -106,19 +106,19 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
.Returns(_config);
}
protected void WithMountPoint(String mountPath)
protected void GivenMountPoint(String mountPath)
{
(Subject.Definition.Settings as SabnzbdSettings).TvCategoryLocalPath = mountPath;
}
protected void WithFailedDownload()
protected void GivenFailedDownload()
{
Mocker.GetMock<ISabnzbdProxy>()
.Setup(s => s.DownloadNzb(It.IsAny<Stream>(), It.IsAny<String>(), It.IsAny<String>(), It.IsAny<int>(), It.IsAny<SabnzbdSettings>()))
.Returns((SabnzbdAddResponse)null);
}
protected void WithSuccessfulDownload()
protected void GivenSuccessfulDownload()
{
Mocker.GetMock<ISabnzbdProxy>()
.Setup(s => s.DownloadNzb(It.IsAny<Stream>(), It.IsAny<String>(), It.IsAny<String>(), It.IsAny<int>(), It.IsAny<SabnzbdSettings>()))
@ -129,7 +129,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
});
}
protected virtual void WithQueue(SabnzbdQueue queue)
protected virtual void GivenQueue(SabnzbdQueue queue)
{
if (queue == null)
{
@ -145,7 +145,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
.Returns(queue);
}
protected virtual void WithHistory(SabnzbdHistory history)
protected virtual void GivenHistory(SabnzbdHistory history)
{
if (history == null)
history = new SabnzbdHistory() { Items = new List<SabnzbdHistoryItem>() };
@ -158,8 +158,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
[Test]
public void GetItems_should_return_no_items_when_queue_is_empty()
{
WithQueue(null);
WithHistory(null);
GivenQueue(null);
GivenHistory(null);
Subject.GetItems().Should().BeEmpty();
}
@ -170,8 +170,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_queued.Items.First().Status = status;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -184,8 +184,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_queued.Items.First().Status = status;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -205,8 +205,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_queued.Items.First().Status = status;
WithQueue(_queued);
WithHistory(null);
GivenQueue(_queued);
GivenHistory(null);
var result = Subject.GetItems().Single();
@ -217,8 +217,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
[Test]
public void completed_download_should_have_required_properties()
{
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -230,8 +230,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_completed.Items.First().Status = SabnzbdDownloadStatus.Failed;
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -241,7 +241,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
[Test]
public void Download_should_return_unique_id()
{
WithSuccessfulDownload();
GivenSuccessfulDownload();
var remoteEpisode = CreateRemoteEpisode();
@ -255,8 +255,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_completed.Items.First().Category = "myowncat";
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var items = Subject.GetItems();
@ -292,8 +292,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
_completed.Items.First().Title = title;
_completed.Items.First().Storage = (@"C:\sorted\" + title + @"\" + storage).AsOsAgnostic();
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -303,10 +303,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
[Test]
public void should_remap_storage_if_mounted()
{
WithMountPoint(@"O:\mymount".AsOsAgnostic());
GivenMountPoint(@"O:\mymount".AsOsAgnostic());
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -318,8 +318,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_completed.Items.First().Storage = @"C:\".AsOsAgnostic();
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -331,8 +331,8 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
{
_completed.Items.First().Storage = @"C:\sorted\somewhere\asdfasdf\asdfasdf.mkv".AsOsAgnostic();
WithQueue(null);
WithHistory(_completed);
GivenQueue(null);
GivenHistory(_completed);
var result = Subject.GetItems().Single();
@ -349,7 +349,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
_config.Misc.complete_dir = completeDir;
_config.Categories.First().Dir = categoryDir;
WithQueue(null);
GivenQueue(null);
var result = Subject.GetStatus();
@ -361,9 +361,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests
[Test]
public void should_return_status_with_mounted_outputdir()
{
WithMountPoint(@"O:\mymount".AsOsAgnostic());
GivenMountPoint(@"O:\mymount".AsOsAgnostic());
WithQueue(null);
GivenQueue(null);
var result = Subject.GetStatus();

View File

@ -16,7 +16,7 @@ namespace NzbDrone.Core.Test.IndexerTests
[TestCase("845 MB", 886046720)]
public void parse_size(string sizeString, long expectedSize)
{
var result = RssParserBase.ParseSize(sizeString);
var result = RssParserBase.ParseSize(sizeString, true);
result.Should().Be(expectedSize);
}

View File

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentValidation.Results;
using NLog;
using System.Collections.Generic;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
@ -13,6 +11,9 @@ using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.MediaFiles;
using NLog;
using Omu.ValueInjecter;
using FluentValidation.Results;
namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
{
@ -115,6 +116,7 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
else
{
historyItem.Status = DownloadItemStatus.Completed;
historyItem.RemainingTime = TimeSpan.Zero;
}
historyItem.RemoteEpisode = GetRemoteEpisode(historyItem.Title);

View File

@ -106,8 +106,9 @@ namespace NzbDrone.Core.Download
else
{
//TODO: Make this more configurable (ignore failure reasons) to support changes and other failures that should be ignored
if (trackedDownload.DownloadItem.Message.Equals("Unpacking failed, write error or disk is full?",
StringComparison.InvariantCultureIgnoreCase))
if (!trackedDownload.DownloadItem.Message.IsNullOrWhiteSpace() &&
trackedDownload.DownloadItem.Message.Equals("Unpacking failed, write error or disk is full?",
StringComparison.InvariantCultureIgnoreCase))
{
UpdateStatusMessage(trackedDownload, LogLevel.Error, "Download failed due to lack of disk space, not blacklisting.");
return;

View File

@ -45,7 +45,7 @@ namespace NzbDrone.Core.Indexers.Newznab
return Convert.ToInt64(sizeElement.Attribute("value").Value);
}
return ParseSize(item.Description());
return ParseSize(item.Description(), true);
}
public override IEnumerable<ReleaseInfo> Process(string xml, string url)

View File

@ -23,7 +23,7 @@ namespace NzbDrone.Core.Indexers.Omgwtfnzbs
protected override long GetSize(XElement item)
{
var sizeString = Regex.Match(item.Description(), @"(?:Size:\<\/b\>\s\d+\.)\d{1,2}\s\w{2}(?:\<br \/\>)", RegexOptions.IgnoreCase | RegexOptions.Compiled).Value;
return ParseSize(sizeString);
return ParseSize(sizeString, true);
}
}
}

View File

@ -31,7 +31,7 @@ namespace NzbDrone.Core.Indexers
{
PreProcess(xml, url);
using (var xmlTextReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse, IgnoreComments = true }))
using (var xmlTextReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
{
var document = XDocument.Load(xmlTextReader);
@ -124,7 +124,7 @@ namespace NzbDrone.Core.Indexers
private static readonly Regex ReportSizeRegex = new Regex(@"(?<value>\d+\.\d{1,2}|\d+\,\d+\.\d{1,2}|\d+)\W?(?<unit>GB|MB|GiB|MiB)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static long ParseSize(string sizeString)
public static Int64 ParseSize(String sizeString, Boolean defaultToBinaryPrefix)
{
var match = ReportSizeRegex.Matches(sizeString);
@ -133,26 +133,33 @@ namespace NzbDrone.Core.Indexers
var cultureInfo = new CultureInfo("en-US");
var value = Decimal.Parse(Regex.Replace(match[0].Groups["value"].Value, "\\,", ""), cultureInfo);
var unit = match[0].Groups["unit"].Value;
var unit = match[0].Groups["unit"].Value.ToLower();
if (unit.Equals("MB", StringComparison.InvariantCultureIgnoreCase) ||
unit.Equals("MiB", StringComparison.InvariantCultureIgnoreCase))
switch (unit)
{
return ConvertToBytes(Convert.ToDouble(value), 2);
}
if (unit.Equals("GB", StringComparison.InvariantCultureIgnoreCase) ||
unit.Equals("GiB", StringComparison.InvariantCultureIgnoreCase))
{
return ConvertToBytes(Convert.ToDouble(value), 3);
case "kb":
return ConvertToBytes(Convert.ToDouble(value), 1, defaultToBinaryPrefix);
case "mb":
return ConvertToBytes(Convert.ToDouble(value), 2, defaultToBinaryPrefix);
case "gb":
return ConvertToBytes(Convert.ToDouble(value), 3, defaultToBinaryPrefix);
case "kib":
return ConvertToBytes(Convert.ToDouble(value), 1, true);
case "mib":
return ConvertToBytes(Convert.ToDouble(value), 2, true);
case "gib":
return ConvertToBytes(Convert.ToDouble(value), 3, true);
default:
return (Int64)value;
}
}
return 0;
}
private static long ConvertToBytes(double value, int power)
private static Int64 ConvertToBytes(Double value, Int32 power, Boolean binaryPrefix)
{
var multiplier = Math.Pow(1024, power);
var prefix = binaryPrefix ? 1024 : 1000;
var multiplier = Math.Pow(prefix, power);
var result = value * multiplier;
return Convert.ToInt64(result);

View File

@ -49,6 +49,11 @@ namespace NzbDrone.Core.Rest
{
restResponse.ValidateResponse(restClient);
if (restResponse.Content != null)
{
Logger.Trace("Response: " + restResponse.Content);
}
return Json.Deserialize<T>(restResponse.Content);
}