Merge branch 'kay.one' of github.com:NzbDrone/NzbDrone into markus

This commit is contained in:
Mark McDowall 2011-10-17 13:06:07 -07:00
commit 714bcee5cb
12 changed files with 130 additions and 40 deletions

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using NLog;
using NUnit.Framework;
namespace NzbDrone.Core.Test.Framework.AutoMoq
{
[TestFixture]
class TestBaseTests : TestBase
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
[Test]
public void Test_should_pass_when_no_exceptions_are_logged()
{
Logger.Info("Everything is fine and dandy!");
}
[Test]
public void Test_should_pass_when_errors_are_excpected()
{
Logger.Error("I knew this would happer");
ExceptionVerification.ExcpectedErrors(1);
}
[Test]
public void Test_should_pass_when_warns_are_excpected()
{
Logger.Warn("I knew this would happer");
ExceptionVerification.ExcpectedWarns(1);
}
[Test]
public void Test_should_pass_when_warns_are_ignored()
{
Logger.Warn("I knew this would happer");
Logger.Warn("I knew this would happer");
Logger.Warn("I knew this would happer");
ExceptionVerification.IgnoreWarns();
}
[Test]
public void Test_should_pass_when_errors_are_ignored()
{
Logger.Error("I knew this would happer");
Logger.Error("I knew this would happer");
Logger.Error("I knew this would happer");
ExceptionVerification.IgnoreErrors();
}
[Test]
public void Test_should_pass_when_exception_type_is_ignored()
{
Logger.ErrorException("bad exception", new WebException("Test"));
ExceptionVerification.MarkInconclusive(typeof(WebException));
}
}
}

View File

@ -72,8 +72,22 @@ namespace NzbDrone.Core.Test.Framework
Ignore(LogLevel.Error);
}
internal static void MarkInconclusive(Type exception)
{
var inconclusiveLogs = _logs.Where(l => l.Exception.GetType() == exception).ToList();
if (inconclusiveLogs.Count != 0)
{
inconclusiveLogs.ForEach(c => _logs.Remove(c));
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
}
}
private static void Excpected(LogLevel level, int count)
{
var levelLogs = _logs.Where(l => l.Level == level).ToList();
if (levelLogs.Count != count)
@ -82,8 +96,7 @@ namespace NzbDrone.Core.Test.Framework
var message = String.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}",
count, level, levelLogs.Count, GetLogsString(levelLogs));
message =
"********************************************************************************************************************************\n\r"
message = "********************************************************************************************************************************\n\r"
+ message +
"\n\r********************************************************************************************************************************";

View File

@ -21,8 +21,7 @@ namespace NzbDrone.Core.Test
{
try
{
LogManager.Configuration =
new XmlLoggingConfiguration(Path.Combine(CentralDispatch.AppPath, "log.config"), false);
LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine(CentralDispatch.AppPath, "log.config"), false);
LogManager.ThrowExceptions = true;
var exceptionVerification = new ExceptionVerification();
@ -35,7 +34,6 @@ namespace NzbDrone.Core.Test
Console.WriteLine("Unable to configure logging. " + e);
}
var filesToDelete = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories);
foreach (var file in filesToDelete)
{
@ -45,6 +43,8 @@ namespace NzbDrone.Core.Test
}
catch { }
}
MockLib.CreateDataBaseTemplate();
}
}
}

View File

@ -17,20 +17,19 @@ namespace NzbDrone.Core.Test.Framework
/// </summary>
internal static class MockLib
{
public static string[] StandardSeries
{
get { return new[] { "c:\\tv\\the simpsons", "c:\\tv\\family guy", "c:\\tv\\southpark", "c:\\tv\\24" }; }
}
private const string DbTemplateName = "_dbtemplate.sdf";
public static IDatabase GetEmptyDatabase(bool enableLogging = false, string fileName = "")
{
Console.WriteLine("Creating an empty PetaPoco database");
Console.WriteLine("Cloning database from template.");
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid() + ".sdf";
}
File.Copy(DbTemplateName, fileName);
var connectionString = Connection.GetConnectionString(fileName);
var database = Connection.GetPetaPocoDb(connectionString);
@ -38,7 +37,15 @@ namespace NzbDrone.Core.Test.Framework
return database;
}
public static Series GetFakeSeries(int id, string title)
public static void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = Connection.GetConnectionString(DbTemplateName);
var database = Connection.GetPetaPocoDb(connectionString);
database.Dispose();
}
public static Series GetFakeSeries(int id, string title)
{
return Builder<Series>.CreateNew()
.With(c => c.SeriesId = id)

View File

@ -66,7 +66,7 @@ namespace NzbDrone.Core.Test
.Setup(h => h.DownloadStream(It.IsAny<String>(), It.IsAny<NetworkCredential>()))
.Returns(File.OpenRead(".\\Files\\Rss\\newbin_none_english.xml"));
var newzbin = mocker.Resolve<Newzbin>();
var parseResults = newzbin.FetchRss();
@ -204,6 +204,8 @@ namespace NzbDrone.Core.Test
var result = mocker.Resolve<NzbsOrg>().FetchEpisode(title, season, episode);
ExceptionVerification.MarkInconclusive(typeof(WebException));
result.Should().NotBeEmpty();
result.Should().OnlyContain(r => r.CleanTitle == Parser.NormalizeTitle(title));
result.Should().OnlyContain(r => r.SeasonNumber == season);
@ -229,6 +231,8 @@ namespace NzbDrone.Core.Test
var result = mocker.Resolve<Newzbin>().FetchEpisode(title, season, episode);
ExceptionVerification.MarkInconclusive(typeof(WebException));
result.Should().NotBeEmpty();
result.Should().OnlyContain(r => r.CleanTitle == Parser.NormalizeTitle(title));
result.Should().OnlyContain(r => r.SeasonNumber == season);
@ -252,6 +256,8 @@ namespace NzbDrone.Core.Test
var result = mocker.Resolve<NzbMatrix>().FetchEpisode("Simpsons", 21, 23);
ExceptionVerification.MarkInconclusive(typeof(WebException));
result.Should().NotBeEmpty();
result.Should().OnlyContain(r => r.CleanTitle == "simpsons");
result.Should().OnlyContain(r => r.SeasonNumber == 21);
@ -275,11 +281,12 @@ namespace NzbDrone.Core.Test
var result = mocker.Resolve<NzbsOrg>().FetchEpisode("Blue Bloods", 1, 19);
ExceptionVerification.MarkInconclusive(typeof(WebException));
result.Should().NotBeEmpty();
result.Should().OnlyContain(r => r.CleanTitle == "bluebloods");
result.Should().OnlyContain(r => r.SeasonNumber == 1);
result.Should().OnlyContain(r => r.EpisodeNumbers.Contains(19));
}
[Test]
@ -299,6 +306,8 @@ namespace NzbDrone.Core.Test
var result = mocker.Resolve<NzbMatrix>().FetchEpisode("Blue Bloods", 1, 19);
ExceptionVerification.MarkInconclusive(typeof(WebException));
result.Should().NotBeEmpty();
result.Should().OnlyContain(r => r.CleanTitle == "bluebloods");
result.Should().OnlyContain(r => r.SeasonNumber == 1);

View File

@ -91,6 +91,7 @@
<Compile Include="BacklogSearchJobTest.cs" />
<Compile Include="BannerDownloadJobTest.cs" />
<Compile Include="ConfigFileProviderTest.cs" />
<Compile Include="Framework\AutoMoq\TestBaseTests.cs" />
<Compile Include="PostDownloadProviderTest.cs" />
<Compile Include="SortHelperTest.cs" />
<Compile Include="EpisodeProviderTest_DeleteInvalidEpisodes.cs" />

View File

@ -15,6 +15,7 @@ using PetaPoco;
namespace NzbDrone.Core.Test
{
[TestFixture]
[Category("Benchmark")]
// ReSharper disable InconsistentNaming
public class DbBenchmark : TestBase
{

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Datastore
EnsureDatabase(connetionString);
Logger.Info("Preparing to run database migration");
Logger.Trace("Preparing to run database migration");
try
{

View File

@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using Exceptioneer.WindowsFormsClient;
using NLog;
using NLog.Targets;
@ -12,17 +11,18 @@ namespace NzbDrone.Core.Instrumentation
protected override void Write(LogEventInfo logEvent)
{
if (!Debugger.IsAttached)
{
Logger.Trace("Sending Exception to Exceptioneer");
if (logEvent == null || logEvent.Exception == null) return;
if (Debugger.IsAttached || Process.GetCurrentProcess().ProcessName.Contains("JetBrains")) return;
Logger.Trace("Sending Exception to Exceptioneer. {0}", Process.GetCurrentProcess().ProcessName);
new Client
{
ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265",
ApplicationName = "NZBDrone",
CurrentException = logEvent.Exception
}.Submit();
new Client
{
ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265",
ApplicationName = "NZBDrone",
CurrentException = logEvent.Exception
}.Submit();
}
}
}
}

View File

@ -23,8 +23,8 @@ namespace NzbDrone.Core.Instrumentation
public static void StartDbLogging()
{
#if RELEASE
#if DEBUG
#else
var exTarget = new ExceptioneerTarget();
LogManager.Configuration.AddTarget("Exceptioneer", exTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Error, exTarget));

View File

@ -1,16 +1,12 @@
using System;
using System.Reflection;
using NLog;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone
{
public static class AppMain
{
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
public static void Main(string[] args)
{
try
@ -21,8 +17,7 @@ namespace NzbDrone
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
MonitoringProvider.AppDomainException(e);
}
}

View File

@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Timers;
using Exceptioneer.WindowsFormsClient;
using NLog;
using Ninject;
@ -33,7 +34,7 @@ namespace NzbDrone.Providers
public void Start()
{
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e));
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception));
AppDomain.CurrentDomain.ProcessExit += ProgramExited;
AppDomain.CurrentDomain.DomainUnload += ProgramExited;
@ -42,7 +43,7 @@ namespace NzbDrone.Providers
prioCheckTimer.Elapsed += EnsurePriority;
prioCheckTimer.Enabled = true;
_pingTimer = new Timer(60000) {AutoReset = true};
_pingTimer = new Timer(60000) { AutoReset = true };
_pingTimer.Elapsed += (PingServer);
_pingTimer.Start();
}
@ -101,12 +102,12 @@ namespace NzbDrone.Providers
}
private static void AppDomainException(object excepion)
public static void AppDomainException(Exception excepion)
{
Console.WriteLine("EPIC FAIL: {0}", excepion);
Logger.Fatal("EPIC FAIL: {0}", excepion);
#if RELEASE
#if DEBUG
#else
new Client
{
ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265",
@ -114,6 +115,8 @@ namespace NzbDrone.Providers
CurrentException = excepion as Exception
}.Submit();
#endif
Logger.Fatal("EPIC FAIL: {0}", excepion);
}
}
}